Ubuntu remove .libPaths() in R - r

After I installed RStudio, I ran:
library()
Warning message:
libraries '/usr/local/lib/R/site-library', 'usr/lib/R/site-library' contain no packages
Then I input:
.libPaths()
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[2] "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library"
[4] "/usr/lib/R/library"
How can I remove [2] and [3] to prevent the warning message appear again?
Expected output:
.libPaths()
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[4] "/usr/lib/R/library"

Linux
First thing to do is read the manpage on it (?.libPaths), and you'll see:
'.libPaths' is used for getting or setting the library trees that R knows about (and hence uses when looking for packages). If called with argument ‘new’, the library search path is set to the existing directories in unique(c(new, .Library.site, .Library)) and this is returned. If given no argument, a character vector with the currently active library trees is returned.
(emphasis added). This should clue us in to wonder what .Library.site holds. Oddly enough, it holds system-wide (ergo "site") library paths that "should" always be kept, so they are always maintained.
It further goes on to say:
'.Library.site' can be set via the environment variable 'R_LIBS_SITE' (as a non-empty semicolon-separated list of library trees).
So one way to fix it is to give it an empty string when you start R (cannot be done from within R):
# in bash on the command line:
$ R_LIBS_SITE=" " R
# in R
R> .libPaths()
[1] "/usr/lib/R/library"
The way to get this to work with RStudio is by creating a ~/.Renviron file with at least the following:
R_LIBS_SITE=" "
That done, you should not have to do anything further to remove the secondary site library paths from .libPaths():
R> .libPaths()
[1] "/usr/lib/R/library"
Windows
Assuming you're doing the following:
R> .libPaths(c("/home/avalon/R/x86_64-pc-linux-gun-library/3.2", .libPaths()))
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[2] "/usr/local/lib/R/site-library"
[3] "/usr/lib/R/site-library"
[4] "/usr/lib/R/library"
If you want to correct it after you've done this, then just do:
R> .libPaths( c(.libPaths()[c(1,4)]) )
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[2] "/usr/lib/R/library"
Alternatively, you can do it this way the first time (i.e., while it still has three elements, two of which are not good for you):
R> .libPaths(c("/home/avalon/R/x86_64-pc-linux-gun-library/3.2", .libPaths()[3]))
[1] "/home/avalon/R/x86_64-pc-linux-gun-library/3.2"
[2] "/usr/lib/R/library"
There is certainly a way to filter the paths programmatically instead of blindly taking the 3rd element, but this should work for now.

Related

How to get the Iris Data set to plot?

Just doing the basic data set from the examples in the R-Graph-gallery, not sure why I can't get anything to plot correctly.
I searched around and some posts said to install 'extrafonts' but that also isn't possible:
PackagesNotFoundError: The following packages are not available from current channels:
- r-extrafonts
iris plot error:
Using theme_ipsum from hrbrthemes package
You perhaps want to figure out which of your Windows fonts are not present or present in a location that R is not expecting. Here's how to find the installed and accessible PDF fonts known to R. I'm working with PDF fonts because you mentioned something that might be a reference to an R package named "extrafont". There is no CRAN -sited package named 'r-extrafonts'.
names(pdfFonts())
[1] "serif" "sans" "mono" "AvantGarde"
[5] "Bookman" "Courier" "Helvetica" "Helvetica-Narrow"
[9] "NewCenturySchoolbook" "Palatino" "Times" "URWGothic"
[13] "URWBookman" "NimbusMon" "NimbusSan" "URWHelvetica"
[17] "NimbusSanCond" "CenturySch" "URWPalladio" "NimbusRom"
[21] "URWTimes" "ArialMT" "Japan1" "Japan1HeiMin"
[25] "Japan1GothicBBB" "Japan1Ryumin" "Korea1" "Korea1deb"
[29] "CNS1" "GB1"
I'm on a Linux box so yours will undoubtedly be different. The first three will be the defaults. You can examine it more carefully to see which font is likely to be askew or missing:
pdfFonts()[['serif']]$family
[1] "Times"
pdfFonts()[['serif']]
$family
[1] "Times"
$metrics
[1] "Times-Roman.afm" "Times-Bold.afm" "Times-Italic.afm" "Times-BoldItalic.afm"
[5] "Symbol.afm"
$encoding
[1] "default"
attr(,"class")
[1] "Type1Font"

Find all packages that depend on a specific one

Is it possible to scan all the packages on CRAN in order to find all those that are dependent on a specific package?
I have found out how to do that but only on the packages I have installed and that is not what I want.
If you don't care about doing it programmatically, the easiest way is to look at the corresponding CRAN web page (i.e. https://CRAN.R-project.org/package=PKGNAME) and look at the "Reverse depends:", "Reverse imports:", and "Reverse suggests:" sections.
You can do (with a package randomly picked from CRAN):
tools::package_dependencies(package = "ABHgenotypeR")
$ABHgenotypeR
[1] "ggplot2" "reshape2" "utils"
If you need the reverse dependencies, you can do:
tools::package_dependencies(package = "ABHgenotypeR",
reverse = TRUE)
$ABHgenotypeR
character(0)
From documentation:
For given packages which are not found in the db, NULL entries are
returned, as opposed to character(0) entries which indicate no
dependencies.
And you can do it for multiple packages simultaneously:
packages <- c("ABHgenotypeR", "accelerometry", "ACNE")
tools::package_dependencies(package = packages)
$ABHgenotypeR
[1] "ggplot2" "reshape2" "utils"
$accelerometry
[1] "Rcpp" "dvmisc"
$ACNE
[1] "aroma.affymetrix" "MASS" "R.methodsS3" "R.oo" "R.utils"
[6] "matrixStats" "R.filesets" "aroma.core"

R: Evaluating a script in an environment

I would like to load a library function within a script evaluated in a specified environment.
Example:
## foo.R
## -----
## blah blah
library(extrafont)
loadfonts()
Assuming for convenience the evaluation environment is the base environment:
sys.source("foo.R")
## Registering fonts with R
## Error in eval(expr, envir, enclos) : could not find function "loadfonts"
Replacing loadfonts() with extrafont:::loadfonts() works better, but still gives:
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'pdfFonts' of mode 'function' was not found
because loadfonts() requires pdfFonts() defined in grDevices.
This is both a not totally satisfactory answer and a long comment to #waterling.
The proposed solution is:
e<- new.env()
source("foo.R", local=e)
i.e.
source("foo.R", local=new.env())
which is substantially equivalent to:
sys.source("foo.R", envir=new.env())
It works for much the same reason why:
sys.source("foo.R", envir=as.environment("package:grDevices"))
As reported in the error (see question), the function not found, pdfFonts() is part of the package grDevices The sys.source above executes the script in the package:grDevices environment, hence the function is found. Instead by default sys.source(..., envir=baseenv()) and the base environment comes before grDevices, therefore pdfFonts() is not found.
A first problem is that I do not know in advance which functions will happen to be in my script.
In this case setting envir=new.env() is a more general approach. By default
new.env(parent=parent.frame()),
therefore it has the same parent of sys.source(), which is the global environment. So everything visible in the global environment is visible in the script with sys.source(..., envir=new.env()), that is every object created by the user and by the user loaded packages.
The problem here is that we are not insulating the script any more, which makes it less reproducible and stable. In fact, it depends on what is in R memory in the very moment we call sys.source.
To make the things more practical, it means foo.R might work just because we usually call it after bar.R.
A second problem is that this not an actual solution.
The question concerns how to run a script foo.R in an environment e and still access, when needed, to functions not belonging to e. Taking an e that (directly or through its parents) has access to these functions is actually a workaround, not a solution.
If this type of workaround is the only possible way to go, IMHO, the best is to make it dependent only on standard R packages.
At start, R shows:
search()
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
that is eight official packages/environments.
New packages/environments, unless explicitly changing the default, go into the second slot and all those after the first one shift one position.
myEnv=new.env()
attach(myEnv)
search()
## [1] ".GlobalEnv" "myEnv" "package:stats"
## [4] "package:graphics" "package:grDevices" "package:utils"
## [7] "package:datasets" "package:methods" "Autoloads"
## [10] "package:base"
So we can take the last eight in the search path, which means taking the first of these eight inheriting the others. We need:
pos.to.env(length(search()) - 7)
## <environment: package:stats>
## attr(,"name")
## [1] "package:stats"
## attr(,"path")
## [1] "path/to//R/R-x.x.x/library/stats"
Therefore:
sys.source("foo.R", envir=new.env(parent=pos.to.env(length(search()) - 7)))
or one can take a standard R reference package, say stats, and its parents.
Therefore:
sys.source("foo.R", envir=new.env(parent=as.environment("package:stats")))
UPDATE
I found the
SOLUTION
As for the script:
#foo.R
#-----
library(extrafont)
f=function() loadfonts()
environment(f) = as.environment("package:extrafont")
f()
To execute in a new environment:
sys.source("foo.R", envir=new.env(parent=baseenv()))
f() now has access to all objects in the package extrafont and those loaded before it.
In sys.source() creating a new.env() with whatever parent is necessary to make environment() assignment work.

opening gzipped file that is saved as .R

I am opening a large (347M) .R file. In Rstudio with R3.1.2
After mucking around with various input functions, I eventually got R to return this:
file("/Users/vincentlaufer/Desktop/all.t.subsets.R")
description class mode
"/Users/vincentlaufer/Desktop/all.t.subsets.R" "gzfile" "rt"
text opened can read
"text" "closed" "yes"
can write
"yes"
So, I tried working on opening it in the following way:
splat <- scan(gzfile("/Users/vincentlaufer/Desktop/all.t.subsets.R"), what="Factor")
(I chose Factor because Rstudio told me that "RDX2" is a Factor. I don't know what RDX2 is because I cannot open the file, probably a gene name..)
displaying the variable I created, splat, returns the following
> splat
[1] "RDX2"
[2] "X"
[3] ""
[4] "\002"
[5] ""
[6] "\025beta.combat.x.th1th17"
[7] "\033q?\xd9\xce\a_o\xd2"
[8] "?\xe0\x85\x87\x93\u0757\xf6?\xe1&\027\xc1\xbd\xa5\022?\xec\037!-w1\x90?\xed|\xed\x91hr\xb0?\xe0-\xe0"
[9] "\033qv?\xc9\xf2\022\xd7s\030\xfc?\xe8\xa3\xd7"
[10] "=p\xa4?\xe1\xdc\xc6?\024\022\006?\xee>BZ\xeec"
[11] "?\xee\027\xc1\xbd\xa5\021\x9d?\xe0\xb0\xf2{\xb2\xfe\xc8?\xe7"
I am a bit out of depth here. Can anyone tell me how to open this .R file.
The file is a binary files saved by R. (You can tell because of the RDX2 entry which says what kind of file it is - see http://biostat.mc.vanderbilt.edu/wiki/Main/RBinaryFormat)
You should try loading it using load("/Users/vincentlaufer/Desktop/all.t.subsets.R")

Remove a library from .libPaths() permanently without Rprofile.site

How can I permanently remove a library in R?
.libPaths()
[1] "\\\\per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
[2] "C:/Program Files/R/R-2.15.2/library"
[3] "C:/Program Files/RStudio/R/library"
The first item is my corporate "My Documents" folder, and the apostrophe in the path from my surname is causing all kinds of grief when using R CMD INSTALL --build on a package I'm making, not to mention issues using packages installed there when I'm offline from the network.
I want to use C:/Program Files/R/R-2.15.2/library as the default instead, but I don't want to have to rely on an Rprofile.site.
What I've tried
> .libPaths(.libPaths()[2:3])
> .libPaths()
[1] "C:/Program Files/R/R-2.15.2/library" "C:/Program Files/RStudio/R/library"
That seems to work, but only until I restart my R session, and then I'm back to the original .libPaths() output...
Restarting R session...
> .libPaths()
[1] "\\\\per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
[2] "C:/Program Files/R/R-2.15.2/library"
[3] "C:/Program Files/RStudio/R/library"
I thought maybe .libPaths() was using R_LIBS_USER
> Sys.getenv("R_LIBS_USER")
[1] "//per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
So I've tried to unset it using Sys.unsetenv("R_LIBS_USER") but it doesn't persist between sessions.
Additional Info
If it matters, here are some environment variables that might be relevant...
> Sys.getenv("R_HOME")
[1] "C:/PROGRA~1/R/R-215~1.2"
> Sys.getenv("R_HOME")
[1] "C:/PROGRA~1/R/R-215~1.2"
> Sys.getenv("R_USER")
[1] "//per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell"
> Sys.getenv("R_LIBS_USER")
[1] "//per-homedrive1.corp.riotinto.org/homedrive$/Tommy.O'Dell/R/win-library/2.15"
> Sys.getenv("R_LIBS_SITE")
[1] ""
I've tried Sys.unsetenv("R_LIBS_USER") but this also doesn't stick between sessions
Just set the environment variable R_LIBS in Windows to something like
R_LIBS=C:/Program Files/R/R-2.15.2/library
Restart R.
This is bit late response to the question, but might be useful for others.
I order to set up my own path (and remove one of the original ones) I have:
used .libPaths() inside R to check current library paths;
identified which paths to keep. In my case, it kept R's original library but removed link to my documents.
found R-Home path using R.home() or Sys.getenv("R_HOME");
R-Home\R-3.2.2\etc\Rprofile.site is read every time R kernel starts. Therefore, any modification will be persistent to every run of R.
Edited Rprofile.site by adding the following,
.libPaths(.libPaths()[2])
.libPaths("d:/tmp/R/win-library/3.2")
How it works?
First line remove all but one path (second from the original list), the second line adds an additional path. We end up with two paths.
note that I use Unix path notation despite using windows. R always use Unix notation, regardless of operating system
restarted R (using Ctr+Shift+F10)
This will work every time now.
Use this function in .Rprofile
set_lib_paths <- function(lib_vec) {
lib_vec <- normalizePath(lib_vec, mustWork = TRUE)
shim_fun <- .libPaths
shim_env <- new.env(parent = environment(shim_fun))
shim_env$.Library <- character()
shim_env$.Library.site <- character()
environment(shim_fun) <- shim_env
shim_fun(lib_vec)
}
set_lib_paths("~/code/library") # where "~/code/library" is your package directory
Original Source: https://milesmcbain.xyz/hacking-r-library-paths/
I have put the Sys.unsetenv("R_LIBS_USER") command in a .Rprofile file in my windows "own documents" folder. Seems to help. My problem was that being in an active directory environment made R upstart and package loading incredibly slow when connected via vpn.
If you want to do this at RProfile file (#library/base/R/), you can search the lines where R_LIBS_* environment variables are set (for e.g. Sys.setenv(R_LIBS_SITE=....) and Sys.setenv(R_LIBS_USER=.....))
You can also search the code .libPaths(), which sets the library tree. So you can achieve your goal by a combination of commenting, unsetting and setting the R_LIBS variables before the .libPaths() call as you wish. For e.g. Something like:
Sys.unsetenv("R_LIBS")
Sys.unsetenv("R_LIBS_USER")
Sys.setenv(R_LIBS_SITE = "D:/R/libs/site")
Sys.setenv(R_LIBS_USER = "D:/R/libs/user")
Sys.setenv(R_LIBS = "D:/R/libs")

Resources