Setting Directory of command prompt - r

I would like to set the working directory of the command prompt using the function system2 in R and I got the following error:
system2("cmd.exe", args = paste("cd", "Users/AKatherine/Downloads", sep = " "))
'Akatherine' is not recognized as an internal or external command,
operable program or batch file.
Also, I tried to run this
system2("cmd.exe", args = "java -mx150m -cp "*;" edu.stanford.nlp.parser.lexparser.LexicalizedParser -outputFormat "penn,typedDependencies" -outputFormatOptions "basicDependencies" edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ./Test/input.txt")
and got the following error:
Error: unexpected ';' in "system2("cmd.exe", args = "java -mx150m -cp "*;"
Do anyone know why? Can anyone help me with this?

For the second part of my question, I tried using the shell function instead of the system2 function and I got the same output that I observe when I type in the same command in the command prompt. I used:
shell(cmd = "java -mx150m -cp \"*;\" edu.stanford.nlp.parser.lexparser.LexicalizedParser -outputFormat \"penn,typedDependencies\" -outputFormatOptions \"basicDependencies\" edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ./Test/input.txt", shell = "cmd.exe")
instead of
system2("cmd.exe", args = "java -mx150m -cp \"*;\" edu.stanford.nlp.parser.lexparser.LexicalizedParser -outputFormat \"penn,typedDependencies\" -outputFormatOptions \"basicDependencies\" edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ./Test/input.txt")

Related

How can I activate a '.js' script from R?

I have a '.js' script that I usually activate from the terminal using the command node script.js. As this is part of a process where I first do some data analysis in R, I want to avoid the manual step of opening the terminal and typing the command by simply having R do it for me. My goal would be something like this:
...R analysis
write.csv(df, "data.csv")
system('node script.js')
However, when I use that specific code, I get the error:
sh: 1: node: not found
Warning message:
In system("node script.js") : error in running command
Of course, the same command runs without problem if I type it directly on the terminal.
About my Software
I am using:
Linux computer with the PopOS!
RStudio 2021.09.1+372 "Ghost Orchid"
R version 4.0.4.
The error message node: not found indicates that it couldn't find the program node. It's likely in PATH in your terminal's shell, but not in system()'s shell (sh).
In your terminal, locate node by executing which node. This will show the full path to the executable. Use that full path in system() instead.
Alternatively, run echo $PATH in your terminal, and run system('echo $PATH') or Sys.getenv('PATH') in R. Add any missing directories to R's path with Sys.setenv(PATH = <your new path string>)
Note that system2() is recommended over system() nowadays - but for reasons unimportant for your case. See ?system and ?system2 for a comparison.
Examples
Terminal
$ which node
/usr/bin/node
R
system('/usr/bin/node script.js')
# alternatively:
system2('/usr/bin/node', c('script.js'))
or adapt your PATH permanently:
Terminal
% echo $PATH
/usr/local/bin:/home/caspar/programs:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
R
> Sys.getenv('PATH')
[1] "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Applications/RStudio.app/Contents/MacOS/postback"
> Sys.setenv(PATH = "/home/caspar/programs:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Applications/RStudio.app/Contents/MacOS/postback")

Single line code to run R code from Windows command line

I Want a single line code to run a shiny app from windows command line without opening the R application.
If I open the R from cmd and run the code it works.
But it gives error for the following single line code.
R -e 'shiny::runApp(system.file("examples/01_hello", package="shiny"), launch.browser = T)'
............
............
> 'shiny::runApp(system.file(examples/01_hello,
+
+ Error: unexpected end of input
Execution halted
Please help. Thanks..
The following code is working:
RScript -e "shiny::runApp(system.file('examples/01_hello', package='shiny'), launch.browser = T)"
I have made two changes:
Replace R by RScript
Interchange single and double quotes
You can try in cmd something like this (for example)
> "C:\Program Files\R\R-3.5.0\bin\i386\Rscript.exe" ".\app.R"
Inside app.R i have, so it is runs shiny app
shinyApp(
ui = ui,
server = server,
options = list(launch.browser = T))
You can try, in Windows command prompt, single quote doesn't work for this case.
R -e "shiny::runApp(system.file("'examples/01_hello'", package="'shiny'"), launch.browser = T)"

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

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.

system() command in R - Error running exiftool

I am trying to run exiftool through R to get metadata from photos using the system() command. When I run this on a mac, it works fine, but from windows I am not linking up with the cmd.exe properly and getting the following error from this code:
exif_datetime <- function(path) {
exif_cmd <- 'exiftool.pl -T -r -DateTimeOriginal '
cmd <- paste(exif_cmd, "'", path, "'" ,sep='')
exif_time <- system(cmd, intern = TRUE)
exif_time
}
photo_time <- exif_datetime('C:/Users/photo.jpg')
photo_time
Error in system(cmd, intern = TRUE) :
'CreateProcess' failed to run 'C:\Windows\exiftool.pl -T -r -DateTimeOriginal 'C:/Users/photo.jpg''
When I run the exiftool command from cmd.exe in Windows, I get the proper result. My exiftool.pl is in the C:Windows folder on my computer. Is there something regarding the PATH that I am missing? Also, I remember something about windows needing a shell, but I have not figured out if that is what I need in my case nor how to create one properly.
Thanks for all your suggestions. I found a solution that works for me, involving the shell() command. I thought that it had to be incorporated with the system() command somehow, but it appears that it will work on its own.
exif_datetime <- function(path) {
exif_call <- 'exiftool.pl'
exif_cmd<-' -r -T -DateTimeOriginal '
exif_timestamp <- shell(paste(exif_call, exif_cmd, path), intern=T)
exif_timestamp
}
photo_time <- exif_datetime('C:/Users/photo.jpg')
photo_time
[1] "2016:02:14 11:50:29"

pass character "&" by args to R

the following R code is to be execute by command prompt (windows)
# Collect arguments
args <- commandArgs(TRUE)
archivo <- as.character(args[1])
cat(archivo)
like this
C:\Users\Owner\Desktop>Rscript prueba.r "hola&chao"
problem is that command prompt respond
hola
'chao' is not recognized as an internal or external command,
operable program or batch file.
C:\Users\Owner\Desktop>
I need to R accepts the "&" as a character and print it all together in the cat()
What should I do?
Thanks
This isn't an R problem, but a shell problem. You actually executed
Rscript prueba.r hola&chao
instead of
Rscript prueba.r "hola&chao"
In the Windows command shell,
prog1 & prog2
executes both programs in sequence, which is why you see the output of
Rscript prueba.r hola
followed by the output of
chao
The other possibility is that Rscript is a buggy batch file.

Resources