collect output of system rscript command as r object - r

I am using RStudio in Windows to develop and run a pipeline for multivariate analysis that involve big dataset (90 by ~ 60000 matrices). With matrices of such a size, I got "protection from stack overflow" pretty often. One way of avoid this problem, while still using RStudio as opposite to the regular Rgui, is to run my script(s) using the following syntax
system("Rscript --max-ppsize=500000, my_script.s").
However, running this command results in the script running succesfully, but I cannot get the desired output. If I run the previous command with the following option
opt-< system("Rscript --max-ppsize=500000, my_script.s", internal = TRUE)
I got the standard output to the terminal as output (as a character vector), but not the desired output.
Consider this toy examples:
save the following code in my_script.R
print("first call")
rnorm(15)
print("second call")
rnorm(20)
and run the following code from the console
a <- system("Rscript my_script.R", intern = TRUE)
a
As you can see, the output is a character vector of length 9 with the standard output to the console.
If you modify my_script.R as follow
print("first call")
i_want_this <- rnorm(20)
and then run it again
a <- system("Rscript my_script.R", intern = TRUE)
a
now the only thing stored in a is the output of the print command.
My question is: is there a way to collect the i_want_this variable as an r object (in this case a numeric vector of length 20) ?
A similar question has been asked here, without a satisfying answer.

Related

what is syntax for using iter function

I have no documentation or knowledge of how to run a iter command. The question is as follows:
Run the kc$iter command. Include the command, output screenshot, and
explain what the output shows.
# set seed
set.seed(12345)
# Cluster analysis
kc<-kmeans(myvehicle, 4)
kc
# Iteration
kc$iter(kc(myvehicle), )
The last command is a guess and I got an error message: Error: attempt to apply non-function. I don't know what the syntax is and the example I used and it had a value of 12 after the comma. However, I don't know why so I left it blank. I couldn't find anything that describes how to use this function.

Not able to run selected lines in REPL R in sublime text

Followed these instructions to set up REPL for sublime text
http://www.kevjohnson.org/using-r-in-sublime-text-3/
R console is running. But I am unable to push text to console using the shortcuts
Ctrl+Shift+,,l
I must be doing something wrong here, not able to figure it out on my own. Any help appreciated.
I get the following error:
Cannot find REPL for 'regexp'
Edit: Adding sample code
library("e1071")
data(iris)
m <- naiveBayes(Species ~ ., data = iris)
m
table(predict(m, iris), iris[,5])
REPL checks scopes to know what console to run, and returns an error if no console is associated to a language. It might have been that your file was not in the R syntax.
To change the syntax : Command Panl (ctrl+shift+p), Set Syntax: R

Error when running (working) R script from command prompt

I am trying to run an R script from the Windows command prompt (the reason is that later on I would like to run the script by using VBA).
After having set up the R environment variable (see the end of the post), the following lines of code saved in R_code.R work perfectly:
library('xlsx')
x <- cbind(rnorm(10),rnorm(10))
write.xlsx(x, 'C:/Temp/output.xlsx')
(in order to run the script and get the resulting xlsx output, I simply type the following command in the Windows command prompt: Rscript C:\Temp\R_code.R).
Now, given that this "toy example" is working as expected, I tried to move to my main goal, which is indeed very similar (to run a simple R script from the command line), but for some reason I cannot succeed.
Again I have to use a specific R package (-copula-, used to sample some correlated random variables) and export the R output into a csv file.
The following script (R_code2.R) works perfectly in R:
library('copula')
par_1 <- list(mean=0, sd=1)
par_2 <- list(mean=0, sd=1)
myCop.norm <- ellipCopula(family='normal', dim=2, dispstr='un', param=c(0.2))
myMvd <- mvdc(myCop.norm,margins=c('norm','norm'),paramMargins=list(par_1,par_2))
x <- rMvdc(10, myMvd)
write.table(x, 'C:/Temp/R_output.csv', row.names=FALSE, col.names=FALSE, sep=',')
Unfortunately, when I try to run the same script from the command prompt as before (Rscript C:\Temp\R_code2.R) I get the following error:
Error in FUN(c("norm", "norm"))[[1L]], ...) :
cannot find the function "existsFunction"
Calls: mvdc -> mvdcCheckM -> mvd.has.marF -> vapply -> FUN
Do you have any idea idea on how to proceed to fix the problem?
Any help is highly appreciated, S.
Setting up the R environment variable (Windows)
For those of you that want to replicate the code, in order to set up the environment variable you have to:
Right click on Computer -> Properties -> Advanced System Settings -> Environment variables
Double click on 'PATH' and add ; followed by the path to your Rscript.exe folder. In my case it is ;C:\Program Files\R\R-3.1.1\bin\x64.
This is a tricky one that has bitten me before. According to the documentation (?Rscript),
Rscript omits the methods package as it takes about 60% of the startup time.
So your better solution IMHO is to add library(methods) near the top of your script.
For those interested, I solved the problem by simply typing the following in the command prompt:
R CMD BATCH C:\Temp\R_code2.R
It is still not clear to me why the previous command does not work. Anyway, once again searching into the R documentation (see here) proves to be an excellent choice!

How to call plot.xts when using RScript

UPDATE: Thanks to Joshua's comment I realized the problem wasn't being inside a function, but inside a script. So I've edited the question and also provided my own answer.
When I use plot.xts() interactively it pops up a graphics window. I just tried it from inside a function (I'm troubleshooting a unit test and wanted some visual help) but nothing appeared. Aha, says I, I know the trick, just use print.
But print(plot.xts(x)) still shows no chart and instead prints my xts object! I.e. it does exactly the same as print(x).
The script I use to run unit tests is:
#!/usr/bin/Rscript --slave
library('RUnit')
options(warn=2) #Turn warnings into errors
#By naming the files runit.*.R, and naming the functions test*(), we can use
# all the defaults to defineTestSuite().
#NOTE: they have a weird default random number generator, so changed here
# to match the R defaults instead.
test.suite=defineTestSuite('tests',dirs=file.path('tests'),
rngKind = "Mersenne-Twister", rngNormalKind = "Inversion")
test.result <- runTestSuite(test.suite)
printTextProtocol(test.result)
The script below does two things:
plot to a device file, as you would in headless setting such as a webserver,
plot a screen device, I use x11() but you could use win().
There is no limitation imposed by Rscript. And this has nothing to do with xts as you could just as easily plot an xts object.
#!/usr/bin/Rscript
set.seed(42)
x <- cumsum(rnorm(100))
png("/tmp/darren.png")
plot(x)
dev.off()
x11()
plot(x)
Sys.sleep(3) # could wait for key pressed or ...
You cannot use graphics (or input functions like readline) when using RScript. However an RScript is still just R, so when you want to add something interactive (e.g. for troubleshooting) start R, then type:
source('run_tests.R')
When run this way, a line like this shows the chart:
plot(x$High);cat("Press a key");readline()
When run directly from the commandline with ./run_tests.R that line gets quietly ignored.

Running R function as command line script with arguments

Lets say I have the following script
k <- as.numeric(readline("Start Index: "))
tot <- NULL
for (i in 1:k){
tot <- c(tot, i)
}
write.csv(tot, "test.csv")
I would like to run this script from the command line. How can I run it so that it still asks for the user input (k). Or otherwise can I add the value of k in the CMD command as argument? I am on Linux.
You might find the information in ?commandArgs helpful.
From the description:
Provides access to a copy of the command line arguments supplied
when this R session was invoked.

Resources