R .libPaths() difference between RStudio and command-line R - r

When I run R from the command line:
> library(ggplot2)
...
> path.package('ggplot2')
[1] "/home/yang/R/x86_64-pc-linux-gnu-library/2.13/ggplot2"
> .libPaths()
[1] "/home/yang/R/x86_64-pc-linux-gnu-library/2.13"
[2] "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library"
[4] "/usr/lib/R/library"
> Sys.getenv('R_LIBS_USER')
[1] "~/R/x86_64-pc-linux-gnu-library/2.13"
(Note: that environment variable actually doesn't exist when I check from my shell.)
But from RStudio Server running on the same box, and after logging in as the same user:
> path.package('ggplot2')
[1] "/home/yang/R/library/ggplot2"
> .libPaths()
[1] "/home/yang/R/library" "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library" "/usr/lib/R/library"
[5] "/usr/lib/rstudio-server/R/library"
> Sys.getenv('R_LIBS_USER')
[1] "/home/yang/R/library"
Can you explain why these are different by default? Is this an RStudio customization? (Why?) Thanks in advance.

Direct answer from the source:
http://support.rstudio.org/help/discussions/questions/204-r-libpaths-difference-between-rstudio-and-command-line-r
Hi there,
Yes, we have a custom R_LIBS_USER setting which is intended to make it
easier to upgrade the server to a new version of R without requiring
that every user rename their library directory and/or re-build their
packages. That way the administrator can do an upgrade without fearing
that they'll break their user's working environment (realize that this
could also be accomplished by writing an upgrade script that does the
requisite rename/rebuild for each user).
Fully agree that this isn't necessarily desirable in all cases. Here
is what you can do to work around it:
The Rtudio Server R_LIBS_USER is controlled by the following setting
in /etc/rstudio/rsession.conf:
r-libs-user=~/R/library
This variable supports the same wildcarding as R_LIBS_USER (as
described here:
http://stat.ethz.ch/R-manual/R-patched/library/base/html/libPaths.html)
so you could change this to the following to make RStudio behave just
like console R:
r-libs-user=~/R/%p-library/%v
(note I believe that is the right syntax to reproduce the directory
shown in your output above but you'll definitely want to double check
that)
Hope that clears things up and that you can get things configured as
you'd like. Let us know if you have other questions or if this doesn't
work as described.
J.J.

A quick googling got me here:
http://support.rstudio.org/help/discussions/problems/868-how-to-configure-libpaths
so it looks like RStudio uses its own libs, set in /etc/rstudio/rsession.conf. Why? Who knows.

Related

Is it possible for `renv` to be "bypassed" or temporarily turned off?

I have an RStudio project that manages packages using renv, which works great.
But sometimes I want to debug or explore something using libraries on my main environment(right word?) that I don't want to add to the project. For example, maybe I want to run some long debug function and call beepr::beep() at the end. Is that possible without installing the beepr package to the project?
long_process()
beepr::beep() # How do I make this call?
You could technically disable sandboxing, which is where renv prevents packages installed in the system library from being made available in the project (which is why you wouldn't be able to use beepr::beep() in your example. You can change your user configuration easily, just see the documentation from env. The setting here would be renv.config.sandbox.enabled.
However, I don't think that's the best option. Depending on the snapshot type settings described in ?renv::snapshot, you needn't worry about installing beepr but it getting added to the lock file.
You can check or set the snapshot type using:
renv::settings$snapshot.type()
If you install the beepr package in the environment, then depending on the type you will get:
"all": the only problematic setting. If you run snapshot with beepr actively loaded, it will be added to the lock file, which isn't what you want.
"implicit": as long as beepr isn't referenced in R code saved to the library, it won't be added to the lock file.
"explicit": only adds packages that are defined in DESCRIPTION.
"custom": as implied, is custom, but depends on files saved in the project, so would only add beepr if saved somewhere rather than just used interactively using debug.
So, in summary, unless you are using "all", you should be safe to install and use beepr! If you are using "all", then just be sure to detach("package:beepr", unload = TRUE) before running snapshot().
Answering the main title rather than the specific example used: to temporarily disable renv, use renv::deactivate(). This will mainly comment/deactivate the line source("renv/activate.R") in .Rprofile, which has the main effect of changing your library path.
Check:
proj <- "your-project-path"
## with renv
renv::activate(proj)
.libPaths()
#> [1] "your-project-path/renv/library/R-4.2/x86_64-pc-linux-gnu"
#> [2] "/usr/lib/R/library"
## without renv
renv::deactivate(proj)
.libPaths()
#> [1] "/home/usert/R/x86_64-pc-linux-gnu-library/4.2"
#> [2] "/usr/local/lib/R/site-library"
#> [3] "/usr/lib/R/site-library"
#> [4] "/usr/lib/R/library"
Created on 2022-09-10 by the reprex package (v2.0.1)

R Console Language Setting [duplicate]

My error messages are displayed in French. How can I change my system language setting so the error messages will be displayed in English?
You can set this using the Sys.setenv() function. My R session defaults to English, so I'll set it to French and then back again:
> Sys.setenv(LANG = "fr")
> 2 + x
Erreur : objet 'x' introuvable
> Sys.setenv(LANG = "en")
> 2 + x
Error: object 'x' not found
A list of the abbreviations can be found here.
Sys.getenv() gives you a list of all the environment variables that are set.
In the case of RStudio for Windows I succeeded in changing the language following the instructions found in R for Windows FAQ, in particular I wrote:
language = EN
inside the file Rconsole (in my installation it is C:\Program Files\R\R-2.15.2\etc\Rconsole); this works also for the command Rscript.
For example you can locate the Rconsole file with this two commands from a command prompt:
cd \
dir Rconsole /s
The first one make the root as the current directory, the second one looks for the Rconsole file.
In the following screenshot you have that Rconsole file is in the folder C:\Program Files\R\R-3.4.1\etc.
You may have more than one location, in that case you may edit all the Rconsole files.
After that you can open the Rconsole file with your favorite editor and look for the line language = and then append EN at the end of that line.
In the following screenshot the interesting line is the number 70 and you have to append EN at the end of it.
For mac users, I found this on the R for Mac FAQ
If you use a non-standard setup (e.g. different language than
formats), you can override the auto-detection performed by setting
`force.LANG' defaults setting, such as for example
defaults write org.R-project.R force.LANG en_US.UTF-8
when run in Terminal it will enforce US-english setting regardless of the system
setting. If you don't know what Terminal is you can use this R command
instead:
system("defaults write org.R-project.R force.LANG en_US.UTF-8")
but do not forget to quit R and start R.app again afterwards. Please
note that you must always use `.UTF-8' version of the locale,
otherwise R.app will not work properly.
This helped me to change my console language from Chinese to English.
This works from command line :
$ export LANG=en_US.UTF-8
None of the other answers above worked for me
If you use Ubuntu you will set
LANGUAGE=en
in /etc/R/Renviron.site.
You may also want to be aware of the difference between, for example, Sys.setenv(LANG = "ru") and Sys.setlocale(locale = "ru_RU.utf8").
> Sys.setlocale(locale = "ru_RU.utf8")
[1] "LC_CTYPE=ru_RU.utf8;LC_NUMERIC=C;LC_TIME=ru_RU.utf8;LC_COLLATE=ru_RU.utf8;LC_MONETARY=ru_RU.utf8;LC_MESSAGES=en_IE.utf8;LC_PAPER=en_IE.utf8;LC_NAME=en_IE.utf8;LC_ADDRESS=en_IE.utf8;LC_TELEPHONE=en_IE.utf8;LC_MEASUREMENT=en_IE.utf8;LC_IDENTIFICATION=en_IE.utf8"
If you are interested in changing the behaviour of functions that refer to one of these elements (e.g strptime to extract dates), you should use Sys.setlocale().
See ?Sys.setlocale for more details.
In order to see all available languages on a linux system, you can run
system("locale -a", intern = TRUE)
To permanently make it works, in both R and Rstudio (with Win 10),
one way to do this is to run the script every time automatically in the background initially.
No more changing the system language that influence the windows.
No more R only but fail in Rstudio. No more run a script every time manually. No more admin right but fail. No more short-cut setting but fail.
Step 1.
Use your system search, to find the file named "Rprofile"
My response is
C:\Program Files\R\R-4.0.5\library\base\R
C:\Program Files\R\R-4.0.5\etc
Step 2.
Edit C:\Program Files\R\R-4.0.5\library\base\R\Rprofile
The content:
This is the system Rprofile file. It is always run on startup.
Additional commands can be placed in site or user Rprofile files
(see ?Rprofile)
... and so on.
Step 3. Add Sys.setenv(LANGUAGE="en") at the end of the scrip
local({
Sys.setenv(LANGUAGE="en")
})
P.S. If you encounter the issue of authorization/saving,
move this file to desktop and replace the original file after editing.
type this first:
system("defaults write org.R-project.R force.LANG en_US.UTF-8")
then you will get a index number(in my case is 127)
then type:
Sys.setenv(LANG = "en")
then type the number and ENTER
127
For me worked:
Sys.setlocale("LC_MESSAGES", "en_US.utf8")
Testing:
> Sys.setlocale("LC_MESSAGES", "en_US.utf8")
[1] "en_US.utf8"
> x[3]
Error: object 'x' not found
Also working to get english messages:
Sys.setlocale("LC_MESSAGES", "C")
To reset to german messages I used
Sys.setlocale("LC_MESSAGES", "de_DE.utf8")
Here is the start of my sessionInfo:
> sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.2 LTS
A simple solution would be setting export Lang=C in your bash script.
I had a similar issue where the default language was german so it reverted back to english.
If you want to change R's language in terminal to English forever, this works fine for me in macOS:
Open terminal.app, and say:
touch .bash_profile
Then say:
open -a TextEdit.app .bash_profile
These two commands will help you open ".bash_profile" file in TextEdit.
Add this to ".bash_profile" file:
export LANG=en_US.UTF-8
Then save the file, reopen terminal and type R, you will find it's language has changed to english.
If you want language come back to it's original, just simply add a # before export LANG=en_US.UTF-8.
The only thing that worked for me was uninstalling R entirely (make sure to remove it from the Programs files as well), and install it, but unselect Message Translations during the installation process. When I installed R, and subsequently RCmdr, it finally came up in English.
In Ubuntu 14.04 LTS I had to remove the # from the comment #LANGUAGE=EN.
All other options din not work for me.
Change your current regional format to a different regional format in region settings on time&language settings in Windows by clicking on your time/date in lower right corner > adjust time/date > Region > change regional format to UK or US
This worked for me with a windows 10 laptop in German, where I wanted i.e. lubridate to return dates in English:
Sys.setlocale("LC_TIME", "English")
Im using R Studio on a Mac and I couldn't find the Rconsole file. So I took a more brutal way and just deleted the unwanted language files from the R app.
You just have to go to your Rapp in your application Folder, right click, show package content then /contents/Resources/. There are the language files e.g. English.lproj or in my case de.lproj wich I deleted. After restarting R, error messages appear in English..
Maybe thats helpful!
you simply have to change the basic language of microsoft on your computer!
press the windows button together with r, and tip the following code into the window that is opened
control.exe /name Microsoft.Language
load the language package you want to use and change the options. but take care, this will change also your keyboard layout!
on windows, when you have no admin right, just create a new program shortcut to Rgui.exe. Then in the properties of that shortcut, go to the 'Shortcut' tab and modify the target to include the system language of your choice, e.g. "C:\Program Files\R\R-3.5.3\bin\x64\Rgui.exe" LANGUAGE=en

R/RStudio: .libPaths() does not append to or overwrite the library tree

I need to set the library path in R for a single use (i.e. I do not want and can't set it in Rprofile.site and can't put the additional packages into the default library).
I tried to append to and to overwrite the library path with .libPaths() but neither worked; the path remained unchanged. See my attempts below.
Since I have seen examples that effectively are the same as my attempts, I have a feeling that it might be an authorisation issue: I simply have no rights to change the path.
I have searched stackoverflow, but the questions/solutions do not match my problem. Any suggestions? And no, turning it off and on again would not help.
My environment is Mac (High Sierra), R 3.4.3, RStudio 1.1.423, R newbie user (not root).
Thanks!
Original path
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library"
> .Library
[1] "/Library/Frameworks/R.framework/Resources/library"
Trying to append
> .libPaths( c( .libPaths(), "<some_other_valid_path>") )
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library"
> .Library
[1] "/Library/Frameworks/R.framework/Resources/library"
Trying to overwrite
> .libPaths("<some_other_valid_path>")
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library"
> .Library
[1] "/Library/Frameworks/R.framework/Resources/library"
I think you just confused yourself. Just add one directory to prepend:
R> .libPaths()
[1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"
[3] "/usr/lib/R/library"
R> .libPaths("/tmp")
R> .libPaths()
[1] "/tmp" "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library" "/usr/lib/R/library"
R>
The new one comes first and will therefore be used first for an installation, or a search via library() etc.
You cannot overwrite from an existing session---that is like cutting the tree branch you are sitting on.
But do see help(Startup). There are other files, even on a per-current-directory level, you could use.
Thanks for the feedback, Dirk! I was indeed confused about how .libPaths works, thanks for the clarification.
Based on your example, I realised that issue was that I had no access to the additional directory. .libPaths() internally calls dir.exists() which returned false due to non-accessibility and thus the new directory was not added to path. I just need to figure out how to provide access.

Directory not found when using Drat on a network drive

I have developed a package that I want to share with my colleagues at work.
I have a network drive in which I created the local repository structure that looks like this:
MyRepo
\__bin
\__windows
\__contrib
\__src
\__contrib
All folders are empty.
So I built my package with RStudio on Windows using the "Build/More/Build source package" menu, which created a tar.gz file.
Then I tried:
drat::insertPackage("../myPkg_0.0.0.9000.tar.gz",
repodir = "file://networkdrive/path/to/MyRepo",
action = "prune")
But this gives me an error:
Error: Directory file://networkdrive/path/to/MyRepo not found
Which is strange because file.exists(//networkdrive/path/to/MyRepo) returns true.
OK, then I tried:
drat::insertPackage("../myPkg_0.0.0.9000.tar.gz",
repodir = "//networkdrive/path/to/MyRepo",
action = "prune")
Without the file: in the repository path and I get another error:
tar (child): "//networkdrive/path/to/MyRepo/src/contrib/myPkg_0.0.0.9000.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
/usr/bin/tar: Child returned status 2
/usr/bin/tar: myPkg/DESCRIPTION: Not found in archive
/usr/bin/tar: Exiting with failure status due to previous errors
reading DESCRIPTION for package ‘myPkg’ failed with message:
cannot open the connection
But when I go in the "//networkdrive/path/to/MyRepo/src/contrib" folder, I can definitely see the myPkg_0.0.0.9000.tar.gz file that has been copied despite the error message.
Can anyone help?
> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252 LC_MONETARY=French_France.1252 LC_NUMERIC=C LC_TIME=French_France.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] drat_0.1.2 tools_3.3.3 git2r_0.18.0
I know this is old, but my colleague just came across the same problem and found this post. I believe the issue may be the lack of a trailing slash in your directory name. I have been able to recreate the error with a mapped network drive. I can resolve the issue by using "H:/MyRepo/" instead of "H:/MyRepo".
I haven't tried it with the "file://" format, but I wanted to include my answer in case someone else comes across this question.
Ok, so after some research, here is my conclusions.
It cannot be done
It's not Drat's fault
The reason why it does not work is that the tools::write_PACKAGES function does not work on network drives. Period.
I manually copied my package on the network drive, then ran setwd() to its location and executed write_PACKAGES(".", type="source") and I got the same error.
So to make this work, I just left my package.tar.gz file on a local drive, ran the tools::write_PACKAGES command locally and then moved the files to the network drive.
Adding the network drive to my repository list using options(repos = c(MyRepo = "file://networkdrive/path/to/MyRepo/")) works: RStudio and available.packages find my package.
It's not completely satisfactory, but I think it's the only way today.
I was having this problem as well and finally got to the bottom of it today.
For me, the problem was not isolated to just network locations but also occurred on C: drive. The root cause was the version of tar.exe being used to unpack the existing packages in the package directory. Calls to utils::untar are made in the tools::write_PACKAGES function.
The documentation for utils::untar explains that on Windows, external tar.exe is tried first. Sure enough, I had a version installed with Git which when used with default arguments fails when a colon is in the file name. I was able to force utils::untar to use to use the RBuildTools version of tar.exe instead by setting the environment variable TAR to "internal".
drat::insertPackage now works.

Setting default url for R to look for packages in Mac OS X

Mac64 R> install.packages("quantmod")
Yields a discouraging message. Namely,
Warning: unable to access index for repository http://www.ibiblio.org/pub/languages /R/CRAN/bin/macosx/leopard/contrib/2.12
Warning message:
In getDependencies(pkgs, dependencies, available, lib) :
package ‘quantmod’ is not available
No big deal, just pass in a different repo, like this:
Mac64 R> install.packages("quantmod", repo="http://R.research.att.com")
And all is well. But how do you set the default repo so you don't need to pass it in every time? In Ubuntu, there is a file that manages this but the documentation for Mac OS X is a bit sparse.
Can't tell if you are using the R64.app GUI but guessing that you may not be. You can set the default repository in the r/Preferences/ panels if you are using R64.app. Personally I use the repository at the same facility that hosts StatLib which shows up on some dialogs something like USA (PA 1) but on my Preferences pane is http://lib.stat.cmu.edu/R/CRAN . Here is a bit of ?options that may be of interest:
repos:
URLs of the repositories for use by update.packages. Defaults to
c(CRAN="#CRAN#"), a value that causes some utilities to prompt for
a CRAN mirror. To avoid this do set the CRAN mirror, by something
like local({r <- getOption("repos"); r["CRAN"] <- "http://my.local.cran";
options(repos=r)}).
Note that you can add more repositories (Bioconductor and Omegahat, notably)
using setRepositories().
At the moment my "other repository" setting in the the R64.app is http://www.stats.ox.ac.uk/pub/RWin/bin/macosx/leopard/contrib/2.12/ , because I was doing something to get a package that was only at Ripley's site but it is usually set to R--Forge: http://r-forge.r-project.org/
See the help for function setRepositories; you can manage your list of repositories in a GUI or by editing the file R_HOME/etc/repositories.
You can know what is your R_HOME directly in R:
> R.home()
[1] "/usr/lib/R"
http://cran.r-project.org/doc/manuals/R-admin.html#Managing-libraries
Try creating the folders and files as suggested for UNIX-like systems; chances are that R will check for them on OS X as well when looking for repo settings.

Resources