run system() command from R script - r

I have R, R Studio and R-tools installed on Windows 7.
I am trying to: cd into a directory and execute a command, this is what I am getting from R, it returns M, what does that mean ?
> system(paste("cmd.exe", input = "cd c:/Users/X123/Desktop/FASTLOAD/", "&& fastload < load_journey.fl > output.txt", sep=" "))
M
>
what am I doing wrong ? I never see the command executed, thanks!
I changed it from system() to shell() and it works now, but get a bunch of warning messages, is that ok or should I do something for them to go away ?
> shell(paste("cd c:/Users/X123/Desktop/FASTLOAD/ ", "&& fastload < load_journey.fl > output.txt", sep=" "))
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c cd c:/Users/X123/Desktop/FASTLOAD/ && fastload < load_journey.fl > output.txt' had status 4
2: In shell(paste("cd c:/Users/X123/Desktop/FASTLOAD/ ", "&& fastload < load_journey.fl > output.txt", :
'cd c:/Users/X123/Desktop/FASTLOAD/ && fastload < load_journey.fl > output.txt' execution failed with error code 4
>

Related

How to readRDS from stdin?

I try to use the following command to read an RDS file. But it doesn't work. My OS is Mac OS X.
$ lr -e "readRDS(file('stdin'))" < /tmp/x.rds
Error in readRDS(file("stdin")) : unknown input format
$ lr -p -e "readRDS('/dev/stdin')" < /tmp/x.rds
Error in readRDS("/dev/stdin") : error reading from connection
But this works.
$ lr -p -e "readRDS('/tmp/x.rds')"
x y
1 1 11
2 2 12
3 3 13
Does anybody know how to readRDS from stdin? Thanks.
It works for me (on linux, using littler 0.3.9 on R-devel) using '/dev/stdin' instead of 'stdin'; so try:
lr -p -e "print(readRDS('/dev/stdin'))" < /tmp/x.rds

How to call exe program and input parameters using R?

I want to call .exe program (spi_sl_6.exe) using a command of R (system), however I can't input parameters to the program using "system". The followwing is my command and parameters:system("D:\\working\spi_sl_6.exe")
I am searching for a long time on net. But no use. Please help or try to give some ideas how to achieve this. Thanks in advance.
This is using the Standardized Precipitation Index software from
http://drought.unl.edu/MonitoringTools/DownloadableSPIProgram.aspx.
This seems to give a working solution using Windows (but not without warnings!)
Fisrt download the software and example files
# Create directory to download software
mydir <- "C:\\Users\\david\\spi"
dir.create(mydir)
url <- "http://drought.unl.edu/archive/Programs/SPI"
download.file(file.path(url, "spi_sl_6.exe"), file.path(mydir, "spi_sl_6.exe"), mode="wb")
# Download example files
download.file(file.path(url, "SPI_samplefiles.zip"), file.path(mydir, "SPI_samplefiles.zip"))
# extract one example file, and write out
temp <- unzip(file.path(mydir, "SPI_samplefiles.zip"), "wymo.cor")
dat <- read.table(temp)
# Use this file as an example input
write.table(dat, file.path(mydir,"wymo.cor"), col.names = FALSE, row.names = FALSE)
From page 3 of the help file basic-spi-program-information.pdf at the above link the command line code should be of the form spi 3 6 12 <infile.dat >outfile.dat, however,
neither of the following worked (just from command line not in R), and various iterations of how to pass parameters.
C:\Users\david\spi\spi_sl_6 3 <C:\Users\david\spi\wymo.cor >C:\Users\david\spi\out.dat
cd C:\Users\david\spi && spi_sl_6 3 <wymo.cor >out.dat
However, using the accepted answer from Running .exe file with multiple parameters in c#
seems to work. That is again from the command line
cd C:\Users\david\spi && (echo 2 && echo 3 && echo 6 && echo wymo.cor && echo out1.dat) | spi_sl_6
So to run this in R you can wrap this in a shell (you will need to change the path to where you have saved the exe)
shell("cd C:\\Users\\david\\spi && (echo 2 && echo 3 && echo 6 && echo wymo.cor && echo out2.dat) | spi_sl_6", intern=TRUE)
out1.dat and out2.dat should be the same.
This throws warning messages, I think from the echo (in R but not from command line) but the output file is produced.
Suppose you can automate all the echo calls sligtly, so all you need to do is input the time parameters.
timez <- c(2, 3, 6)
stime <- paste("echo", timez, collapse =" && ")
infile <- "wymo.cor"
outfile <- "out3.dat"
spiCall <- paste("cd", mydir, "&& (" , stime, "&& echo", infile, "&& echo", outfile, " ) | spi_sl_6")
shell(spiCall)
You can construct the command using sprintf :
cmd_name <- "D:\\working\spi_sl_6.exe"
param1 <- "a"
param2 <- "b"
system2(sprintf("%s %s %s",cmd_name,param1,param2))
Or using system2( I prefer this option):
system2(cmd_name, args = c(param1,param2))

running map2stan function error 127

I installed rstan and when I call map2stan I get the error below:
> library(rstan)
> packageVersion("rstan", lib.loc = "C://TOOLS//R-3.4.0patched//library" )
[1] ‘2.16.2’
here's the call to map2stan
library(rethinking)
data(rugged)
d <- rugged
d$log_gdp <- log(d$rgdppc_2000)
dd <- d[ complete.cases(d$rgdppc_2000) , ]
dd.trim <- dd[ , c("log_gdp","rugged","cont_africa") ]
str(dd.trim)
m8.1stan <- map2stan(
alist(
log_gdp ~ dnorm( mu , sigma ) ,
mu <- a + bR*rugged + bA*cont_africa + bAR*rugged*cont_africa ,
a ~ dnorm(0,100),
bR ~ dnorm(0,10),
bA ~ dnorm(0,10),
bAR ~ dnorm(0,10),
sigma ~ dcauchy(0,2)
) ,
data=dd.trim )
here's the error
Warning message:
running command 'make -f "C:/TOOLS/R-34~1.0PA/etc/x64/Makeconf" -f "C:/TOOLS/R-34~1.0PA/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="file29cc72047709.dll" WIN=64 TCLBIN=64 OBJECTS="file29cc72047709.o"' had status 127
ERROR(s) during compilation: source code errors or compiler configuration errors!
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! Warning message:
running command 'make -f "C:/TOOLS/R-34~1.0PA/etc/x64/Makeconf" -f "C:/TOOLS/R-34~1.0PA/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="file344416f9304.dll" WIN=64 TCLBIN=64 OBJECTS="file344416f9304.o"' had status 127
In addition: Warning message:
running command 'C:/TOOLS/R-34~1.0PA/bin/x64/R CMD SHLIB file344416f9304.cpp 2> file344416f9304.cpp.err.txt' had status 1
Error in map2stan(alist(log_gdp ~ dnorm(mu, sigma), mu <- a + bR * rugged + :
Something went wrong, when calling Stan. Check any debug messages for clues, detective.
Compilation ERROR, function(s)/method(s) not created! Warning message:
running command 'make -f "C:/TOOLS/R-34~1.0PA/etc/x64/Makeconf" -f "C:/TOOLS/R-34~1.0PA/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="file344416f9304.dll" WIN=64 TCLBIN=64 OBJECTS="file344416f9304.o"' had status 127
>
Hmm, I installed rethinking library via http://xcelab.net/rm/software/ (it was not on CRAN) and ran the same code and didn't get any errors in my case. What's the Rtools, R, and OS type/version? Have you followed all installation instructions, e.g. if your OS is Windows, on https://github.com/stan-dev/rstan/wiki/Installing-RStan-on-Windows? Can you check that their eight school example in https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started runs fine?

r system doesn't work when trying 7zip

Hi I'm trying to use R to control unzipping a file.
I've added 7z into PATH, and did
7z e hat.2015-09-26T01-10-02.gz
and it worked.
Now in R, i'm in the same directory, and i tried
> command1 = paste0('7z e ', drop.file)
> command1
[1] "7z e hat.2015-09-26T01-10-02.gz"
> system(command1, intern=T)
Error in system(command1, intern = T) : '7z' not found
> system2(command1)
Warning message:
running command '"7z e hat.2015-09-26T01-10-02.gz"' had status 127
> shell(command1)
'7z' is not recognized as an internal or external command,
operable program or batch file.
Warning messages:
1: running command 'C:\Windows\system32\cmd.exe /c 7z e hat.2015-09-26T01-10-02.gz' had status 1
2: In shell(command1) :
'7z e hat.2015-09-26T01-10-02.gz' execution failed with error code 1
It's little bit raw, but try this out (:
Only Windows. It's uses CMD
ZiparEm7zip = function(sQualPasta)
{
sWDTava = getwd()
setwd(dirname(sQualPasta))
sQuem = gsub("[/]", "\\\\", sQualPasta)
dirname(sQuem)
NomeArquivo = paste0("eufaco7zip",".bat")
sNomePasta7zip = basename(sQuem)
sArquivoSaida = basename(sQuem)
sQualPasta7zip = sQuem
if(dir.exists("C:/Program Files/7-Zip/"))
{
sTexto = "set PATH=%PATH%;C:\\Program Files\\7-Zip\\"
}else if(dir.exists("C:/Program Files (x86)/7-Zip/"))
{
sTexto = "set PATH=%PATH%;C:\\Program Files (x86)\\7-Zip\\"
}else
{
stop("Error, o 7zip not installed?")
}
sTexto2 = paste0("\npushd ",sQualPasta7zip)
sTexto3 = paste0("\n7z a -r ../",sArquivoSaida," *")
sTexto4 = '\n( del /q /f "%~f0" >nul 2>&1 & exit /b 0 )'
cat(sTexto,file=NomeArquivo,append = TRUE)
cat(sTexto2,file=NomeArquivo,append = TRUE)
cat(sTexto3,file=NomeArquivo,append = TRUE)
cat(sTexto4,file=NomeArquivo,append = TRUE)
shell.exec(NomeArquivo)
while(file.exists(NomeArquivo))
{
Sys.sleep(10)
}
setwd(sWDTava)
return(TRUE)
}
Try like:
ZiparEm7zip("F:/MYFOLDER")

R > Monetdb, dbConnect error (package Monetdb.R)

I'm getting an error when trying to connect (dbConnect()) from within R to MonetDB (using MonetDB.R). There is a (related SO question) and I also tryed the sugestions there ( (a) use MonetDB.R beta version 0.9.5 or (a) use the regular MonetDB.R but replace DBI package with an earlier version (0.2-7)) without success.
This is the code and errors:
library(MonetDB.R)
options( "monetdb.sequential" = TRUE )
setwd("C:/Users/lucas_000/Desktop/Curso R/importa_RAIS")
batfile <-
monetdb.server.setup(
database.directory = paste( getwd() , "/MonetDB", sep="") ,
monetdb.program.path =
ifelse(.Platform$OS.type == "windows" , "C:/Program Files/MonetDB/MonetDB5" , "" ) ,
dbname = "RAIS" , dbport = 50003
)
batfile <- "C:/Users/lucas_000/Desktop/Curso R/importa_RAIS/MonetDB/RAIS.bat"
dbname <- "RAIS"
dbport <- 50003
pid <- as.numeric(monetdb.server.start( batfile ))
# output indicates: MonetDB 5 server v11.17.21 "Jan2014-SP3"
db <- dbConnect( MonetDB.R() , "monetdb://localhost:5003/RAIS" , wait = TRUE )
Assertion Failed!
Program: C\:Program Files\RStudio\bin\x64\rsession.exe
File: mapi.c, Line 91
Expression: IS_INTEGER(port)
#Then RStudio displays an error: "R Session Aborted"
Sistem details:
Windows 8.0, on RStudio, Version 0.98.1062
> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
other attached packages:
[1] R.utils_1.33.0 R.oo_1.18.0 R.methodsS3_1.6.1 downloader_0.3
[5] RCurl_1.95-4.3 bitops_1.0-6 sqlsurvey_0.6-11 survey_3.30-3
[9] KernSmooth_2.23-12 MonetDB.R_0.9.4 digest_0.6.4 DBI_0.3.1
loaded via a namespace (and not attached):
[1] tools_3.1.1
Attempted solutions and respective errors:
a) TRYING TO INSTALL MonetDB.R_0.9.5.zip (beta version)
library(devtools)
remove.packages("MonetDB.R")
install_url("http://homepages.cwi.nl/~hannes/R/MonetDB.R_0.9.5.zip")
* installing *binary* package 'MonetDB.R' ...
cp: unknown option -- )
Try `/usr/bin/cp --help' for more information.
Aviso: execução do comando 'cp -R . "C:/Users/lucas_000/Documents/R/win- library/3.1/MonetDB.R" || ( tar cd - .| (cd "C:/Users/lucas_000/Documents/R/win-library/3.1/MonetDB.R" && tar -xf -))' teve status 1
ERROR: installing binary package failed
* removing 'C:/Users/lucas_000/Documents/R/win-library/3.1/MonetDB.R'
Error: Command failed (1)
b) Keeping the MonetDB.R regular version (cran) but changing the DBI to an earlier version (also sugested on SO):
remove.packages("DBI")
library(devtools)
install_url("cran.r-project.org/src/contrib/Archive/DBI/DBI_0.2-7.tar.gz")
library(DBI)
db <- dbConnect( MonetDB.R() , "monetdb://localhost:5003/RAIS" , wait = TRUE )
# gives same error as above,
Assertion Failed!
Program: C:Program Files\RStudio\bin\x64\rsession.exe
File: mapi.c, Line 91
Expression: IS_INTEGER(port)
#Then RStudio displays an error: "R Session Aborted"
MonetDB.R 0.9.5 has been released to CRAN. Windows binary distributions should be available shortly.
The error may have been in the line where you create the dbConnect.
Your code (incorrect port):
db <- dbConnect( MonetDB.R() , "monetdb://localhost:5003/RAIS" , wait = TRUE )
Correct code:
db <- dbConnect( MonetDB.R() , "monetdb://localhost:50003/RAIS" , wait = TRUE )
Another issue with this code may have been choosing dbport to be be 50 003; I believe the default is 50 000.

Resources