Send parameters to child Knitr file - r

I have some identical sections in a document but the entry data's files are differents.
Is it possible to make a master knitr file who all sections are wrote and call a child Knitr-file, who have the code for the corps of all section (identical code), but take as parameter the datafiles ?
Lile if we passed some parameters to a R-script sourced in another script.

One solution is this workflow:
Write the knitr file (Rnw or rmd, whatever) which uses a few variables that are not defined in that script.
Write a function that knits the knitr file, and takes a bunch of arguments that the knitr file should use (these should correspond in name to the variables I refer to in 1.)
Then knit using the envir argument (assuming it is an rmd file):
knitrenv <- new.env()
assign("someargument", someargument, knitrenv)
assign("someargument2", someargument2, knitrenv)
knit2html(myrmdfile, envir=knitrenv)

Related

Change same line of code in multiple RMD or R files

Is there a way to change the same line of code in multiple RMD or R script files instead of manually going in to each file and changing it?
also is there a way to change the first chunk of multiple RMD files to something else instead of manually doing it? The code in the first chunk of each RMD file isn't exactly the same but I want to change the entire first chunk of each RMD file to some other code.
One straightforward option here is to create a separate R script file that performs the process included in the R line or the RMD chunk and simply source that script file in all of the separate R script files or RMD files that use it. Then when you need to change that process you simply edit the external sourced script file.

file.remove() with dynamic input in R

I create dynamic LaTex reports in R markdown and want to delete the .tex file after the .pdf has been created as part of the overall function get_report. The name of the .pdf file in dynamic. I tried
file.remove(paste0("/sang/pect/apps/office/admin/", DATA$report_filename, ".tex"))
where DATA$report_filename is the name of the .pdf that is being created. So if i want to remove the file of a report called "hello", the path would be /sang/pect/apps/office/admin/hello.tex
When I call the function mentioned above, I however get an error: 'No such file or directory'
The reason is that R tries to remove the file /sang/pect/apps/office/admin/.tex
It just leaves out the dynamic part
But when i run
file.remove(paste0("/sang/pect/apps/office/admin/", DATA$report_filename, ".tex"))
in the console, all works well.
So what is my mistake?
If paste0("/sang/pect/apps/office/admin/", DATA$report_filename, ".tex") returns "/sang/pect/apps/office/admin/.tex", I would inspect the DATA$report_filename vector by e.g. using print() function.

Keep auxiliary TeX files when rendering a rmarkdown document

I am rendering two documents, that cross-reference items in each other. For that I am using the LaTeX package zref.
To make zref work, it needs the *.aux file of the documents which are created when calling pdflatex.
Unfortunately, using RStudio and its basic approach to render the document (the knit button, Cmd+Shift+K or rmarkdown::render()) these files will be deleted after the compilation was successful.
Unchecking the global option Tools -> Global Options -> Sweave -> Clean auxiliary output after compile does not help.
I know of two options to go around this:
Manually compile the tex file after the pdf was rendered.
Write a makefile that does that.
But is there another option I am not aware of?
rmarkdown::render() eventually calls tinytex::latexmk() to compile the intermediate .tex to .pdf. To preserve auxiliary files, you need tinytex::latexmk(..., clean = FALSE). One way to set clean = FALSE is through the global option options(tinytex.clean = FALSE). You can set this in either your .Rprofile or a code chunk of your Rmd document.
The RStudio option you mentioned is only for Sweave documents (.Rnw).

Using knit spin or stitch to interleave R into a template

This is probably documented somewhere, but I cannot find it ...
I have an .R file which can be used as a read_chunk() file and call from an .Rnw latex template or in a .Rmd file for primary review.
This design worked well for the first part of our project, but since the .R file will change, it's not 'reproducible research'.
Since I already have a 'template' with the named chunks referring to chunks in the read_chunk() file, is there any way to interleave the R into the .Rnw for posterity?
You want the reverse operation of purl(), I believe. I don't know any such function, but technically it doesn't seem too hard; all the pieces are there:
chunks <- knitr:::knit_code$get()
for(k in names(chunks))
cat(c(sprintf("<<%s>>=", k), chunks[[k]], "#\n"), sep = "\n")
would return all the chunks in a format suitable for inclusion in the Rnw file.

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').

Resources