The Reports Repository

Author

Robert W. Walker

The Entire Process

I want to generate parameterized R Markdown documents and then link them via a table. This will go in two parts.

  1. I will show two ways to make use of parameterized markdown and R. I will deploy RMarkdown and then quarto markdown. Each has advantages and deficiencies, as you will see.
  2. I want to build a table of the documents. ## RMarkdown

RMarkdown with parameters is a bit easier because of the way that it handles output directories and the like. For example, in the first iteration of this project, I could use something like the following to render all of the documents to some specified target directory. NB: Because this is all built for github pages, the root of the served directory defaults to /docs.

library(tidyquant)
library(rmarkdown)
library(purrr)
Equity.Analyser <- function(x) {
  render("TQ-Parameters.qmd", 
         params = list(ticker = x), 
         output_file = paste0("docs/files/",x,".html"))
}
SP500 <- tq_index("SP500")
SP500$symbol %>% map(., function(x) { Equity.Analyser(x)})

Quarto

This took two steps. The first one is to locate where I want the files to appear. In this case, I want them to show up in the docs/files directory. On the Mac, I can deliberately select the working directory like so.

setwd("/Vols/Work/ReportTQ/docs/files")

The issue is that the input file is in the root and quarto_render doesn’t really like this. Instead, I copy the bibliography file and the template [TQ-Parameters.qmd] to this same directory and then invoke from there. With a working directory set, I can then invoke the map with quarto_render and get the files rendered.

library(tidyquant)
library(quarto)
library(purrr)
Equity.Analyser <- function(x) {
  quarto_render("TQ-Parameters.qmd", 
                execute_params = list(ticker = x), 
                output_file = paste0(x,".html"))
}
SP400 <- tq_index("SP400")
SP400$symbol %>% map(., function(x) { Equity.Analyser(x)})

Once I have executed the above, the .html files will be addressable from an expected location with an expected name. Let me now turn to building that table.

A Big Table

I want to use datatable for this. Most of the data comes packaged appropriately from tq_index; I need to match up the naming conventions for the .html files to the directory and naming conventions of the .html.

Code
library(tidyquant)
library(tidyverse)
library(DT)
library(magrittr)
# Grab the relevant index
SP400 <- tq_index("SP400")
# Mutate the relevant columns, combine them, round them, and select what we want to display.
SP400 %<>% mutate(weight = round(weight, digits=3), 
                  links=paste0("https://robertwwalker.github.io/ReportTQ/files/",symbol,".html", sep=""), 
                  file=paste0("Report on ",company,sep=""),
                  sector=as.factor(sector),
                  ) %>% mutate(Report = paste0('<a  target=_blank href=\'', links, '\' >', file,'</a>' ))
SP400.Res <- SP400 %>% select(-c(file, links, company)) %>% relocate(Report, .after=symbol) %>% relocate(sector, .after=Report)
datatable(SP400.Res, 
          filter="top", 
          escape=FALSE, 
          colnames = c('Ticker', 'Report Link', 'Sector', 'CUSIP', 'SEDOL', 'Weight','Shares Held','Currency'), 
          rownames = FALSE)

What do we have?

The Items

  • Ticker
  • Sector
  • Report on Company
  • CUSIP [Committee on Uniform Securities Identification Procedures]
  • SEDOL [Stock Exchange Daily Official List]

Some components of the index.

An Example File as iframe

Screen shot

References

Code
knitr::write_bib(names(sessionInfo()$otherPkgs), file="bibliography.bib")

References

Bache, Stefan Milton, and Hadley Wickham. 2022. Magrittr: A Forward-Pipe Operator for r. https://CRAN.R-project.org/package=magrittr.
Dancho, Matt, and Davis Vaughan. 2022. Tidyquant: Tidy Quantitative Financial Analysis. https://github.com/business-science/tidyquant.
Grolemund, Garrett, and Hadley Wickham. 2011. “Dates and Times Made Easy with lubridate.” Journal of Statistical Software 40 (3): 1–25. https://www.jstatsoft.org/v40/i03/.
Müller, Kirill, and Hadley Wickham. 2023. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.
Peterson, Brian G., and Peter Carl. 2020. PerformanceAnalytics: Econometric Tools for Performance and Risk Analysis. https://github.com/braverock/PerformanceAnalytics.
Ryan, Jeffrey A., and Joshua M. Ulrich. 2022. Quantmod: Quantitative Financial Modelling Framework. https://CRAN.R-project.org/package=quantmod.
———. 2023. Xts: eXtensible Time Series. https://github.com/joshuaulrich/xts.
Spinu, Vitalie, Garrett Grolemund, and Hadley Wickham. 2023. Lubridate: Make Dealing with Dates a Little Easier. https://CRAN.R-project.org/package=lubridate.
Ulrich, Joshua. 2021. TTR: Technical Trading Rules. https://github.com/joshuaulrich/TTR.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
———. 2022. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.
———. 2023a. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.
———. 2023b. Tidyverse: Easily Install and Load the Tidyverse. https://CRAN.R-project.org/package=tidyverse.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.” Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2023. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Wickham, Hadley, and Lionel Henry. 2023. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2023. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.
Wickham, Hadley, Davis Vaughan, and Maximilian Girlich. 2023. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.
Xie, Yihui, Joe Cheng, and Xianying Tan. 2023. DT: A Wrapper of the JavaScript Library DataTables. https://github.com/rstudio/DT.
Zeileis, Achim, and Gabor Grothendieck. 2005. “Zoo: S3 Infrastructure for Regular and Irregular Time Series.” Journal of Statistical Software 14 (6): 1–27. https://doi.org/10.18637/jss.v014.i06.
Zeileis, Achim, Gabor Grothendieck, and Jeffrey A. Ryan. 2022. Zoo: S3 Infrastructure for Regular and Irregular Time Series (z’s Ordered Observations). https://zoo.R-Forge.R-project.org/.