Executing multiple source files in sequence using R - r

I have multiple source files in R which I want to execute daily in sequence using one cron job.
The code looks like -
Master_script.R
...
...
tryCatch({
source(abc.R)
source(pqr.R)
source(xyz.R)
}, error=function(e) {
print(e)})
...
...
In case there is an error in abc.R, an error is thrown and the rest of the Master_script.R is not executed. However, I want to execute the rest of the R files pqr.R and xyz.R incase abc.R fails.
In case there is an error in abc.R, is there a way to skip file abc.R, and continue executing the remaining source files?
FYI... I am not considering packing things up into a R package for now. Also, I don't want to create separate cron jobs.

I use tryCatch to avoid the Shinyapp crashing when encountering an error. See if this works. Good luck.
tryCatch(error = function(err) {return(error_value)},
source(abc.R)
)

Related

How to address an Rscript parse error: premature EOF?

Running my working R script in the windows command line (cmd) using Rscript results in a parsing error (premature EOF).
When I run the script in RStudio, it compiles and runs as expected.
I have read the Rscript page in R documentation, and I see that the problem must be due to spaces in my script itself, which probably make it into the cmd console somehow during parsing, but that's as far as I get.
Or should I have done something with the #! functionality mentioned therein?
I am trying to run it on cmd:
Rscript .\start_app.r
I am in the right working directory, and have set the folder containing Rscript in my environment.
The script is too long to share, and I am too inexperienced to give you the parts that make it break (otherwise I wouldn't be here), but it is full of functions, if statements and the like, that use curly brackets and are indented. I also often include empty rows (someteimes indented) for readability. It makes use of the shiny-package. An example could be:
islocal = nchar(Sys.getenv("LOCAL"))>1 | interactive()
if (islocal){
source('../../path/app/variables/styling.R')
} else {
source('./variables/styling.R')
}
As the example above, it also includes other R code called via source()
Can that somehow make it to the cmd line and be incorrectly compiled?
I get the following messages:
Error: parse error: premature EOF
(right here) ------^
Execution halted
Not enough memory resources are available to process this command.
(I guess the second message is an unrelated issue, but include it here just to be sure.)
As suggested in a comment, the solution was changing the encoding.
As mentionned by the requestor himself, Using "Save with Encoding -> ISO-8895-1 (System default)" solves the issue.

Write R console to file with source() despite errors

I want to capture the input and output of R into a separate text file. I found that the best way to do so is a combination of sink() and source() commands (How to save all console output to file in R?).
My problem is that in some data constellations, I get error messages. The exeution of the script via source() seems to stop right after an error occurs. However, I would like to run the script to the end and simply record everything, including the errors.
How can I adapt my approach to "skip" errors?
You have to use tryCatch() for every error.
For example,
tryCatch({
print('This line works well');
stop('that line contains an error')
}, error = function(e){})

Executing an R script in a way other than using source() in JRI

I am new to R and have been trying to use JRI. Through JRI, I have used the "eval()" function to get certain results. If I want to execute an R script, I have used "source()". However I am now in a situation where I need to execute a script on continuously incoming data. While I can still use "source()", I don't think that would be an optimal way from a performance perspectve.
What I did was to read the entire R script into memory and then try and use "eval()" passing the script - but this does not seem to work. I have ensured that the script has been correctly loaded into memory - that is because if I write this script (loaded into the memory) into a file and source this newly created file, it does produce the expected results.
Is there a way for me to not keep sourcing the same file over and over again and execute it from memory? Each of my data units are independent and have to be processed independently and as soon as they become available. I cannot wait to collect a bunch of data units and then pass them on to the R script.
I have searched a lot and not found anything related to this. Any pointers which could help me in this direction would be really helpful.
The way I handled this is as below -
I enclosed the entire script into a function.
I sourced the script file (which now contains the function) at the start of the execution of my program.
The place where I was sourcing the file, I am now just calling the function which contains the script itself i.e. -
REXP result = rengine.eval("retVal<-" + getFunctionName() + "()");
Here, getFunctionName() gives me the name of the name of the function which contains the script.
Since this is loaded into the memory and available, I do not have to source the script file every time I want to execute the script. Any arguments being passed to the script are done as env. variables.
This seems to be a workaround, but solves my problem. Any better options are welcome.

R - Execute a function in a file

There is a R file and there is a function getInfo() in it.
I want to run this function in that script file alone.
Is that possible ?
I know running the script command on the file and then running the function name will help.
But then it will also run the rest of stuffs from the script file which i dont want.
What is the best way out here
When you use source on a script file, all the code in that file will be loaded into the R session currently active. Any code that is not in a function, will be executed. I see two options:
Put the function in a seperate source file, or even a package if the number of functions grows.
Set a global R variable using option and retrieve its value in the file to be sourced using getOption, making the execution of the non-function code dependend on this option. This does require you to always set this option before sourcing the file, in any project you use it in.
I would go for option 1.

How to continuously restart/loop R script

I want an R script to continuously run and check for files in a folder and do something with those files.
The code simply checks for a file, then moves the file to somewhere else and renames it, deleting the old file (in reality it's a bit more elabore than this).
If I run the script it works fine, however I want R to automatically detect for the files. In other words, is there a way to have R run the script continuously so that I don't have to run the script if I put files in that folder?
In pure R you just need an infinite repeat loop...
repeat {
print('Checking files')
# Your code to do file manipulation
Sys.sleep(time=5) # to stop execution for 5 sec
}
However there may be better tools suitable to do this kind of file manipulation depending on your OS.
You can use the function tclTaskSchedule from the tcltk2 package to schedule a function or expression to run on a regular interval. You can have multiple such tasks scheduled and still work in the R session (just be careful not to modify something that the scheduled task could also modify or you can get unpredictable results).
Though an OS based solution that runs a given rscript may still be a better approach.

Resources