Run a file.R inside other code in R
I have a code that reads the XML files that are in a corporate network folder, and generates a file .RData
. I have other codes that generate different reports for the different sectors, using RData
as a basis. My problem is: these XML files are updated with varying frequencies and I can't always know when the last update was performed. I would like to know if there is any function that executes this other code (which generates the RData
) whenever it generates a report, precisely to avoid an outdated XML-based RData file and having to run the other code manually every time a new report is requested.
PS: I know I can insert code that reads XML inside the report Code, but I don't do it to prevent the report from getting too extensive .
Edit: the script leiturapastaDIPR.R
generates the file DIPRConsolidado.RData
.RData is a list with 5 elements and each of them is a Data Frame. Then this RData is loaded into the RMarkDown markdown.RMD
which generates the PDF of the report. If you need any more information, let us know!
1 answers
Makefile
it works as a chain of rules to where you need to determine the , target, which is usually the object that you want to create (in this case a pdf), the dependencies at this , target, which are required to create it, and, finally, the recipe - what is the command used to create the , target. Here's a pattern:
target: depencia1
receita
In the example above, if dependência1
is not up to date, makefile
will run receita
to then create the target
.
Example
Whereas the file name of the report you want to create is relatorio.Rmd
which will generate the pdf relatorio.pdf
. To create the report you need to 'call' R and render the document with the package rmarkdown
.
In addition, you can create a dependency chain. In your case, the rendering of your document depends on whether the analysis data (.Rdata
) is up to date, which depends on whether the data .XLM
is up to date. In this way your makefile
would be more or less like this:
# exemplo de makefile
# aqui o pdf do relatorio depende do script .Rmd para cria-lo e também dos dados
# (se um desses dois não estão atualizados, ele vai rodar a receita)
relatorio.pdf: relatorio.Rmd DIPRConsolidado.RData
Rscript -e "rmarkdown::render('relatorio.Rmd')"
# como é uma cadeia, na etapa anterior o makefile testou se o .Rdata é atualizado,
# mas este depende do script .R e dos dados .XLM. Se um desses dois não estiver atualizado,
# ele ira rodar a receita abaixo
DIPRConsolidado.RData: leiturapastaDIPR.R dados.XLM
Rscript leiturapastaDIPR.R
You must save this script with the name makefile
and when you want to call it, you will write make
in the terminal.
makefile
it is not the simplest and most logical language in the first contact, but it is a fundamental tool to ensure the reproducibility of a project.