How to wait for user input in R-script ran from Windows CMD - r

I'm new to R and I'm stuck in a very simple task.
I want to run an R script from console, but I want the script to be able to read user inputs.
This is how I'm reading from the script:
library = readLines(n = 1L)
if(library == "1")
{
library = "GUDHI"
}
And this is how I'm running my script from R-Portable with a .bat:
#echo on
cd..
cd..
cd..
cd..
cd..
PATH C:\Users\MyUser\Desktop\App\RFolder\R-Portable\App\R-Portable\bin;%path%
cd C:\Users\MyUser\Desktop\App\RFolder
Rscript Phom.R 1
pause
When I run this .bat it throws an error (Argument is of length zero):
As if the console didn't wait for user input.
The problem is not the .bat code. If I remove the readLines functions from my script and hardcode the input, it works perfectly. I also tried the readline function with no success.
Thanks.

Solution for Interactive R script from Windows CMD:
cat("Prompt Message: ")
library = readLines(con = "stdin", 1)
I'm not sure if the prompt MUST end with ": ", but I had trouble when I removed that piece of string.
This worked for me, I hope this helps somebody.

Related

How to release R's prompt when using 'system'?

I am writing an R code on a Linux system using RStudio. At some point in the code, I need to use a system call to a command that will download a few thousand of files from the lines of a text file:
down.command <- paste0("parallel --gnu -a links.txt wget")
system(down.command)
However, this command takes a little while to run (a couple of hours), and the R prompt stays locked while the command runs. I would like to keep using R while the command runs on the background.
I tried to use nohup like this:
down.command <- paste0("nohup parallel --gnu -a links.txt wget > ~/down.log 2>&1")
system(down.command)
but the R prompt still gets "locked" waiting for the end of the command.
Is there any way to circumvent this? Is there a way to submit system commands from R and keep them running on the background?
Using ‘processx’, here’s how to create a new process that redirects both stdout and stderr to the same file:
args = c('--gnu', '-a', 'links.txt', 'wget')
p = processx::process$new('parallel', args, stdout = '~/down.log', stderr = '2>&1')
This launches the process and resumes the execution of the R script. You can then interact with the running process via the p name. Notably you can signal to it, you can query its status (e.g. is_alive()), and you can synchronously wait for its completion (optionally with a timeout after which to kill it):
p$wait()
result = p$get_exit_status()
Based on the comment by #KonradRudolph, I became aware of the processx R package that very smartly deals with system process submissions from within R.
All I had to do was:
library(processx)
down.command <- c("parallel","--gnu", "-a", "links.txt", "wget", ">", "~/down.log", "2>&1")
processx::process$new("nohup", down.comm, cleanup=FALSE)
As simple as that, and very effective.

Correct way to use here package with cronR scheduling

I've been using the here package to make my projects more portable. It works great apart from when I use cronR to schedule some of my scripts. When I run my_script.R from Rstudio I get a message from library(here):
here() starts at /home/pd/projects/my_proj
When I set script.R to run using cronR I get a different message:
here() starts at /home/pd
Which is where my_schedule.cron is stored. Ideally I want to keep my_schedule.cron where it is. I can see from the logs that my_script.R runs fine apart from when it comes to saving data because the path used by here() is incorrect. Is there anyway to get the here function to detect the project dir when my_script.R is run from cronR or the terminal?
You can modify the command cmd that is usually created with cron_rscript() by adding cd to your project folder followed by the usual part:
cmd <- "cd /home/pd/projects/my_proj && /usr/lib/R/bin/Rscript ./my_script.R >> ./my_script.log 2>&1"
cron_add(command = cmd, frequency = 'daily', at = '18:00')
If the first line of your #rstats script is wd <- here(), I will come
into your lab and SET YOUR COMPUTER ON FIRE.
Learn how to use environment variables
wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Or use the 'Additional arguments to Rscript' element in the cronR user interface to pass something extra to the Rscript and fetch it with commandArgs().
If you don't use the cronR interface but cron_rscript, use cronR::cron_rscript(..., rscript_args = "/home/pd/projects/my_proj")
args <- commandArgs(trailingOnly = TRUE)
if(length(args) > 0){
wd <- args[1]
}

R system functions always returns error 127

I need to execute an external tool from R and process errors (if any) occurred in that tool.
I know 3 functions to do something familiar with my task:
shell, system and system2.
Trying to test those, I see that command
shell("notepad")
opens notepad. As far as I know shell doesn't allow to check errors (there's no interface to look into stderr).
When I call
system("notepad")
or
system2("notepad")
R freezes trying to make those commands.
Calling
system("start notepad")
or
system2("start notepad")
returns warning
Warning message:
running command '"start notepad"' had status 127
Adapting #DavidTseng's answer (sorry for not having enough reputation to upvote it)...
system("cmd.exe", input = "notepad")
worked for me in Windows.
As I mentioned in my comments, the R documentation reveals that in Windows the system() function does not launch a separate shell (if needed). This is why command line commands run with system(), but Notepad, which needs a separate window, does not run:
From the documentation for system():
The most important difference is that on a Unix-alike system launches a shell which then runs command. On Windows the command is run directly – use shell for an interface which runs command via a shell (by default the Windows shell cmd.exe, which has many differences from a POSIX shell).
system("bash -l", input = "notepad")
I'm not sure if there's been an update to R that allows this since the question was asked nearly four years ago, but system("\"C:\path\to\exe.exe\" args", intern = T) works for me and WILL bring up a separate child window and works on Windows 10 + R 3.6 + RStudio.
Not using the 'intern = T' was giving me a return code of 127 and did not run the process.
I had the same issue. there is an additional step in the installation process which i did not do.
refer to to the url
https://cran.r-project.org/bin/windows/Rtools/
Look for "Putting Rtools on the PATH"
writeLines('PATH="${RTOOLS40_HOME}\usr\bin;${PATH}"', con = "~/.Renviron")
for windows users
wrong: system(path("c:", "program files", "r", "anysoft.EXE"))
but works : system(path("c:", shQuote("program files"), "r", "anysoft.EXE"))
You guys are making it so complicated. I solved this problem by referring to this answer. The problem is with the PATH. type Sys.which('') in R, and you will see nothing. So you have to set the path in CMD, and then use Sys.setenv(PATH = '') in R to get this work.

Using python to execute .R script

After seven hours of googling and rereading through somewhat similar questions, and then lots of trial and error, I'm now comfortable asking for some guidance.
To simplify my actual task, I created a very basic R script (named test_script):
x <- c(1,2,3,4,5)
avg <- mean(x)
write.csv(avg, file = "output.csv")
This works as expected.
I'm new to python and I'm just trying to figure out how to execute the R script so that the same .csv file is created.
Notable results come from:
subprocess.call(["C:/Program Files/R/R-2.15.2/bin/R", 'C:/Users/matt/Desktop/test_script.R'])
This opens a cmd window with the typical R start-up verbiage, except there is a message which reads, "ARGUMENT 'C:/Users/matt/Desktop/test_script.R' __ ignored __"
And:
subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript', 'C:/Users/matt/Desktop/test_script.r'])
This flashes a cmd window and returns a 0, but no .csv file is created.
Otherwise, I've tried every suggestion I could identify on this site or any other. Any insight will be greatly appreciated. Thanks in advance for your time and efforts.
Running R --help at the command prompt prints:
Usage: R [options] [< infile] [> outfile]
or: R CMD command [arguments]
Start R, a system for statistical computation and graphics, with the
specified options, or invoke an R tool via the 'R CMD' interface.
Options:
-h, --help Print short help message and exit
--version Print version info and exit
...
-f FILE, --file=FILE Take input from 'FILE'
-e EXPR Execute 'EXPR' and exit
FILE may contain spaces but not shell metacharacers.
Commands:
BATCH Run R in batch mode
COMPILE Compile files for use with R
...
Try
call(["C:/Program Files/R/R-2.15.2/bin/R", '-f', 'C:/Users/matt/Desktop/test_script.R'])
There are also some other command-line arguments you can pass to R that may be helpful. Run R --help to see the full list.
It might be too late, but hope it helps for others:
Just add --vanilla in the call list.
subprocess.call(['C:/Program Files/R/R-2.15.2/bin/Rscript', '--vanilla', 'C:/Users/matt/Desktop/test_script.r'])

R and System calls

I have used R in the past to do very basic calls to the commmand line. The example can be found here.
This time around, I am looking to mimic this code which runs successfully from the command line in Windows:
> cd C:\Documents and Settings\BTIBERT\My Documents\My Dropbox\Eclipse\Projects\R\MLB\retrosheet\rawdata
> bgame -y 2010 2010bos.eva >2010bos.txt
This is the code I am trying to run inside of R. I have already set the working directory inside of R.
dir <- paste("cd", getwd(), sep=" ")
system(dir)
system("bgame -y 2010 2010bos.eva >2010bos.txt")
I am sure this is user error, but what am I doing wrong? It appears to work initially, but returns the following error. I very well could be doing something wrong, but I believe I am using the same commands.
Expanded game descriptor, version 109(185) of 05/08/2008.
Type 'bgame -h' for help.
Copyright (c) 2001 by DiamondWare.
[Processing file 2010bos.eva.]
>2010bos.txt: can't open.
Warning message:
running command 'bgame -y 2010 2010bos.eva >2010bos.txt' had status 2
Any help you can provide will be appreciated.
You need to issue all commands in one system() call:
system(paste("cd",getwd() "&& bgame -y 2010 2010bos.eva >2010bos.txt",sep=" "))
You should already be in your working directory, so I'm not sure the cd getwd() is necessary. And you may need quotes around your path because it contains spaces. The error may be resolved by putting spaces around >.
If I were in your shoes, I would try this:
system("bgame -y 2010 2010bos.eva > 2010bos.txt")
UPDATE:
And you should probably heed this advice in the "Differences between Unix and Windows" section of ?system that says you should use shell:
• The most important difference is that on a Unix-alike
‘system’ launches a shell which then runs ‘command’. On
Windows the command is run directly - use ‘shell’ for an
interface which runs ‘command’ _via_ a shell (by default the
Windows shell ‘cmd.exe’, which has many differences from the
POSIX shell).
This means that it cannot be assumed that redirection or
piping will work in ‘system’ (redirection sometimes does, but
we have seen cases where it stopped working after a Windows
security patch), and ‘system2’ (or ‘shell’) must be used on
Windows.
Has no-one else found that system("dir", intern = T) for example doesn't work, but that you need system("cmd.exe /c dir", intern = T)? Only the latter works for me. I found this at the discussion site here (William Dunlap's post, about a third of the way down).
Also, it doesn't work with the "cd" command, but you can use the setwd() function within R and then the command will be executed within that directory.
I created the following functions for convenience, for executing programmes and running commands:
#the subject is an input file that a programme might require
execute <- function(programme, subject.spec = "", intern = FALSE, wait = FALSE){
if(!identical(subject.spec, "")){subject.spec <- paste0(" ", subject.spec)} #put space before the subject if it exists
system(paste0("cmd.exe /c ", programme, subject.spec), intern = intern, wait = wait)
}
command <- function(command, intern = TRUE, wait = FALSE){
system(paste("cmd.exe /c", command), intern = T, wait = wait)
}
Does it break your code when you get error 1 or does execution continue?
Whenever executing system commands through another language it is useful to print the system call before you call it to see exactly what is happening, pull up the shell you are intending to use and check for the same error. As the command is executed correctly this could be a hickup in bgame or R.
If you look at http://astrostatistics.psu.edu/datasets/R/html/base/html/shell.html you can see the variable flag passed to the system call."flag the switch to run a command under the shell. If the shell is bash or tcsh the default is changed to "-c"."
Also "the shell to be used can be changed by setting the configure variable R_SHELL to a suitable value (a full path to a shell, e.g. /usr/local/bin/bash)."

Resources