Quick Tip : RServe tricks that can make your life easier
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
RServe offers a number of configuration options that can come in handy when working with R inside Tableau but they are not captured in detail in RServe’s documentation. Let’s talk about a few.
How can I start RServe with a configuration file in a custom location?
RServe has a default place for the placement of configuration file e.g. /etc/Rserv.conf on Linux/Mac but you may not want to use that location or even have multiple config files that you switch sometimes so it is much more convenient to explicitly set it.
You can do this in the following way when starting RServe from R:
On Windows
Rserve(args="--RS-conf C:\\PROGRA~1\\R\\R-215~1.2\\library\\Rserve\\Rserv.cfg")
On Linux/Mac
Rserve(args=" --no-save --RS-conf ~/Documents/Rserv.cfg")
You can also do this from command line/terminal window. Assuming Rserve.exe is in the path or you’re in the folder that contains Rserve.exe so Rserve would be recognized:
Rserve –-RS-conf C:\\Users\\bberan\\Rserv.cfg
You probably noticed that on Mac there is an extra argument “--no-save”
. On Mac starting RServe requires using one of --save
,--no-save
or --vanilla
but what do they mean? The answer is in R help
BBERAN-MAC:~ bberan$ R --help Usage: R [options] [< infile] [> outfile] or: R CMD command [arguments] Start R, a system for statistical computation and graphics, with the specified options, or invoke an R tool via the 'R CMD' interface. Options: --save Do save workspace at the end of the session --no-save Don't save it --no-environ Don't read the site and user environment files --no-site-file Don't read the site-wide Rprofile --no-init-file Don't read the user R profile --restore Do restore previously saved objects at startup --no-restore-data Don't restore previously saved objects --no-restore-history Don't restore the R history file --no-restore Don't restore anything --vanilla Combine --no-save, --no-restore, --no-site-file,--no-init-file and --no-environ
Config files are very useful since they provide a centralized place to pre-load all the libraries you need so you don’t have to load them as part of each request which results in better performance, they allow evaluating any R code as part of RServe startup allowing you to load trained models or R script files, even data that you may want to use as part of your analysis when running R code from Tableau.
How can I debug R scripts I have in Tableau by taking advantage of my R development environment?
As you’re writing scripts in Tableau, you may want to understand different steps data is going through especially if you’re getting errors when script is being evaluated. There are many ways to debug. Using RServe in debug mode (Rserve_d.exe) outputs all the exchanges into the command line and can be a a bit verbose. You can also insert statements like write.csv in your script to create output of different data structures but by favorite option is to do this using an environment like the basic R GUI or RStudio.
On Linux/Mac
If you started Rserve from your R console the way described in the beginning, you can insert print statements to print into the console and even your plot statements will create visuals in R (if you have X11 installed) which can come in handy when debugging large and complex chunks of R code.
On Windows
You can achieve the same by starting R using:
run.Rserve(args="--no-save")
This takes over the current R session and makes it into an Rserve session as opposed to starting it by typing RServe() which starts a new Rserve process by calling Rserve.exe.
I hope you find this useful.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.