Run a file . R within another code in R

Asked

Viewed 1,104 times

4

I have a code that reads the XML files that are in an enterprise network folder, and generates a file .RData. I have other codes that generate different reports for different sectors, using as a basis the RData. My problem is: these XML files are updated at various frequencies and I can’t always tell when the last update was performed. I wonder if there is any function that runs this other code (which generates the RData) whenever you generate a report, precisely to avoid an outdated XML-based Rdata file and have to run the other code manually every time a new report is requested. PS: I know I can insert the code that reads XML into the report code, but I don’t do it to prevent the report from getting too long .

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 Rmarkdown markdown.RMD that generates the PDF of the report. If you need any more information!

  • 3

    You can use the function source(), but the script will be read every time, even if the XLM has not changed. Another more elegant solution would be to create a makefile in which it will run this R script only when the XLM changes. If you give more information about the names of the scripts, data and the order of execution, we can help in building the makefile.

  • The source() nor would itself be a problem, but from what I understood this makefile seems to be a more elegant solution!

  • That, source() is not a problem but an inefficient solution (in a matter of time wasted repeating analyses that have already been done). I will post here as an answer a makefile basic that will serve as an example for your problem.

1 answer

1

Makefile acts as a chain of rules where you must determine the target which is usually the object you want to create (in this case a pdf file), the dependencies of this target that are necessary to create it, and finally the recipe which is the command used to create the target. Here is a pattern:

target: depencia1
    receita

In the above example, if the dependência1 is not up to date, the makefile will rotate the receita to create the target.

Example

Whereas the file name of the report you want to create is relatorio.Rmd that 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 chain of services. In your case, the rendering of your document depends on whether the analysis data (.Rdata) are up to date, which depends on whether the data .XLM are updated. 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 him, you will write make in the terminal.

makefile is not the simplest language and logic at first contact, but is a key tool to ensure the reproducibility of a project.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.