Is there an opencpu equivalent to browser()? - r

Is it possible to insert a browser statement (or equivalent) in your R code with opencpu and when the ajax or plot call is made the R session (or Rstudio session) from which the opencpu server instance is running will stop and allow debugging?

No, I don't think that would make sense. The OpenCPU server exposes an HTTP API to call R functions. If you want to debug the R function, just call it locally in R. That way you an use all your usually debugging tools.
Once the function works as expected you can call it remotely through the OpenCPU API, and it will give the same result.
Perhaps you can give an example of the type of problem that you're trying to debug, maybe I can give some advice on your workflow.

Related

Spawn subprocess in 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.

Workflow for using command line R?

I am used to using R in RStudio. For a new project, I have to use R on the command line, because the data storage and analysis are only allowed to be on a specific server that I connect to using ssh. This server doesn't have rstudio-server to support remote RStudio sessions.
The project involves an extremely large dataset, and some pre-written code to load/format the data that I have been told to run using "source()" before I do anything else. This takes several minutes to run and load the data each time.
What would a good workflow be for something like this? Editing my code in a .r file, saving, then running it would require taking several minutes to load the data each time. But just running R in an interactive session would make it hard to keep track of what I am doing and repeat things if necessary.
Is there some command-line equivalent to RStudio where you can have an interactive session but be editing/saving a file of your code as you go?
Sounds like JuPyteR might be your friend here.
The R kernel works great.
You can use it on a remote server either with exposing an open port (and setting up JuPyteR login credentials)
Or via port forwarding over SSH.
It is a lot like an interactive reply, except it holds state.
And you can go back and rerun cells.
(Of course state can be dangerous for reproduceability)
For RStudio you can launch console and ssh to your remote servers even if your servers don't use expensive RStudio for servers platform. You can then execute all commands from R Studio directly into the ssh with the default shortcut key. This might allow to continue using R studio, track what you're doing in the R script, execute interactively.

Improve R Script execution from NodeJS

I'm new to R, and I'm invoking an R script from a NodeJS app. When the R Script is invoked, it takes a long time in producing output. I investigated and realized that the bulk of that overhead is when it loads the libraries and the model I'm using. Let me clarify that any optimization would work, taking into account that I'm running this code in a Raspberry Pi 2 b+.
My question is: Is there a way to preload all the libraries and the model on R and then trigger predictions on demand? So that I won't need to reload the libraries and the model every time I want a prediction.
No. Since you're just invoking a script the loading of everything it has to be done everytime the script is run; since nothing didn't exist in memory before you invoked it.
One workaround I would suggest is to instead run a R script have your R script running as a service and then query that service from nodejs.
I cannot help you with that since my expertise for R doesn't go very far away and I don't know if having an R server is even possible.
An alternative to that, if it is not too cumbersome, is to port your R project to python and mount a server of some kind (which with python is extremely easy to do) and then poke that server from nodejs. Since you would be running a server you can just cache the libraries at the server startup time and have everything in RAM for your next query.

Run R.NET in and Azure Function

I am trying to integrate R into an Azure Function.
Instead of just calling the R exe - I want to be able to try the R.NET library to make it easier to pass and collect data between .NET and R. For example, respond to an event.
It runs fine locally, but once deployed as an Azure function, I get various errors. The latest ": This engine is not running. You may have forgotten to call Initialize"
For anyone else wanting to try this, I had to force the Azure function to run as 64bit, and also install the R extension library to the function. at RDotNet.REngine.CheckEngineIsRunning()
Has anyone had any success? Is anyone with R.NET experience wanting to help getting to work as an Azure function environment?
Looking at the code in R.NET for the functions RDotNet.REngine.GetInstance(), RDotNet.REngine.CheckEngineIsRunning(), and RDotNet.REngine.Initialize(), it appears as though creating the engine instance via RDotNet.REngine.GetInstance() should help you avoid this issue, as after calling that the method should be running, and you should not be encountering this area.
It is possible that you are encountering some error in creating this instance. Looking at the code comments about usage in the above link, it looks like an environment variable needs to be set for PATH. It is possible that the code you used to set this up does not work in Azure Functions. You can manually set environment variables in Azure Functions by using App Settings.

r jug package not able to run, need local hosting of r functions

I'm trying to replicate the toy example found here:
https://www.r-bloggers.com/jug-easily-create-r-apis/
When I load the jug package, and run the script, my R console just seems to run continuously, thus I not able to attempt the second part of the code where I submit the curl request. I basically want to create a few functions that accept HTTP input, and host the functions locally. What am I missing?
See the screenshot image of my R console
Did you try going to http://127.0.0.1:8080? Do it while the script is running (as per your screenshot). It should prompt you the response of the function. In any case, I had similar issues with jug, so I recommend using plumber instead.

Resources