How to Specify Command Line Parameters to R-Script in Rstudio

Is it possible to specify command line parameters to R-script in RStudio?

I know this question is old and the link below is old, but it answers the question. No, it's not possible (or wasn't as of Jan 29, 2012) to access command line arguments from RStudio.

Link
https://support.rstudio.com/hc/communities/public/questions/200659066-Accessing-command-line-options-in-RStudio?locale=en-us

How to pass command line parameters in R-Studio?

I've had a similar problem, and the way I solved it was to create a variable in the shell call as follows:

R -e "file <- 'input_file.txt'; param1 <- 1; param2 <- 2; Sweave('$PRGDIR/Test.Rnw')"

within the .Rnw script do a check to search for the variables using the exists function:

if(exists("file")){
# do stuff here
} else{
stop('I Need a file!')
}

How to pass command line argument when using source()

There really is no notion of a "command line" for the R parser. It executes a complete function call if it finds one in the "do_something.r" file before moving on to any expression that might follow the correct separator, a semi-colon. The parser will not pass the trailing expression to the evaluator until the sourced code is completed, so will not "see" any expression that follows. You have two choices:

To do this from the RGui console you need to do something like:

 val <- 10; source("do_something.r")  # set value first, then `source`

To use a "real" command line (not from the RGui), try this (assuming that the code in do_something.r will access a variable named val):

Rscript do_something.r -e 'val <- 10'

See ?Rscript for more details and examples.

How to get a user input in command prompt and pass it to R

From the documentation of readline():

This can only be used in an interactive session. [...] In non-interactive use the result is as if the response was RETURN and the value is "".

For non-interactive use - when calling R from the command line - I think you've got two options:

  1. Use readLines(con = "stdin", n = 1) to read user input from the terminal.
  2. Use commandArgs(trailingOnly = TRUE) to supply the input as an argument from the command line when calling the script instead.

Under is more information.

1. Using readLines()

readLines() looks very similar to readline() which you're using, but is meant to read files line by line. If we instead of a file points it to the standard input (con = "stdin") it will read user input from the terminal. We set n = 1 so that it stops reading from the command line when you press Enter (that is, it only read one line).

Example

Use readLines() in a R-script:

# some-r-file.R

# This is our prompt, since readLines doesn't provide one
cat("Please write something: ")
args <- readLines(con = "stdin", n = 1)

writeLines(args[[1]], "output.txt")

Call the script:

Rscript.exe "some-r-file.R"

It will now ask you for your input. Here is a screen capture from PowerShell, where I supplied "Any text!".

Print screen from PowerShell

Then the output.txt will contain:

Any text!


2. UsingcommandArgs()

When calling an Rscript.exe from the terminal, you can add extra arguments. With commandArgs() you can capture these arguments and use them in your code.

Example:

Use commandArgs() in a R-script:

# some-r-file.R
args <- commandArgs(trailingOnly = TRUE)

writeLines(args[[1]], "output.txt")

Call the script:

Rscript.exe "some-r-file.R" "Any text!"

Then the output.txt will contain:

Any text!

Run R script from command line

If you want the output to print to the terminal it is best to use Rscript

Rscript a.R

Note that when using R CMD BATCH a.R that instead of redirecting output to standard out and displaying on the terminal a new file called a.Rout will be created.

R CMD BATCH a.R
# Check the output
cat a.Rout

One other thing to note about using Rscript is that it doesn't load the methods package by default which can cause confusion. So if you're relying on anything that methods provides you'll want to load it explicitly in your script.

If you really want to use the ./a.R way of calling the script you could add an appropriate #! to the top of the script

#!/usr/bin/env Rscript
sayHello <- function(){
print('hello')
}

sayHello()

I will also note that if you're running on a *unix system there is the useful littler package which provides easy command line piping to R. It may be necessary to use littler to run shiny apps via a script? Further details can be found in this question.

How can I pass an array as argument to an R script command line run?

First, i am not sure if you should use set to declare any variable in terminal, but you can see more of this on man set page.

There are some ways i think you can try to pass an array to R script, it mainly depends on how you declare the my_array in terminal.


1 - "my_array" can be a string in bash:

$ my_array="c(0,1,2,3,4)"
$ Rscript my_script ${my_array}

And use the eval(parse(text=())) in R script to transform the argument as a vector to R environment.

args <- commandArgs(trailingOnly=TRUE) 
print(args)
# c(0,1,2,3,4,5,6,7,8,9)

args <- eval(parse(text=args[1]))
print(args)
# [1] 0 1 2 3 4 5 6 7 8 9

2 - "my_array" can be an array in bash:

$ my_array=(0,1,2,3,4)
$ Rscript my_script ${my_array[*]}

and in R the args is already your array, but of type character:

args <- commandArgs(trailingOnly=TRUE) 
print(args)
# [1] "0" "1" "2" "3" "4" "5"

Passing arguments to R script in command line (shell/bash): what to do when column names contain tilde (~)

You are successfully passing an argument that specifies a column name containing tilde. However, read.csv has "fixed" the column names so it doesn't actually contain a tilde.

read.csv is silently converting the column name to age.blah.value. Use check.names = FALSE to make it age-blah~value.

data_raw <- read.csv(file = path_to_input, check.names = FALSE)


Related Topics



Leave a reply



Submit