Pass R object to R batch process? - r

I would like to pass a PostgreSQL connection object (which is an RObject) to a batch process. Is this possible at all?
Why am I trying to do this?
I had trouble running a script that sources another script on R Studio Server. The script itself runs fine without the other script sourcing it. So this is clearly an R Studio Server Web Interface issue. However I am trying to circumvent the issue by just triggering a batch from R Studio server, after I used its nice .rs.askForPassword function that ships with Rstudio. I tried to following but couldn't get it to work cause to connection always ending up being expired when it was used from the batch.
# run file
save(con,file="conObj.Rdata")
system("R CMD batch.R") # also tried Rscript
batchfile looks like this
# batch file
load("conObj.Rdata")
#.... run some db query #
save("someObjFromDb","test.Rdata")
This breaks because of an expired connection object.

Related

How to execute an R shiny script from the command line

I've created a single R file 'myfile.R' which is composed of several functions, reading from an odbc connection, and of course the ui and server components for the shiny application. The goal is for the user to treat this as an executable, that is, I'd like them to be able to run this app without having to run Rstudio. Currently on this environment, Rstudio is installed (as are the relevant packages), what I'm currently writing in command line is:
Rscript myfile.R
The file looks to be successfully loaded, but the application itself does not 'pop up' towards me, infact nothing does, it just says that it is listening on http....
I was thinking of creating a bash script that could do this, and this seemed to be the most simple approach but this is not working.

How to access R session/global environment after running script through Rscript on Linux

I am using a Linux Workstation to run my R script. I did so using screen and then Rscript myRscript.R. Is there anyway to access the R session after the script has run? I want to be able to write new commands and access the global environment that was created during that session.
I tried asking the Unix community, but no response...
https://unix.stackexchange.com/questions/608073/how-to-pass-code-to-attached-screen
The session is lost after the script is done running. But you can save the environment using save.image("env_file.Rdata") and use it later using load("env_file.Rdata").
See http://ugrad.stat.ubc.ca/R/library/base/html/save.html

R web scraping in Azure ML errors out

I have written a script in RStudio (running R 3.5.2) that scrapes data from a particular website. The script reaches out to a website, uses download.file to pull the underlying code, and uses tags to extract the desired data.
The script runs without error in RStudio, but when I try to run the code in the "Execute R Script" node in Azure ML it throws a 0063 error saying that it "cannot reach URL ". The code runs perfectly up until it tries to reach out to the URL. (see code below)
I have tried switching the R version in Azure ML--neither of the 3 options work.
for(a in 1:length(job_url)) {
download.file(url, destfile = filename, quiet=TRUE)
...
}
I expect the script to run the same in RStudio and Azure ML. Any ideas how to get this script to run in Azure ML the same way it runs in RStudio?
For security reasons, all networking from or to R code in Execute R Script modules is blocked by Azure.
https://learn.microsoft.com/en-us/azure/machine-learning/studio-module-reference/execute-r-script#networking

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.

How to run R script from command line repeatedly but only load packages the first time

I want to run an R script (in Win 7) from SQL Server 2014 each time a new record is added (to perform some analysis on the data). I saw that this can be done with the xp_cmdshell command which is like running it manually from the command line.
My problems (and questions) are:
I've made out from various websites that probably the best option is to use Rscript. This would have to be used at the command line as:
C:\Program Files\R\R-3.2.3\bin\x64\Rscript "my_file_folder\my_file.r
Can I copy Rscript.exe to the folder where my script is, such that I can run my script independently, even if R is not installed? What other files do I need to copy together with Rscript.exe such that it would work independently?
My script loads some packages that contain functions that it uses. Is there a way to somehow include these in the script such that they don't have to be loaded every time (it takes about 5 sec so far and I need this script to be faster)? Or is there a way to only load these packages the first time that the script runs?
In case the overall approach I've described here is not the best one, I am open to doing it differently. Maybe there is a way to somehow package the R script together with all the required dependencies (libraries and other parts of the R software which the script would need to run independently).
What I ultimately need is a for the script to run silently, and reasonably fast, without any windows or anything else popping up, each time a new record is added to my database, do the analysis and exit.
Thanks in advance for any answers.
UPDATE:
I figured out an elegant solution to running the R script. I'm setting up a job in SQL Server and inside that job I'm using "xp_cmdshell" to run my script as a parameter to Rscript.exe, as detailed at point 1 above. I can start this job from any stored procedure and the beauty of it is that the stored procedure does not wait for the script to finish. It just triggers the job (that runs the script in a separate thread) and then it continues with its business.
But questions from points 1 and 2 still remain.

Resources