How can I access an arg variable from cronR or the command line more generally?

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

I haven't used cron in some time and not from RStudio. I can explain the error, however.

commandArgs() is a function. As such it doesn't understand subsetting. Depending on what blah does, the object that is returned may well be subsettable. I'm pretty sure that blah() -> someobject["fruit"] will yield your result, but I haven't tried setting it up.

Looks like it's just strings which is all I need. I was able to get what I need with:

library(tidyverse)
x <- commandArgs(trailingOnly = T)

b <- x[1]
f <- x[2]
v <- x[3]

print(paste0("here is b: ", b))
print(paste0("here is f: ", f))
print(paste0("here is v: ", v))

Where in cronR I added blah apple cabbage. I think I'm good now! The Google results for this one item were unusually tricky. Kept returning edge cases such as passing args while sourcing a script or other niche.

1 Like

Great! Please mark the solution for the benefit of those to follow.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.