Spawn subprocess in R - r

I'm trying to spawn a sub-process in R using the subprocess library, as presented in this tutorial. The Problem is that the program I'm trying to launch requires an additional command after the executable.
Example:
I would launch the command from the shell like this:
monetdbd create mydb
where 'create' is the additional command and 'mydb' a parameter.
I tried giving 'create mydb' as parameters in R like this:
handle <- spawn_process('/usr/local/bin/monetdb', c('create mydb'))
However from the output I got with
process_read(handle, PIPE_STDOUT, timeout = 3000)
I conclude that the parameters don't work as I'm getting the info message from monetdb on how to call it, just as if I call only 'monetdb' without the create command from the shell:
Usage: monetdb [options] command [command-options-and-arguments]
The second thing I tried is to include the create command into the path, but this leads to a "No such file and directory" error.
Any hints are appreciated.

MonetDB is the daemon process for MonetDB and has little to do with the (now old) version of MonetDBlite used in R. The latter one is decommissioned from CRAN and a newer version of MonetDBlite is expected to arrive early next year.

Without knowing anything about the package you’re using, and going purely by the documentation, I think you need to separate the command line arguments you pass to the functions:
handle <- spawn_process('/usr/local/bin/monetdb', c('create', 'mydb'))
This also follows the “conventional” API of spawn/fork/exec functions.
In addition, using c(…) is (almost) only necessary when creating a vector of multiple elements. In your code (and in the tutorial) it’s unnecessary around a single character string.
Furthermore, contrary to what the tutorial claims, this functionality is actually already built into R via the system2 and pipe functions (although I don’t doubt that the subprocess package is more feature-complete, and likely easier to use).
But if your ultimate goal is to use MonetDB in R then you’re probably better advised following the other answer, and using dedicated MonetDB R bindings rather than interacting with the daemon binary via subprocess communication.

Related

How to send prints of R commands to the log

The question might be unclear -sorry about that- but is pretty simple.
I'm currently writing a Rscript for which I'd like to send all the terminal's outputs to a log file, using the logr package, without using any redirection with >.
However, I have issues to redirect to the log file all the various statements that print to the console by the R commands of an other package (which is dada2).
For example, when I use a command of dada2, it prints some various informations to the console while computing its stuff, but these informations are not in the final log file.
Is there a way to correct this or should I use an other package or a simple redirection with >?
Thanks.

Using an active rsession when running a script via Rscript

I know that I can use Rscript to run a R script in the command line. Currently, I'm passing different parameters to my script, load a few packages and run a few functions. Then, I change the parameters — via a bash script — and run the same script with different parameters . This is all fine, however, I was wondering if there is a way that I can instantiate one rsession and grab it instead of going through the process of loading all my packages, etc. every time that Rscript executes my script.
library(ipc) might be of interest.
It allows to set up continuous communication between parent and child R processes by creating so called queues in the parent process.
Unfortunately you haven't specified your usecase with any code, so please see the vignette for examples.
One solution may be to use the session package. With it, you can perform the analogous load.session() and save.session() actions to the start and exit of the standard R launch and exit. The result is not 100 % equivalent, since the q() function is .Internal.

How to use psql in R? copy_to function of the "rpg" package not working

I am connecting to a PostgreSQL database and I would like to make use of psql commands (especially the \copy command) from within R.
I’m on a windows client using ODBC drivers to connect to the database. Basically any of the major ODBC package in R, including the „rpg“ package, is working to connect to the database and to read and write tables etc.
Apart from placing regular SQL queries the „rpg“ package allows to make use of psql commands. The „copy_to“ function should send the psql „\copy“ command to the database.
However, when running the function I get the error: „psql not found“
I also have pgAdmin III installed. Here running the \copy command is no problem at all.
Digging deeper I found that the rpg::copy_to function first runs Sys.which(„psql“) which returns: "" leading to said error.
Reading this thread made me think that adding the path to the pgAdmin psql.exe would do the trick. So I added the line
psql=C:\Program Files (x86)\pgAdmin III\1.16\psql.exe
in the R environment.
Running Sys.which(„psql“) still returns "", while Sys.getenv() correctly shows the path to the pqsl.exe that I specified.
How can I make Sys.which() find the psql.exe? Given that’s the correct way to solve this issue in the first place.
I would appreciate any help!

Call R scripts in Matlab

Is it possible to call R scripts in a MATLAB program? How can I do that?
You can use R in batch mode. If R is in your path, then you can call from MATLAB:
system('R CMD BATCH infile outfile');
will run the code in infile and place output in the outfile.
EDIT:
You can also give it a try with another approach using a R package rscproxy and R(D)COM Server, described here.
After using R(D)COM and Matlab R-link for a while, I do not recommend it. The COM interface has trouble parsing many commands and it is difficult to debug the code. I recommend using a system command from Matlab as described in the
R Wiki.
system is almost definitely the way to go, as described in other answers. For completeness, you could also use MATLAB's capability to run Java code, and JRI or RCaller to call R from Java. Similarly, you can use MATLAB's capability for running .NET code and R.NET.
Yes. On Windows, I have done a lot of this via the Matlab R-link and then R(D)COM server on the R side.
It works beautifully for passing commands and data back and forth. Calling R via the OS is feasible, but then you have to deparse (write) and parse (load) data passed between them. This is tedious and no fun. Especially if you are much data around. It also means that you lose state on the R side and every invocation is just like the first time.
On Linux or another OS, or even for more general usage, I'd now try Rstudio as a server -- see http://www.rstudio.org/docs/server/getting_started for more info.
Another way RWiki recommended:
CurrentDirectory=strrep(pwd,'\','/');
eval(['!C:\R\R-3.0.1\bin/Rscript "' CurrentDirectory '/Commands.R"'])
You can run command line functions in matlab using the unix command. The easiest way would probably be to set up an R script which outputs results to a text file, run the script in matlab using the unix command, and then (in matlab) verify that the file exists and load it up.
You could use the system command to execute R scripts. Something like the following:
[status] = system('R CMD BATCH [options] script.R [outfile]')
where [options] are the options your send to the R interpreter, and [outfile] is your output file.

Is there any method or plug in to handle R packages in NetBeans

I have created a NetBean GUI application.In that GUI i have added several menu items and i want to handle several statistical functions when clicking each menu items.For that purpose i need R plug in Netbeans such that I can easily link with R.
Thanks
Depending on your requirements there are different ways to do this, e.g. eval, link, pipe, socket or stub. I will write about these 5 methods here:
EVAL:
You can call R and pass in a script-file, R will evaluate the script and return the result of the statistical computation to the console.
(Please also tell us what operating-system you are running on, in the following I will just assume you are running on Windows)
If you have a file a.txt:
1+2
You can enter in the console cmd.exe:
R.exe -f a.txt
LINK:
You link some C-code to the R-sources: this is probably not very interesting to you because Netbeans is written in Java and linking would be difficult.
PIPE:
This is the possiblity you would choose if you want to also use the R-GUI. The above console-eval-option only produces textual output but with this method you can (eventually) also produce graphics and transmit them to Netbeans. You use named pipes (again, i have to assume you are using Windows) which you can open on the R-side with the function pipe().
SOCKET:
analogues, but with the function socketConnection
STUB:
You write a DLL in e.g. C, load the DLL using dyn.load(), and call functions in it with the function .C(); in that DLL you can use e.g. a pipe or socket to communicate with Java.

Resources