Installing EBImage on Windows 7 - r

I'm very new to R. I've tried to install the package EBImage
> source("http://bioconductor.org/biocLite.R")
> biocLite("EBImage")
and got the Warning:
installed directory not writable, cannot update packages 'foreign',
'lattice', 'Matrix', 'mgcv', 'nlme'
As I've tried to load an image
> f = system.file("images", "C:/aeskuimg/Patterns/Homogenious/h.jpg", package="EBImage")
> hom = readImage(f)
I've got the error:
Error in readImage(f) : Unable to determine type: Empty filename.
I suspect this is because of the prior warning.
Is there any solution there?

The warning can be ignored, it indicates that some packages were installed in a way that does not allow them to be updated, probably by an Administrator account whereas you're running as a regular user. To use an R package, load it first
library(EBImage)
Probably just
readImage("C:/aeskuimg/Patterns/Homogenious/h.jpg")
will work (R has tab completion, so no need to try too hard at typing the correct path; also file.choose() provides graphical navigation).
system.file() is used to specify a path relative to the location in which a package has been installed, and is typically used to reference an example file the package authors have distributed with the package. Probably after your command the value of f is character(0), which is R's way of saying that that the path you specified, relative to the installed location of the EBImage package, does not exist; system.file() has an argument mustWork=TRUE that will generate an error if the file path is incorrect.

Related

Having issues installing an R package from Github

Please be patient with me as I'm a total noob, but I'm really trying to learn.
I'm trying to make a choropleth map for my country, and found an R package on Github that handles it excellently. However, I'm working on a university computer and I don't have write privileges on any drive but M://, so whenever the package tries to install on C:// it obviously throws an error. This hasn't been a problem since I can just specify a libpath as an argument on install.packages, but devtools::install_github does not seem to have such an argument.
I tried using
with_libpaths(new = "M:\R\win-library\3.2", install_github('diegovalle/mxmaps'))
But I got an error message saying
with_libpaths' is deprecated. Use 'withr::with_libpaths' instead.
I take this to mean that I need to install the "withr" package in order to use that? However, I keep getting errors when trying to install that package. First, I got
Warning in install.packages : installation of package ‘withr’ had
non-zero exit status
because of the not having access to C:// issue. I usually bypass this by installing directly from the binaries, but when I try that it tells me
"Warning in install.packages : package ‘withr’ is not available (for R
version 3.2.2)".
Other than updating my version of R (which will be a nighmare since I don't have installation privileges on this machine), how else can I either install withr or find another way to specify the directory to install the package from github?
I would suggest you to go with latest version of 3.4.All above mentioned packages are available under latest version.
Two ways to set local library paths (at least on Linux running R 3.5.3):
(1) At the beginning of your script, set the the .libPaths option to your local library path, i.e.: .libPaths("M:\R\win-library\3.2")
(2) Add to your .Renviron file a line that specifies your local library path: i.e.: R_LIBS="M:\R\win-library\3.2"
Note: For (1) You will need to manually run every time you start a new R session, whereas (2) will be set automatically when R starts.

Install r libraries

I have installed an R project on a network. It automatically installs the library in the C:\Users\\AppData\Local\Temp\downloaded_packages, however, I would like to install the library to lets say Q:\Apps\R-Project\Libraries.
I managed to install the library to the Q network using the following command:
install.packages("dplyr", lib="Q:\Apps\R-Project\Libraries", dependencies=T)
When I load the library it says that it cannot find the Rccp.
Any help how to solve that issue?
You told R to install the package in a certain location, namely Q:\Apps\R-Project\Libraries.
When you tell R to use a certain package, R will not search your entire computer whether that package exists. Usually, the packages are being saved in a standard location that R knows and where R will also search for it, once you tell it to use that package. You can view these locations with .libPaths().
If Q:\Apps\R-Project\Libraries is not a location that you have saved in .libPaths(), you have two options:
# 1) Add it to `.libPaths()` like this:
.libPaths( c( .libPaths(), "Q:\Apps\R-Project\Libraries") )
# 2) Tell `R` explicitly where to look while loading the package:
library(packagename, lib.loc = "Q:\Apps\R-Project\Libraries")
I recommend using option 1

Manually Downloading and Installing Packages in R

I am currently trying to run some R code on a computing cluster but cannot run the install.packages function due to some weird firewall settings on my cluster. Since I am only using a few packages in my R code, I was hoping to avoid using the install.packages function by downloading and installing the packages manually.
Note: I am aware that there is a way to avoid this issue by using an HTTP proxy as described in the R FAQ. Unfortunately the people in charge of my cluster are not being helpful in setting this up so I'm forced to consider this alternative approach.
Ideally, I would like to download the packages files from CRAN to my computer, then upload these files to the cluster and install them using the appropriate commands in R. In addition, I would also like to make sure that the packages are installed to a location of my choice since I do not have the permission to "write" in the default R directory (I believe that I can do this within R by using the .libPaths function)
Lastly, the computers that I am working with on the cluster are Unix x86_64.
You can install the package manually using the following command
install.packages('package.zip', lib='destination_directory',repos = NULL)
See the help of ?install.packages, for further description
I also went through the same problem while installing the caret package. There are many dependencies of the caret package.
So, I did the following:
install.packages('caret'):
This gives all packages in zip format the location of download is shown in the error message. Unzip all packages from download source to a location for example in C:/PublicData/RawRPackages, then run following command.
foldername <- 'C:/PublicData/RawRPackages'
install.packages(paste(foldername, 'caret', sep = '/'),
repos = NULL, type = "source")
library(caret, lib.loc = foldername)
this the better way, if we want to download and install locally :
download.packages('lib_name',destdir='dest_path')
for example :
download.packages('RJDBC',destdir='d:/rlibs')
install.packages("libname",lib = "file://F:/test")

Possible to override the blocking of a package's (re-)installation after it has been required/loaded?

Actual question
Is it possible to override the blocking of a package's (re-)installation after it has been required/loaded?
I understand that blocking a "true" re-installation to the same library that the package was loaded from makes perfect sense once the package is in use. But my use case is a bit different
Background
I like the idea of having a "sandbox library" to test own packages. Besides the package(s) to test, those sandbox libraries contain all base packages of a clean base R installation plus some contrib packages necessary in order for my whole package building and testing framework to work (e.g. digest, stringr etc.). However, my framework loads the latter packages from the standard lib and then needs to install them to the sandbox lib - which is blocked. Hence my question if it is possible to override this as I'm not really doing what people had in mind when implementing the block.
Illustration
Specify paths to the two libraries
lib <- file.path(R.home(), "library")
lib.sandbox <- file.path(tempdir(), "library")
Create sandbox library
dir.create(lib.sandbox, showWarnings=FALSE)
Install a package to the standard library and load it
install.packages("digest", lib=lib)
require("digest", lib.loc=lib)
Then also install it to the sandbox library
> install.packages("digest", lib=lib.sandbox)
Warning: package 'digest' is in use and will not be installed
My framework figures certain stuff out after certain packages that should also be installed to the sandbox lib are loaded, so I can't put the "install-to-sandbox-lib" step before the initial loading step.
Here's a general version of what you suggest in your answer. This will unload the package before installing and reload the package from the same location afterward.
install.packages.sandbox <- function(pkgs, lib, repos=getOption("repos"), ...) {
if (is.null(repos))
stop("Can't install from source. Need package name.")
pkg.pos <- grep(pkgs, search())
pkg.path <- searchpaths()[grep(pkgs, searchpaths())]
in.use <- length(pkg.pos) > 0
# detach
if (in.use) do.call(detach,
list(pkg.pos),
envir=.GlobalEnv)
# install
utils::install.packages(pkgs, lib, repos, ...)
# re-attach from original library location
if (in.use) library(pkgs,
character.only=TRUE,
lib.loc=.libPaths()[sapply(.libPaths(), grepl, pkg.path)])
}
Not what I was after conceptually, but a pragmatic workaround: detaching the package right before installing it
Specify paths to the two libraries
lib <- file.path(R.home(), "library")
lib.sandbox <- file.path(tempdir(), "library")
Create sandbox library
dir.create(lib.sandbox, showWarnings=FALSE)
Install a package to the standard library and load it
install.packages("digest", lib=lib)
require("digest", lib.loc=lib)
Detach package
detach(package:digest)
Then also install it to the sandbox library
> install.packages("digest", lib=lib.sandbox)
trying URL 'http://cran.at.r-project.org/bin/windows/contrib/2.15/digest_0.5.2.zip'
Content type 'application/zip' length 79053 bytes (77 Kb)
opened URL
downloaded 77 Kb
package 'digest' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\wwa418\AppData\Local\Temp\Rtmp6XSVYq\downloaded_packages
On top of the already mentioned search path altering approach, it may be worthwhile noting that this warning and behavior comes (at least as of R 3.6.2) from utils:::.install.winbinary, where the check against attached packages is made.
What we therefore can do alternatively to circumvent this issue is to instead of using the binary installation method, install the package from source:
library(digest)
utils::install.packages("digest", lib = tempdir(), type = "source")
This of course comes with some potential caveats if the package requires compilation of C/C++/Fortran code, as written in help("install.packages") for Windows:
Using packages with type = "source" always works provided the package
contains no C/C++/Fortran code that needs compilation. Otherwise, you
will need to have installed the Rtools collection as described in the
‘R for Windows FAQ’ and you must have the PATH environment variable
set up as required by Rtools.
...

error with R CMD check because of package dependency

Background
I am creating a newpackage that depends on oldpackage, and have indicated this dependency in the file newpackage/DESCRIPTION.
Furthermore,
oldpackage is installed in the directory, ~/lib/R
my .Rprofile includes .libPaths("~/lib/R")
hence, I can successfully load oldpackage without specifying the library location, e.g., using the command library(oldpackage) in R
Despite the ability to load the package without having its library specified, R CMD check newpackage gives an error indicating that it can not fine oldpackage:
checking whether the package can be loaded ... ERROR
Loading required package: oldpackage
Error: package 'oldpackage' could not be loaded
In addition: Warning message:
In library(pkg, character.only = TRUE, logical.return = TRUE, lib.loc = lib.loc) :
there is no package called 'oldpackage'
Execution halted
It looks like this package has a loading problem: see the messages for
details.
Questions:
Why is R unable to find the package?
Can I specify the library location in the DESCRIPTION file?
Regarding question 1), it is both a FAQ and yet somewhat annoying. R CMD check runs in vanilla mode, so it will not find user-level libraries. As I recall, setting R_LIBS="...." in the call helps, so try that.
Regarding question 2), no you cannot give a location in DESCRIPTION. Which makes sense as that file needs to work 'everywhere' whereas your location info is local to your machine.

Resources