Summary
I like to create a pdf file from an rmd script by simply clicking on the icon / icon so that my staff do not run out of their tracks by opening RStudio first.
Question
When I saw this one on R-bloggers and got it working, I thought that I was approaching the ideal workflow from scripts to sharing my work, allowing my colleagues to execute the file and get pdf with updated numbers as a result. However, I cannot get it to work with some functions of the knitr library.
The best random scenario is that this question is only interesting to some of you there, but here goes:
Below you can see the script in a file called RexecKnit.Rmd . The only reason it exists is that you can test the entire procedure for yourself if you want. By the way, I am running RStudio version 0.99.467 on Windows 7, 64 bit.
--- title: "Executable R, rmd and pdf" header-includes: \usepackage{caption} \usepackage{fancyhdr} output: pdf_document fig_caption: no --- \addtolength{\headheight}{0.5cm} \pagestyle{fancyplain} \renewcommand{\headrulewidth}{0pt} ```{r Settings, echo = FALSE, eval = TRUE, results = "hide", warning = FALSE, message = FALSE} rm(list=ls()) pck_loaded <- (.packages()) # Packages to load pck_toload <- c('ggplot2', 'xts', 'quantmod', 'zoo', 'PerformanceAnalytics', 'tseries', 'mvtnorm', 'data.table', 'XLConnect', 'sqldf', 'stargazer', 'xtable', 'gridExtra', 'grid', 'TTR') # Load packages for(i in 1:length(pck_toload)) { if (!pck_toload[i] %in% pck_loaded) print(pck_toload[i]) library(pck_toload[i], character.only = TRUE) } ``` \captionsetup[table]{labelformat=empty} ```{r repex1, echo = FALSE, eval = TRUE, results = "asis", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8} # Data table with formatted numbers and text v1 <- c("\\colorbox{white}{0.05}" , "\\colorbox{yellow}{0.57}", "\\colorbox{red}{-0.99}") v2 <- c("An unexpected comment", "A qurious question", "And an insightful answer") dt1 <- data.table(v1,v2) # Data table using xtable print(xtable(dt1, caption = 'Fancy table'), caption.placement = 'top', comment = FALSE, sanitize.text.function = function(x) x) ``` ```{r repex2, echo = FALSE, eval = TRUE, results = "asis", warning = FALSE, message = FALSE, fig.width = 12, fig.height = 8} # Data table wiht random numbers dt2 <- data.table(replicate(2,sample(0:100,10,rep=TRUE))) # ggplot of random numbers plx <- ggplot(data=dt2 ,aes(x=V1, y = V2)) plx <- plx + geom_line(color = 'blue', fill = 'grey') plx <- plx + theme_classic() plx <- plx + labs(title="Random numbers", x="x-axis",y="y-axis") plot(plx) ```
I know that a fairly long script is for testing, but just to make sure everything works when I execute the script by double-clicking this little beauty
, which is a file called caller knitr.Rexe (for example, in an R-Bloggers post) containing this little piece of code:
library(knitr) library(rmarkdown) setwd('C:/repos/r_tutorials/executable R files') knit('RexecKnit.Rmd') Sys.sleep(3)
And it works as expected. When the file is double-clicked, the script is launched without opening R or Rstudio, and the .md file is created in the desired folder. And the same script works when it is stored as a .Rexe file and as a .R file. But here is the problem:
I want to create a pdf file, and as prompted here , replacing
knit('RexecKnit.Rmd')
from
rmarkdown::render("RexecKnit.Rmd")
should do the trick until the YAML header includes the following:
output: pdf_document
And this works (this means that pdf is created with every detail specified in the lenghty script), at least when it is run as a .R file. To my disappointment, it does NOT work when it is launched from a .Rexec file as follows:
library(knitr) library(rmarkdown) setwd('C:/repos/r_tutorials/executable R files') rmarkdown::render("RexecKnit.Rmd") Sys.sleep(3)
Thanks for watching this!