Run an .R file inside another code in R

2

I have a code that reads XML files that are in a corporate network folder, and generates a .RData file. I have other codes that generate different reports for the different sectors, based on RData . My problem is: these XML files are updated with different frequencies and I can not always tell when the last update was performed. I would like to know if there is a function that executes this other code (which generates RData ) whenever it generates a report, precisely to avoid a RData file based on outdated XML and to have to run the other code manually every time a new one requested. PS: I know I can insert code that reads the XML into the report code, but I do not do this to keep the report from being too long.

Edit: The script leiturapastaDIPR.R generates the file DIPRConsolidado.RData . The RData is a list with 5 elements and each one is a Data Frame. Then this RData is loaded into the RMarkDown markdown.RMD that generates the PDF of the report. If you need more information, just let me know!

    
asked by anonymous 11.10.2018 / 15:47

1 answer

0

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) of this target that are needed to create it, and finally the recipe which is the command used to create the target . Here's a pattern:

target: depencia1
    receita

In the example above, if dependência1 is not updated, makefile will run receita to then create target .

Example

Whereas the file name of the report you want to create is relatorio.Rmd which will generate the relatorio.pdf pdf. To create the report you need to 'call' R and render the document with the rmarkdown package.

In addition, you can create a depot chain. In your case, the rendering of your document depends on whether the parsing data (% with%) is updated, which depends on whether the .Rdata data is updated. This way your .XLM would look something 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 should save this script with the name makefile and when you want to call it, you will write makefile in the terminal.

make is not the most simple and logical language in the first contact, but it is a fundamental tool to ensure the reproducibility of a project.

    
11.10.2018 / 21:24