can you print/echo message from .Renviron? - r

It is possible to print messages from .Renviron so it is shown in RStudio at startup?
For example:
R_LIBS_USER="C:/Rlib/"
APIKEY="blabla"
echo Company envvar set
I don't want to use .Rprofile as that is overwriten in projects.
EDIT:
This is on locally installed R on windows. We roll out R/Rstudio/Rtools in a software package to all members of a certain AD group. It is in this process that I would like to do some configurations and information.

As noted above you could set environment variables in R/R-4.x.x/etc/Renviron.site to set these for all users, even when projects are involved:
hello_message = "Hello! This has worked!"
Then set R/R-4.x.x/etc/Rprofile.site to print these variables:
message(Sys.getenv("hello_message"))
Then starting a new session, inside outside of a project will set all variables and print all messages:
R version 4.1.2 (2021-11-01) -- "Bird Hippie"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
...
Hello! This has worked!
>

Related

Runing an R Script that includes messageboxes from the windows terminal

I am trying to run a simple R Script that creates a messagebox using the R library svDialogs:
library(svDialogs)
user.input <- dlgInput("Enter a number", Sys.info()["user"])$res
This runs well whe I execute it using RStudio for example:
RStudio 2922.02.1 Build 461, using
R version 3.6.1 (2019-07-05) -- "Action of the Toes"
Platform: i386-w64-mingw32/i386 (32-bit)
However, I have also tried to run this using a .bat file:
"Path_to_Rscript\Rscript.exe" Path_to_File\File.r
I have used .bat files to automate the sequential execution of RScripts from different R installations and Architectures (32Bit and 64bit), but I am having problems executing RScripts that require showing these messageboxes to either show data or to require input from users.
What am I doing wrong?

Source .bashrc in Rstudio

I have just installed some Perl modules locally in Ubuntu 18.04 LTS.
When calling them using the RStudio Server like this
system("perl -MBio::TreeIO -e 1")
I get the following error
Can't locate Bio/TreeIO.pm in #INC (you may need to install the Bio::TreeIO module) (#INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base)
When I source ~/.bashrc in the terminal everything's ok.
I tried creating a ~/.Rprofile containing:
system(". ~/.bashrc")
but no good.
According to the documentation, accessed by running help(Startup) in the R shell, you should create a file .Renviron in your home directory which will be read when starting up a new R session. For example:
.Renviron
FOOBAR=/foo/bar/foo/bar
R shell
R
R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...
> cat(Sys.getenv("FOOBAR"), "\n")
/foo/bar/foo/bar
So just create your .Renviron file with the PERL5LIB variable set to your local perl package installation directory.

RStudio not loading RevoScaleR on Ubuntu 16.04

Long story short:
Calling rxImport() works fine in a Terminal R session but fails when using RStudio, despite using the same installation of R.
Setup:
I'd like to use RevoScaleR functions like rxImport(...) on Ubuntu. I have an installation of RStudio and R done through package manager.
I've installed MS Rclient following the instructions here.
Everything seems to work as I can run in the terminal:
/opt/microsoft/rclient/3.4.3/bin/R$ ./R
R version 3.4.3 (2017-11-30) -- "Kite-Eating Tree"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...
Microsoft R Open 3.4.3
The enhanced R distribution from Microsoft
Microsoft packages Copyright (C) 2018 Microsoft
Loading Microsoft R Client packages, version 3.4.3.0097.
Microsoft R Client limits some functions to available memory.
See: https://go.microsoft.com/fwlink/?linkid=799476 for information
about additional features.
...
Using the Intel MKL for parallel mathematical computing (using 2 cores).
Default CRAN mirror snapshot taken on 2018-01-01.
See: https://mran.microsoft.com/.
> R.home()
[1] "/opt/microsoft/rclient/3.4.3/runtime/R"
> rxImport()
Error in rxImport() : 'inData' must be specified.
So the function is loaded and available by default. Now I tell the RStudio to use this installation of R by adding the following at the end of ~/.profile :
RSTUDIO_WHICH_R="/opt/microsoft/rclient/3.4.3/bin/R/R"
Error:
When I run rstudio I get a R console in the IDE saying:
> R.home()
[1] "/opt/microsoft/rclient/3.4.3/runtime/R"
> rxImport()
Error in rxImport() : could not find function "rxImport"
Possible causes
I've found some threads with people complaining about MS packages not dealing properly with ~ and working directory (i.e. on a remote server), setwd() doesn't solve my issue, though.
Do you have any ideas on how to get RevoScaleR to work with RStudio? Thanks in advance.
An answer I got from Github:
https://github.com/rstudio/rstudio/issues/2455#issuecomment-375327109
try setting
R_LIBS_SITE=/opt/microsoft/rclient/3.4.3/libraries/RServer
in
/opt/microsoft/rclient/3.4.3/runtime/R/etc/Renviron

RStudio - LD_PREOAD Issue

I get a RDOBC error in RStudio that does not occur when I use R on the server itself.
R -
R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)
library(RODBC)
conn = odbcConnect("Cloudera_HIVE64", uid="manishm", pwd="tdpass")
data = sqlQuery(conn,"SELECT * from default.test")
data
[1] test.id test.name
<0 rows> (or 0-length row.names)
RStudio -
R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)
library(RODBC)
conn = odbcConnect("ClouderaHIVE64", uid="manishm", pwd="tdpass")
Warning messages:
1: In odbcDriverConnect("DSN=ClouderaHIVE64;UID=manishm;PWD=tdpass") :
[RODBC] ERROR: state HY000, code 11560, message [unixODBC][Cloudera]ODBC Unable to locate SQLGetPrivateProfileString function.
2: In odbcDriverConnect("DSN=Cloudera_HIVE64;UID=manishm;PWD=tdpass") :
ODBC connection failed
I have checked the environment variables and have ensured that the below are correct for all users
ODBCINI=/opt/cloudera/hiveodbc/Setup/odbcinst.ini
LD_PRELOAD=/usr/lib64/libodbcinst.so
Any idea why this will work in R and not in RStudio with the same environment settings?
I once had a similar problem on ubuntu 12.04 that I could solve, but it was kind of hacky. For reasons I don't understand, it seems that environment variables are sometimes not passed to applications that are started by clicking on the launcher. The problem that I had concerned another package, but the symptoms were similar: a properly defined environment variable seemed to be unknown to RStudio.
From this experience, I suggest the following:
Open a linux shell and change directory to cd /usr/share/applications
Type sudo cp rstudio.desktop rstudio.desktop.bak to create a backup copy of this file which we will modify.
Edit rstudio.desktop as sudo with your favorite text editor, like sudo gedit rstudio.desktop
The second line after [Desktop Entry] probably contains something like Exec=/usr/lib/rstudio/bin/rstudio %F. Change this line so that the required environment variables are explicitly passed, e.g., Exec=sh -c 'env LD_PRELOAD=/usr/lib64/libodbcinst.so /usr/lib/rstudio/bin/rstudio %F'
Close RStudio and try to start again; possibly after rebooting.
If you experience any problem launching RStudio after these modifications, restore the previous version of the file rstudio.desktop with cd /usr/share/applications followed by cp -f rstudio.desktop.bak rstudio.desktop. This should restore the initial state. It means that your problem wasn't soved, but at least no harm should be done.
Hope this helps. Wishing you good luck...

What causes the error when using ggplot plot on different OSes?

I'm getting a strange error when loading .Rdata produced on one OS and transferred to another. On a Windows machine I generate a large number of plots and store them in a .Rdata file. I then transfer them to a linux server running CentOS 5 and access them by loading the file and recalling the plot.
When I run the following lines on CentOS I get an error:
library(ggplot2)
load('mydata.Rdata')
p
Error in UseMethod("facet_train_layout") :
no applicable method for 'facet_train_layout' applied to an object of class "c('proto', 'environment')"
The Windows 7 OS that was used to produce this .Rdata file is using the following version of R:
R version 2.14.2 (2012-02-29) Copyright (C) 2012 The R Foundation for
Statistical Computing ISBN 3-900051-07-0 Platform:
x86_64-pc-mingw32/x64 (64-bit)
The CentOS system that produces an error is as follows:
R version 2.14.2 (2012-02-29) Copyright (C) 2012 The R Foundation for
Statistical Computing ISBN 3-900051-07-0 Platform: i686-pc-linux-gnu
(32-bit)
The ggplot2 version on both systems is ggplot2_0.9.2.1. This process has worked fine for the last six months, the only problem has been today due to an update and I don't understand what is going wrong.
Both versions of R were upgraded to 2.14.2, as well as the ggplot package to ggplot2_0.9.2.1.
I presume the old version was 0.8.9 or below. There was a fundamental change with version 0.9.0:
FACETS
Converted from proto to S3 objects, and class methods (somewhat) documented in facet.r. This should make it easier to develop new
types of facetting specifications.
See http://cran.r-project.org/web/packages/ggplot2/NEWS for more information

Resources