R - run system call with space in path on Mac - r

I am trying to invoke some system commands on R using system() calls. However, I am having the infamous problem with spaces in the directories.
For example, I am trying to use this directory:
odir <- '/Volumes/Macintosh HD 2/data/cmip5/historical/clt/models'
on this command:
system(paste('ls ', odir,sep=''))
and I receive the following error:
> system(paste('ls ', odir,sep=''))
ls: /Volumes/Macintosh: No such file or directory
ls: 2/data/cmip5/historical/clt/models: No such file or directory
ls: HD: No such file or directory
How can I overcome this?
Thanks!

Most command lines require that you escape spaces with a backslash. And when you make R strings, you need to escape backslashes with a backslash. Try
odir <- '/Volumes/Macintosh\\ HD\\ 2/data/cmip5/historical/clt/models'

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")

Can't move file after download and unzip

I'm trying to download a zip file from a source, unzip it and after move to another directory.
First the download:
if (!file.exists("inst/extdata/sp_resultados_universo")) {
tmp <- tempfile(fileext = ".zip")
download.file("ftp://ftp.ibge.gov.br/Censos/Censo_Demografico_2010/Resultados_do_Universo/Agregados_por_Setores_Censitarios/SP_Capital_20180416.zip", tmp, quiet = TRUE)
unzip(tmp, exdir = "inst/extdata/sp_resultados_universo", junkpaths=T)
unlink(tmp)
}
The file i want is on this directory inst/extdata/sp_resultados_universo/SP Capital/Base informa�oes setores2010 universo SP_Capital (codificação inválida)/CSV/, so when i try copy to inst/extdata/sp_resultados_universo/ i get an error
file.rename("inst/extdata/sp_resultados_universo/SP%20Capital/Base%20informa%87oes%20setores2010%20universo%20SP_Capital(condificação inválida)/CSV/Domicilio02_SP1.csv",
"inst/extdata/sp_resultados_universo/Domicilio02_SP1.csv")
Warning message:
In file.rename("inst/extdata/sp_resultados_universo/SP%20Capital/Base%20informa%87oes%20setores2010%20universo%20SP_Capital(condificação inválida)/CSV/Domicilio02_SP1.csv", :
it was not possible to rename file 'inst/extdata/sp_resultados_universo/SP%20Capital/Base%20informa%87oes%20setores2010%20universo%20SP_Capital(condificação inválida)/CSV/Domicilio02_SP1.csv'
for 'inst/extdata/sp_resultados_universo/Domicilio02_SP1.csv',
reason 'File or directory not found'
I'm translating the error message, so it could be inconsistent with english message.
I can change the directory name or move the file manually, but breaks the flow and it's not nice for reproducibility. How can i handle it inside R?
My system info:
Sys.info()
sysname
"Linux"
release
"4.9.0-6-amd64"
version
"#1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07)"
machine
"x86_64"
Many thanks in advance for any help.
when using R you can interact with the linux shell (or the windows cmd line) through a call to system() where you put the quoted command just as you would use in the shell,
for instance:
system("pwd") # prints current working directory
system("date") # prints
system("ls | grep .R") # prints a list of r scripts in the current working directory
system("mv file.txt /home/new_directory/file.txt") # moves your file to another directory

How do I open a document from 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

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=""))

Syntax error only when command is run from cron

This command:
/usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`date +%Y%m%d`".sql"
works fine from the command line but whenb cron runs it I get
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
The command is all on one line in the crontab as well so I'm confused by the line 0 and line 1 references...
Can anyone advise me as to what I am doing wrong there?
It's the obvious dumb question, but do you have the matching backquote in your crontab (crontab -l)?
The line one, line zero stuff isn't referring to the lines in the crontab, only to the 'lines' in the one-line script.
Updated:
Ah, I think I've got it. This is from crontab(5):
Percent-signs (%) in the command, unless escaped with backslash (\),
will be changed into newline characters, and all data after the
first % will be sent to the command as standard input.
So the percent characters in your date spec are being interpreted as newlines, which means the backquote isn't terminated before the newline, which would produce your error message.
So escape the percent characters. I'd forgotten that about crontab....
The easiest fix is probably to put the whole command in a shell script and just have that be run. So make a scriptName.sh file that contains the command you listed and have crontab call that script. That gets around all these odd problems.
Commands executed from cron do not have access to the environment variables from your login shell, including the path. So try the following (adding fully qualified path to date):
/usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`/usr/bin/date +%Y%m%d`".sql"
Of course, verify if your date command is located elsewhere by running which date then adjust the path if necessary.

Resources