Does {logrx} work with Rscript?
Closed this issue · 5 comments
I've seen that you can make use of {logrx} when using Rscript
like so Rscript -e "axecute('my_file.R')"
. However, what about the case where the R script itself takes command line arguments by making use of {optparse} or other packages? Let's say my script has a pop-filter
argument and usually I would call it like Rscript my_script.R --pop-filter "SAFFL == 'Y'"
. With the current design is there any way to leverage {logrx} in this case?
It seems possible. I'm not familiar with {optparse} but I made a script, test.R, and just had it print commandArgs(trailing = TRUE)
.
So I ran this:
Rscript -e "logrx::axecute('test.R')" --pop-filter "SAFFL == 'Y'"
And it returned:
[1] "--pop-filter" "SAFFL == 'Y'"
@thomas-neitmann does the above example from @nicholas-masel work?
I can confirm this works 🎉
# test.R
library(optparse)
parser <- OptionParser(add_help_option = FALSE) |>
add_option("--pop-filter", "store", "character") |>
add_option("--anl-filter", "store", "character")
args <- parse_args(parser, convert_hyphens_to_underscores = TRUE)
print(args)
Rscript -e "logrx::axecute('test.r')" --pop-filter SAF --anl-filter HGB
$pop_filter
[1] "SAF"
$anl_filter
[1] "HGB"
Thanks a lot for the suggestion @nicholas-masel. I hadn't even imagined that you could call Rscript
with the -e
option and be able to pass command line arguments to the script.
@thomas-neitmann No problem! Glad that worked for you. I learned something new too. That's the first I've seen optparse. I'm sure it will come in handy one day.