Save a Sweave PDF output using custom file name - r

I have a sweave code that is producing a pdf. The code works fine.
I run it from the command line using:"
R CMD Sweave --pdf filename.Rnw
The resulting pdf that is produced is save in the working directory as
filename.pdf
Instead I would like 2 things:
Have the resulting output .pdf saved in another directory say wrk\random\dir
Change the name on a daily basis by adding the date at the end of it so I can maintain a history - such as filename.05032017.pdf
I tried using suggestions from here: Attach date to PDF generated with Sweave
but that didn't work. Needless to say it could be because I am absolutely new to R/SWEAVE and suck?

I don't think that can be done on the command line, but you can do it if you run Sweave within R:
filename <- paste0("wrk/random/dir/filename", format(Sys.Date(), "%d%m%Y"), ".tex")
Sweave("filename.Rnw", output=filename)
tools::texi2pdf(filename)
You could put this in a *.R file and run it with Rscript if you don't want to start R.

Related

Run only R commands in rmd file

I have a large Markdown file (*.Rmd) and want to execute only the R commands in that file (not the text) in RStudio. How can this be done?
If I mark the whole document and try to execute it, R tries to execute also the text in the Markdown file, which, of course, results in errors.
Click on the arrows at the top right of the chunks:

Can't run Dataframes chunks on R Notebook (Rstudio)

Anytime I try to check on a dataframe on a .Rmd file, RStudio won't run it. I can read the csv file, but when I call it, nothing happens. I can tell it's a problem with Dataframes because I can run mathematical equations and even plot some graphic.
When I open the .html file, saved using Knitr, it's all ok:

How to remove .tex after running knit and texi2pdf

I have an R data frame that I run through knitr using the following code:
knit('reportTemplate.Rnw', 'file.tex') # creates a .tex file from the .Rnw one
texi2pdf('file.tex') # creates a .pdf file from the .tex one
Inside my R script, I want to remove 'file.tex' from my computer folder afterwards. How do I achieve this? It is important that I do this within my .R file, since those lines are actually inside a loop that generates 1000 different reports from that template.
There are a family of functions in R that allow the user to interact with the computer's file system. Run ?files to see functions that make it possible to, for instance, create, rename and remove files.
As noted by Josh O'Brien, in a comment to the OP, in this specific case, the command to be used is file.remove('file.tex').

Knit2html not replicating functionality of Knit HTML button in R Studio

I'm trying to write a Bash script in Ubuntu 10.04 that opens a Python file which exports a CSV, and then runs the following Rscript with the goal of exporting a HTML with plots from Dashboard.Rmd:
require(knitr)
setwd('/home/sensors/Desktop/')
knit2html('Dashboard.Rmd')
browseURL('Dashboard.html')
Dashboard.Rmd is an R markdown that calls read.csv on the csv from the first step, makes a data frame and creates plots, but that part's working fine. According to this, I figure that Rscript should replicate the action of pressing "Knit HTML" in R Studio. However, the html it creates is identical to the last time Knit HTML was pressed; i.e. even if the CSV is different, the html doesn't reflect the change.
I also tried using a separate line for knit and markdownToHTML with the same effect. It seems like it doesn't source the code from the Rmd when performing knit. It does update the html properly when I enter the commands from that Rscript into the console of R Studio with Dashboard.Rmd open. However I'm not sure how to translate that into a Bash script. I also tried knit2html with envir=new.env(), envir=R_GlobalEnv, and envir=parent.frame() with no luck. Any help would be appreciated!
So it turns out that this was an artifact of cache=TRUE -- the HTML file was not changed because everything was cached.

How to convert R Markdown to HTML? I.e., What does "Knit HTML" do in Rstudio 0.96?

What commands are run when pressing "Knit HTML" on an R Markdown file in Rstudio 0.96?
My motivation is that I might want to run the same command when I'm in another text editing environment or I might want to combine the command in a larger makefile.
Basic Script
So now that the R markdown package has been released, here is some code to replicate the features of Knit to Html.
require(knitr) # required for knitting from rmd to md
require(markdown) # required for md to html
knit('test.rmd', 'test.md') # creates md file
markdownToHTML('test.md', 'test.html') # creates html file
browseURL(paste('file://', file.path(getwd(),'test.html'), sep='')) # open file in browser
where test.rmd is the name of your R markdown file.
Note that I'm not 100% confident about the browseURL line (hence my question here about opening files in a web browser).
markdownToHTML Options
The good thing about markdownToHTML is that there are heaps of options in how the HTML is created (see ?markdownHTMLOptions). So for example, if you want just a code fragment without all the header information, you could write:
markdownToHTML('test.md', 'test.html', options='fragment_only')
or if you don't like hard wrapping (i.e., inserting line breaks when there are single manual line breaks in the markdown source), you can omit the 'hard_wrap' option.
# The default options are 'hard_wrap', 'use_xhtml',
# 'smartypants', and 'base64_images'.
markdownToHTML('test.md', 'test.html',
options=c('use_xhtml', 'base64_images'))
Makefile
This could also all be added to a makefile perhaps using Rscript -e (e.g., something like this). Here's a basic example makefile I put together, where test indicates that the rmd file is called test.rmd.
RMDFILE=test
html :
Rscript -e "require(knitr); require(markdown); knit('$(RMDFILE).rmd', '$(RMDFILE).md'); markdownToHTML('$(RMDFILE).md', '$(RMDFILE).html', options=c('use_xhtml', 'base64_images')); browseURL(paste('file://', file.path(getwd(),'$(RMDFILE).html'), sep=''))"
The makefile uses my preferred markdown options: i.e., options=c('use_xhtml', 'base64_images')
Put Sys.sleep(30) in a chunk and you will see clearly what commands are called by RStudio. Basically they are
library(knitr); knit() to get the markdown file;
RStudio has internal functions to convert markdown to HTML;
The second step will be more transparent in the next version of the markdown package. Currently you can use knitr::knit2html('your_file.Rmd') to get a similar HTML file as RStudio gives you.
Update on 2019/09/17: The above answer applies to RStudio v0.96 (in the year 2012). Now R Markdown is compiled through rmarkdown::render(), which uses Pandoc instead of the retired R package markdown. See the post Relationship between R Markdown, Knitr, Pandoc, and Bookdown for more details.
Very easy command line method from knitr in a knutshell:
R -e "rmarkdown::render('knitr_example.Rmd')"
This requires rmarkdown to be installed with install.packages(rmarkdown) and that pandoc is installed (apparently it comes with Rstudio, see knitr in a knutshell for more details).
So far when I've used this it nicely puts all the plots in the HTML file rather than as images in a figure directory and cleans up any intermediate files, if any; just like compilation in RStudio does.
It seems you should call rmarkdown::render() instead of knitr::knit2html() because a.rmd appears to be an R Markdown v2 document.

Resources