I have done some Google search already but am finding it surprisingly difficult to get a clear guide on how to access a arg when calling an rscript via the shell, or in my case via cron.
cronR job set up screen:
There is an arg I added called blah.
I found a reference to commandArgs() on stack overflow so wanted to see what that looks like when I call a script.
My script scribbles.R has a single line print(commandArgs())
When cronR runs the job is saves the results in scribbles.log which looks like this:
── [1mAttaching packages[22m ────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
[32m✓[39m [34mggplot2[39m 3.2.1 [32m✓[39m [34mpurrr [39m 0.3.3
[32m✓[39m [34mtibble [39m 2.1.3 [32m✓[39m [34mdplyr [39m 0.8.3
[32m✓[39m [34mtidyr [39m 1.0.0 [32m✓[39m [34mstringr[39m 1.4.0
[32m✓[39m [34mreadr [39m 1.3.1 [32m✓[39m [34mforcats[39m 0.4.0
── [1mConflicts[22m ───────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
[31mx[39m [34mdplyr[39m::[32mfilter()[39m masks [34mstats[39m::filter()
[31mx[39m [34mdplyr[39m::[32mlag()[39m masks [34mstats[39m::lag()
[1] "/usr/lib64/R/bin/exec/R"
[2] "--slave"
[3] "--no-restore"
[4] "--file=/home/rstudio-gavin/analysis/radhoc/revenue_model/scribbles.R"
[5] "--args"
[6] "blah"
I can see that blah is there as the 6th item. But if I use cronR elsewhere will the args always start at item 6? I could find no reference to that. What if I pass two args to the job, blah and 123... how can I access those within the script that is being called by cron?
Another example:
Here I added an additional arg fruit=apple
. I then ran scribbles.R with cronR where scribbles.R looks like:
print(commandArgs())
print(commandArgs['fruit'])
scribbles.log now looks like:
[1] "/usr/lib64/R/bin/exec/R"
[2] "--slave"
[3] "--no-restore"
[4] "--file=/home/rstudio-gavin/analysis/radhoc/revenue_model/scribbles.R"
[5] "--args"
[6] "blah"
[7] "fruit=apple"
Error in commandArgs["fruit"] :
object of type 'closure' is not subsettable
Calls: print
Execution halted
What is the 'right' way to access args passed during cron? How could I set a variable in the file being called to get the fruit?
# scribbles.R
first_arg <- ??? # should be 'blah'
fruit <- ??? # should be apple