Starting Rserve in debug mode and printing variables from Tableau to R - r

I can't start Rserve in debug mode.
I wrote these commands in R:
library(Rserve)
Rserve(debug=T, args="RS-enable-control", quote=T, port = 6311)
library(RSclient)
c=RSconnect(host = "localhost", port = 6311)
RSeval(c, "xx<-12")
RSeval(c, "2+6")
RSeval(c, "xx")
RSclose(c)
install.packages("fpc")
I placed the Rserve_d.exe in the same directory where the R.dll file is located. But when I launch it and I launch Tableau with the Rserve connection I can't see anything in the debug console, just these few lines.
Rserve 1.7-3 () (C)Copyright 2002-2013 Simon Urbanek
$Id$
Loading config file Rserv.cfg
Failed to find config file Rserv.cfg
Rserve: Ok, ready to answer queries.
-create_server(port = 6311, socket = <NULL>, mode = 0, flags = 0x4000)
INFO: adding server 000000000030AEE0 (total 1 servers)
I tried another solution by the command Rserve(TRUE) in R, but I can't see the transactions between R and Tableau neither in the Rstudio console.
I wanted then to print the output of the variable in R from the R-script function, by print(.arg1). But nothing appears in the R console
but when I run print in the R console it works fine.

According to this article*, RServe should be run with the following command to enable debugging:
R CMD Rserve_d
An alternative is to use the ‘write.csv’ command within the calculated field that calls an R script, as suggested by this FAQ document from Tableau

Starting Rserve_d.exe from command line works. Most likely you have multiple instances of Rserve running and Tableau is sending requests to one that is not Rserve_d running in the command line.
Did you try killing all Rserve processes and then starting Rserve_d from command line?
If you don't want to run from the command line you can try starting Rserve in process from RStudio by typing run.Rserve() then using print() statements in your Tableau calculated fields for things you want to print.

In the R bin directory, you have two executables Rserve for normal execution and Rserve.dbg for debug execution. Use
R CMD Rserve.dbg
My OS is CENTOS7 and I am using the R installation from anaconda. If your RServe debug executable has a different name you should be using that.

Related

How to release R's prompt when using 'system'?

I am writing an R code on a Linux system using RStudio. At some point in the code, I need to use a system call to a command that will download a few thousand of files from the lines of a text file:
down.command <- paste0("parallel --gnu -a links.txt wget")
system(down.command)
However, this command takes a little while to run (a couple of hours), and the R prompt stays locked while the command runs. I would like to keep using R while the command runs on the background.
I tried to use nohup like this:
down.command <- paste0("nohup parallel --gnu -a links.txt wget > ~/down.log 2>&1")
system(down.command)
but the R prompt still gets "locked" waiting for the end of the command.
Is there any way to circumvent this? Is there a way to submit system commands from R and keep them running on the background?
Using ‘processx’, here’s how to create a new process that redirects both stdout and stderr to the same file:
args = c('--gnu', '-a', 'links.txt', 'wget')
p = processx::process$new('parallel', args, stdout = '~/down.log', stderr = '2>&1')
This launches the process and resumes the execution of the R script. You can then interact with the running process via the p name. Notably you can signal to it, you can query its status (e.g. is_alive()), and you can synchronously wait for its completion (optionally with a timeout after which to kill it):
p$wait()
result = p$get_exit_status()
Based on the comment by #KonradRudolph, I became aware of the processx R package that very smartly deals with system process submissions from within R.
All I had to do was:
library(processx)
down.command <- c("parallel","--gnu", "-a", "links.txt", "wget", ">", "~/down.log", "2>&1")
processx::process$new("nohup", down.comm, cleanup=FALSE)
As simple as that, and very effective.

Using R to scp files from local to remote

I am trying to move data files created in R (3.3, Windows 64) to a remote Linux server. Something along the lines of
mydata <- data.frame(a = c(1,2), b = c(3,4))
write.csv(mydata, "data.csv")
system("scp data.csv [username#ip.address:~/]")
However, this returns
Warning message: command ... had status 127
which I believe has to do with scp's password prompt. R does not know how to respond to the prompt, so it aborts. I've also tried using shell(), but that returns
'scp' is not recognized as an internal or external command, operable program or batch file.```
So I guess my question is: Is there a way to get R's system() function to respond to prompts, or else is there another way to scp from R?
Additional information:
The remote IP address changes every day so I cannot use a passphrase-free SSH key
I cannot easily install tools like expect or sshpass to add to a bash script that would run from R
There is an scp function in the Rcurl package, but it only handles retrieval from remote to local. I'm trying to deposit to remote.

R.matlab - Cannot start server

I'm trying to use the R.matlab package but frankly I'm not able to start the Matlab server.
I'm on OS X 10.11.5 and use Matlab R2015b.
Within R I get
> Matlab()
[1] "Matlab: The MATLAB host is 'localhost' and communication goes via port 9999. Objects are passed via the local file system (remote=FALSE). The connection to the MATLAB server is closed (not opened)."
> Matlab$startServer()
[1] 0
sh: matlab: command not found
But within a terminal I can indeed launch Matlab with the matlab command.
Also within a Terminal
matlab -nodesktop -nosplash -r MatlabServer
results in
Undefined function or variable 'MatlabServer'.
>>
I managed to fix it to run matlab from terminal. You just to go to your user profile ~/usr/your_nickame and edit file .bash_profile in any editor (and save!) and add
alias matlab=“/Applications/MATLAB_R2015a.app/bin/matlab”
That way terminal knows what you mean by matlab and where to find it and execute it. And it will make the alias permanent. That is option if you are starting the server from terminal
If you want to start it from R directly, I managed to make it work by setting what matlab command runs:
options(matlab="/Applications/MATLAB_R2015a.app/bin/matlab")
Then the usual works:
require(R.matlab)
Matlab$startServer()
matlab <- Matlab()
isOpen <- open(matlab)
< M A T L A B (R) >
Copyright 1984-2015 The MathWorks, Inc.
R2015a (8.5.0.197613) 64-bit (maci64)
February 12, 2015
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
Running MatlabServer v3.5.9-9000
MATLAB v7.x or higher detected.
Saving with option -V6.
Added InputStreamByteWrapper to dynamic Java CLASSPATH.
----------------------
MATLAB server started!
----------------------
MATLAB working directory: /Users/air/Desktop/Dissertation/myPack/finalPack
Trying to open server socket (port 9999)...Error using MatlabServer (line 130)
Java exception occurred:
java.net.BindException: Address already in use
at java.net.PlainSocketImpl.socketBind(Native Method)
at
java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:376)
at java.net.ServerSocket.bind(ServerSocket.java:376)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
>>
>

Rserve setting for connect to Tableau

This is the first time I have tried to connect R and Tableau.
I have downloaded and installed Rserve successfully but every time I try to start Rserve is get this warning:
Starting Rserve...
"C:\Users\SIMON~1.HAR\DOCUME~1\R\WIN-LI~1\3.1\Rserve\libs\x64\Rserve.exe"
Warning message:
running command '"C:\Users\SIMON~1.HAR\DOCUME~1\R\WIN-LI~1\3.1\Rserve\libs\x64\Rserve.exe" ' had status 127
I have been searching for days and couldn't find any fix.
The Rserve() function is trying to start an application (Rserve.exe) and failed. There's a couple of things you can do.
Go to the "C:\Users\SIMON~1.HAR\DOCUME~1\R\WIN-LI~1\3.1\Rserve\libs\x64\" file directory and try loading the exe yourself, troubleshoot from there.
use the run.Rserve() function instead of Rserve(). This will use your current R session to start the Rserve server. This means the exe doesn't need to be run. This worked well for me because I am working in an environment where I don't enough privileges to run the exe. It does mean that your R session can't do anything else while the server is running, but you can always load up 2 sessions at the same time.

Run R interactively from Rscript

I'm trying to start a shiny app or an interactive .Rmd document from an Rscript. However, all I get is a message
Listening on http://127.0.0.1:...
I believe this is because R is running in interactive mode (another post about this). How can I write the proper Rscript so that either of the following would work?
My script
#!/usr/bin/Rscript
## This
library(shiny)
runApp(appDir = "../app")
## Or this
## rmarkdown::run("Main.Rmd")
If I understand your question correctly, I was able to achieve this with littler, which I use in lieu of Rscript for scripting tasks that revolve around R. I'm running CentOS 7, and based on the code in your question it looks like you are on a Unix-like machine, so installing littler should not be an issue. For minimal reproducibility, I used the default shiny application and shiny-based Rmarkdown templates provided by RStudio, saving them as testapp (the project / application directory name) and testRMD.rmd, respectively. Then, I have the following scripts:
testapp.r
#!/usr/bin/env r
shiny::runApp(
"~/tmp/delete/testapp",
port = 7088,
launch.browser = TRUE,
host = "127.0.0.1")
testRMD.r
#!/usr/bin/env r
rmarkdown::run(
file = "testRMD.rmd",
dir = "~/tmp/delete",
shiny_args = list(
port = 7088,
launch.browser = TRUE,
host = "127.0.0.1"))
Set the permissions for these files so they can be executed -
[nathan#nrussell R]$ chmod +x testapp.r testRMD.r
(chmod +u ... should suffice, but regardless...), and you should be all set to run them from your terminal, etc...
[nathan#nrussell R]$ ./testapp.r
Loading required package: shiny
Listening on http://127.0.0.1:7088
[nathan#nrussell R]$ ./testRMD.r
Loading required package: shiny
Listening on http://127.0.0.1:7088
There is some additional command line output for the Rmd file that I omitted, but I'm sure this could be suppressed easily if desired. Anyhow, this seems to be working properly - both the shiny application and Rmarkdown application are interactive, just as when launched from RStudio - but if you had something else in mind please clarify.
Thanks #nrussell, your example helped me a lot!
Here is my solution for launching an interactive markdown doc on Windows 10.
REM Change to correct directory
cd "C:\Users\me\Documents\project_folder\"
REM Print files available (not required, but helpful)
dir
REM Point system to R's Pandoc with Rscript then launch
"C:\Program Files\R\R-4.0.3\bin\Rscript.exe" -e ^
"Sys.setenv(RSTUDIO_PANDOC='C:/Users/me/Documents/RStudio/bin/pandoc'); rmarkdown::run(file = 'myInteractiveMarkdown.Rmd', shiny_args = list(launch.browser = TRUE, host = '127.0.0.1'))"
I found I got two errors initially:
When I didn't point the system env to R's pandoc it gave the error message error pandoc version 1.12.3 or higher is required which I solved using the instructions here
When I set the port in the shiny_args, subsequent executions of the bat file would get an error that the port was already busy

Resources