How do I open a document from R? - r

I want to open a file from within R.
I can launch the software (graphpad prism) with the following:
system2("C:/Program Files (x86)/GraphPad/Prism 7/prism.exe")
I expected this to open my prism file as if I were double clicking on it or running it from cmd, but it didn't:
system2("H:/Graphs/Shell/Templates/NASH4_Standard.pzfx")
I am receiving the message:
Warning message: running command
'H:/Graphs/Shell/Templates/NASH4_Standard.pzfx' had status 127
I see that this is not an error but just a warning. Am I unintentionally "shelling" the document in the background? How would I make sure it pops up as a window?
Status 127 was addressed here, but for launching the software, not opening the document with it.

In Windows environments, you need to call a command line interpreter like CMD prompt or PowerShell. Also, any file path that has spaces needs to be enclosed in double quotes above the quotes needed in R for string literals (the case for your .exe not specific file).
With system() send entire command in one string:
system('cmd /c "H:/Graphs/Shell/Templates/NASH4_Standard.pzfx"')
# POWER SHELL REQUIRES MORE QUOTE ESCAPING (ONLY ONE PAIR W/O SPACES)
system('powershell & """H:/Graphs/Shell/Templates/NASH4_Standard.pzfx"""')
With system2() use the args parameter:
# FILES
system2('cmd', args=c('/c', '"H:/Graphs/Shell/Templates/NASH4_Standard.pzfx"'))
system2('powershell', args=c(' & """H:/Graphs/Shell/Templates/NASH4_Standard.pzfx"""'))
# EXECUTABLES
system2('cmd', args=c('/c', '"C:/Program Files (x86)/GraphPad/Prism 7/prism.exe"'))
system2('powershell', args=c(' & """C:/Program Files (x86)/GraphPad/Prism 7/prism.exe"""'))

shell.exec("C:/Program Files (x86)/GraphPad/Prism 7/prism.exe")
does it work for you ?
ps. and shell.exec("MyWorkbook.xls") open file with default program

Related

Spaces in paths in batch mode R

I'm trying to get an R script to run from a batch file so it can be nice and clean for other users. Currently, you drag and drop a CSV file onto the batch file and it passes the file name to the R script for input.
When there's a space in the file path/name it works fine in RStudio but causes problems when I call it from the batch file. When I do that it tries to open the path before the space.
I've tried to reformat the file path from within R by using shortPathName(inputPath) and by replacing spaces with "\ " but it doesn't seem to work.
At the moment, the script is launched with
"%~dp0\R-3.6.0\bin\R.exe" CMD BATCH "--args %~1" "%~dp0\Script.R"
with the script containing
args <- commandArgs(TRUE)
inputPath <- args[1]
inputPath <- shortPathName(inputPath)
inputData <- read.csv(inputPath)
It runs fine from within RStudio but crashes when launched from the batch producing this error message in the output file:
Error in file(file, "rt") : cannot open the connection
Calls: read.csv -> read.table -> file
In addition: Warning message:
In file(file, "rt") :
cannot open file 'file path up to the space': No such file or directory
Execution halted
By no means a R expert, but I'd try
%~dp0\R-3.6.0\bin\R.exe" CMD BATCH "--args %~s1" "%~dp0\Script.R"
The %~s1 should supply the short filename as the argument.
After trying several formulations of the batch file and some debugging, I found that the batch file was passing the first part of the file before the space as the first argument.
After finding that the use of R in CMD BATCH mode is no longer advisable so switched to running using Rscript mode as
"%~dp0\R-3.6.0\bin\Rscript.exe" --vanilla "%~dp0\Script.R" "%~1"
This allowed for the argument to be passed to R with "", and hence with the space.
Since v3.5.1, R accepts file paths with spaces.

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.

command prompt, opening a csv file?

how do i open a file csv ? with the last command here vi
it open it the command prompt, but how do i open it on csv file normarlly
without open it on the command prompt?
[#dc1-ora rci]$ ls
backup diamond20130306.csv
diamond2012_04_30_dev.csv diamond_points_20120820.csv
diamond20120801.csv diamond_points_20120827.csv
diamond20120802.csv diamond_points_20120828.csv
diamond20120803.csv diamond_points_20121217.csv
diamond20120804.csv diamond_points_20130129.csv
diamond20120806.csv diamond_points_20130130.csv
diamond20120807.csv diamond_points_20130205.csv
diamond20120808.csv diamond_points_20130306.csv
diamond20120828.csv diamond_points_20130326.csv
diamond20120906.csv diamond_points_20130410.csv
diamond20121025.csv diamond_rental_20121219.csv
diamond20121029.csv diamond_rental_20121220.csv
diamond20121218.csv diamond_rental_20121221.csv
diamond20130128.csv ilx_final_lead_merge.csv
diamond20130129.csv test_diamond20120820.csv
diamond20130130.csv test_diamond20120827.csv
diamond20130131.csv test_diamond20120828.csv
diamond20130205.csv test.txt
diamond20130219.csv THH_RCT_RCI_EDM_FILE_01_28_13.csv
[#dc1-ora rci]$ vi test_diamond20120820.c
sv
( opens file here on the command prompt)
On windows/cmd you should do:
start notepad++ test_diamond20120820.csv
in your case:
start Excel.exe test_diamond20120820.csv
or for the default application associated with it use :
start test_diamond20120820.csv
(didn't test I am using linux maybe I will test it later if I reboot)
But I suspect you don't mean command prompt but a linux terminal (because of your tags):
you can use any of those:
gedit test_diamond20120820.csv &
nedit test_diamond20120820.csv &
etc depending on the programs you have installed
or for the default application associated with it use :
xdg-open test_diamond20120820.csv
if you are remotely logged in using ssh make sure you have X enabled

Executing console command in R

I would like to execute this DOS command under R:
iconv -f ISO-8859-1 -t UTF-8 FileName.md > FileNameNew.md
The above command creates new file after transforming from ISO to UTF.
I have tried execute this command however unsuccessfully with:
system(paste("iconv -f ISO-8859-1 -t UTF-8 FileName.md > FileNameNew.md", sep=""))
This gives me two types of errors:
Invalid argument
No such file or directory
I don't think the issue is the second since when I run the command under R it in fact executes the command as it re-reads the FileName.md, which means he found the file. I think it is just a issue with the > and hence formulation of the command in the system(paste("")) command.
When I rund this command directly under console it works.
The problem is (most likely) simply with where the R session is located. Check this by running getwd() in R and see if it is in the same place as the file. The paste part shouldn't be needed, as it is not really pasting anything (paste combines 2 strings together, while it is one string here).
Solve this by explicitly attaching input and output to those files.
If you would insist on using paste, you could use it for instance like this:
system(paste("iconv -f ISO-8859-1 -t UTF-8 ", getwd(), "/FileName.md > ",
getwd(), "/FileNameNew.md", sep=""))

R script from command line

I wanted to run this example script: http://mazamascience.com/WorkingWithData/?p=912 from Windows command line. So I opened the command line and typed Rscript tryCatch.R 1. However, I keep getting the error message Error: R not found. I did set the PATH environment variable as C:\Programme\R\R-3.0.1\bin. If I just type R.exe, it does start R, but it cannot find the packages that are to be loaded at start (e.g. package 'utils' in options<"defaultPackages"> was not found). I guess I have to set another path to the libraries somewhere, but I haven't got any idea where to do this.
UPDATE: After explicitly typing PATH C:\Programme\R\R-3.0.1\bin (rather than just adding this to the value of the environment variable PATH) it seems that R is found. However, a new problem occurs: In normalizePath<path.expand(path), winslash, mustWork>: path[2] = "C:/Programme/R/R-3.0.1/library": Access denied, the same than for the methods library. Then: Calls: .First ... library -> .getRequiredPackages2 -> library -> normalizePath Execution stopped. I'm using Windows 7 and I do have administrator rights.
Rscript is very handy (R CMD BATCH is the old way to ) specially under windows, But generally under I create a batch file to avoid all path's headache.
For example say launcher.bat:
#echo off
C:
PATH C:\Programme\R\R-3.0.1\bin;%path%
cd PATH_TO_YOUR_RSCRIPT
Rscript tryCatch.R 1
pause
And open a console(using cmd) , go where you have stored your launcher.bat and launch it. Or from the R cosnole using shell:
shell('path_to_launcher\launcher.bat')
I've found out that it was a language-specific problem on Windows 7, similar to what is described here: https://stat.ethz.ch/pipermail/r-help/2011-May/276932.html
After changing PATH to C:\Program Files\R\R-3.0.1\bin the script is properly executed from the command prompt.
Thanks to everyone who tried to help!
I ran into this problem under windows 7, apparently, when setting environment variables>user variables the path is not added into the PATH, so the user must add this path in system variables > PATH
at the end just add the path to your .EXE files and voila.

Resources