file.remove() with dynamic input in R - 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.

Related

Use texreg::wordreg within whole markdown knitted Word document

I want to use texreg::wordreg within a Markdown file with several other calculations that I want to finally knit to an MS Word file. However, the function demands that I enter a file name and hence export each table separately. Is there a workaround that I could use so I don't need extra files for each table? I already tried "file = NULL" but it doesn't work (Error: "'file' must be a valid file path.")
That's the expected behaviour of wordreg.
Try
texreg::knitreg(your_list_of_models, center = FALSE)
It should automatically adapt your texreg table based on the type of document that you are knitting.

Line breaks in directory using knitr/R Markdown

I'm currently working with R Markdown and trying to get the code, results and documentation to one pdf-file with the knit function in the end.
Within in the document, I want to set my directory with the setwd command.
setwd("C:/Users/Testuser/Documents/Testfolder/Testsubfolder/Testsubfolder2/Testsubfolder3/Testsubfolder4/Testsubfolder5")
However, the file path exceeds the width of the pdf and there is no line break so part of it is cut off at the right side. When I try to insert a page break manually, for example by pressing Enter after "Testdubfolder2", I get the following Error message:
"Fehler in setwd("C:/Users/Testuser/Documents/Testfolder/Testsubfolder/Testsubfolder2/\n
Testsubfolder3/Testsubfolder4/Testsubfolder5") : kann Arbeitsverzeichnis nicht wechseln."
, which in English simply means, that the directory can't be changed. Is there any way to display the file path over two lines using the knit function? I'd be grateful for any solution or hint.
You can use file.path to construct paths from multiple subpaths, for example:
setwd(file.path(
"C:/Users/Testuser/Documents",
"Testfolder/Testsubfolder/Testsubfolder2",
"Testsubfolder3/Testsubfolder4/Testsubfolder5"
))

Save a Sweave PDF output using custom file name

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.

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

Send parameters to child Knitr file

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)

Resources