Loading packages installed from a local .zip file - r

I am trying to load a self-written package in order to use the functions I created there without having to copy and paste them into each new project. The program is having difficulties to recognize the package though. These are the lines I am inputting and the messages issued by the system.
> .libPaths(c(.libPaths(), "path-to-package"))
> install.packages("somepath/myPackage.zip", repos = NULL, lib = "path-to-package")
> library("myPackage", lib.loc = "path-to-package")
Error in library("myPackage", lib.loc = "path-to-package") :
‘myPackage’ ist keine gültiges installiertes Paket
After issuing "install.packages", new directories and files are displayed in Microsoft Explorer. While other files vanish at the end, the directory named "myPackage" remains.
Would it be easier to try and get R do my bidding and make it recognize myPackage as a package not only when installnig, but also when loading it? Or is it easier to copy and paste the functions from the package when I need them?
I do own the book "R Packages" by Hadley Wickham in its first edition from 2015. Are there any passages I might want to reread to get going?

Related

How to install R packages into Azure Machine Learning

I have trained a model locally using the R package locfit. I am now trying to run this in Azure Machine Learning.
Most guides/previous questions appear to be in relation to Azure Machine Learning (classic). Although I believe the process outlined in similar posts will be similar (e.g. here, here, I am still unable to get it to work.
I have outlined the steps I have followed below:
Download locfit R package for windows Zip file from here
Put this downloaded Zip file into a new Zip file entitled "locfit_package"
I upload this "locfit_package" zip folder to AML as a dataset (Create Dataset > From Local Files > name: locfit_package dataset type: file > Upload the zip ("locfit_package") > Confirm upload is correct
In the R terminal I then execute the following code:
install.packages("src/locfit_package.zip", lib = ".", repos = NULL, verbose = TRUE)
library(locfit_package, lib.loc=".", verbose=TRUE)
library(locfit)
The following error message is then returned:
system (cmd0): /usr/lib/R/bin/R CMD INSTALL
Warning: invalid package ‘src/locfit_package.zip’
Error: ERROR: no packages specified
Warning message:
In install.packages("src/locfit_package.zip", lib = ".", repos = NULL, : installation of package ‘src/locfit_package.zip’ had non-zero exit status
Error in library(locfit_package, lib.loc = ".", verbose = TRUE) : there is no package called ‘locfit_package’
Execution halted
I just checked on the document, it says:"Excute R Script module does not support installing packages that require native compilation, like qdap package which requires JAVA and drc package which requires C++. This is because this module is executed in a pre-installed environment with non-admin permission. Do not install packages which are pre-built on/for Windows, since the designer modules are running on Ubuntu. To check whether a package is pre-built on windows, you could go to CRAN and search your package, download one binary file according to your OS, and check Built: part in the DESCRIPTION file. Following is an example:
And the sample code:
# R version: 3.5.1
# The script MUST contain a function named azureml_main,
# which is the entry point for this module.
# Note that functions dependent on the X11 library,
# such as "View," are not supported because the X11 library
# is not preinstalled.
# The entry point function MUST have two input arguments.
# If the input port is not connected, the corresponding
# dataframe argument will be null.
# Param<dataframe1>: a R DataFrame
# Param<dataframe2>: a R DataFrame
azureml_main <- function(dataframe1, dataframe2){
print("R script run.")
if(!require(zoo)) install.packages("zoo",repos = "https://cloud.r-project.org")
library(zoo)
# Return datasets as a Named List
return(list(dataset1=dataframe1, dataset2=dataframe2))
}
Could you please check on if your package is on this?
Reference document: https://learn.microsoft.com/en-us/azure/machine-learning/algorithm-module-reference/execute-r-script

How do i keep source files when using R's devtools library function 'install'

I am trying to build an R package (DESeq2) from source so that I can debug it. I've installed all the dependencies required and I'm following Hillary Parker's instructions for creating R packages. I'm running this on CentOS 6.6 using R-3.4.2.
I run :
library("devtools")
install("DESeq2", keep_source=TRUE)
It installs it in the directory with all my other R libraries. When I look at the installed DESeq2 library it is missing all the DESeq2/R/*.R and DESeq2/src/*.cpp files.
QUESTION : Where are these files and why didn't they get installed? This does not seem like the expected behavior.
R uses binary database format for installed packages to pack the objects into a database-alike file format for efficiency reasons (lazy loading). These database files (*.rdb and *.rdx) are stored in the R sub folder of the package installation path (see ?lazyLoad).
Even if
you are looking at the right place to find the installed package (use .libPaths() in R to find the installation folder)
and you have installed the package with the source code (like you did or
via install.packages("a_CRAN_package", INSTALL_opts = "--with-keep.source"))
you will not find R files in R folder there.
You can verify that the source code is available by picking one function name from the package and print it on the console. If you can see the source code (with comments) the package sources (R files) are available:
print(DeSeq2::any_function)
To make the source code available for debugging and stack traces you can set the option keep.source.pkgs = TRUE (see ?options) in your .Rprofile file or via an environment variable:
keep.source.pkgs:
As for keep.source, used only when packages are
installed. Defaults to FALSE unless the environment variable
R_KEEP_PKG_SOURCE is set to yes.
Note: The source code is available then only for newly installed and updated packages (not for already installed packages!).
For more details see: https://yetanothermathprogrammingconsultant.blogspot.de/2016/02/r-lazy-load-db-files.html

R: Error in install.packages : cannot open the connection

I was trying to install the package RINDSEL but I am unable to install it and I keep getting the following error:
Error in install.packages : cannot open the connection
I downloaded the package from:
rindsel_1.0_2.zip | Integrated Breeding Platform
and loaded it from the directory. Other packages from the directory can be installed but just not this one.
Is the package corrupt or could there be any other error?
I would really be grateful for any help. Thanks in advance
Rename the zip file RinSel Software into Rindsel. That's the name specified in the discription file.
Then, you can install the package in R with the command
install.packages("C:/path/to/Rindsel.zip",repos=NULL,type="win.binary")
That works fine... at first (!!!).
Problem with the Rindsel package is. It is quite old. It was build with R 2.13.1. Therefore, if you want to load the library which would be the next step to use the package in R you will get the error:
Problems building package (Error: "package has been build before R-3.0.0")
My suggestion: Contact the authors of the package and ask them if they can either provide the source file that you can build the package by yourself or if they can bundle the Rindsel package with a newer R version.
(Or you could try to hunt down an old R version and see if you can get the thing running with an old R... However, I would not seriously suggest to do that. It would probably result in conflicting dependencies with the other required packages...)
EDIT 15-02-2018: OP asked if one can build an R package with sources that are presumeably the Rindsel source files.
Yes, basically, you could do that. You would have to make a your own description and namespace file and put the source file in the R folder than invoke the command in R to build it....
But it's not neccessary with the script files provided by the link the OP posted.
OP, just run the scripts in R! It's quite easy.
Download the zip-file and extract it on your machine.
Go to that directory. The R command would be
setwd('path/to/your/directory')
Than run the R script, e.g, the KNIndex.r. It's simple:
source('KNIndex.r')
Then the script will run and produce some output / prompts.
For future readers,
I was able to fix the error by running RStudio with administrative privileges to get the command to work.
If that does not fix it, you might wish to try
Installing "r tools" if that is not installed already. That can be downloaded from
https://cran.r-project.org/bin/windows/Rtools/
Download a relevant package that you are trying to install (e.g., tidyverse_1.3.0.zip) from https://cran.rstudio.com/
and install that from local path
It can also be installed directly from the web using install.packages("https://cran.rstudio.com/bin/windows/contrib/4.0/tidyverse_1.3.0.zip")
I had the same problem. R was not able to extract and compile the package files to the default installation directory for some system-specific reasons (not R related).
I was able to fix this by specifying the installation directory of the package lib using:
install.packages("your package", lib = 'path/to your/required/installation/directory')
You can then load the package by specifying the lib.loc option while loading it:
library('your package', lib.loc='path/to your/required/installation/directory')
A better solution:
Create a new environment variable (if you are using windows) R_LIBS_USER with the following directory path/to your/required/installation/directory.
This will change the default installation directory of the packages and make it easier to load and install them without specifying the location everytime.

Error installing .zip file with R package or library

I am trying to install the R package MixeR 2003 by Batagelj and Bren . This is not in CRAN, but is accessible on this website as a downloadable zip file containing an R file (MixeR.R), a dataset (Glac.dat) and 3 pdf's.
I downloaded the zip file, and placed it in my R directory. After that, I attempted to install it from within RStudio using the drop-down menus Tools > Install Packages > Package Archive File (.zip; .tar.gz) > MixeR.zip, getting the following error message:
install.packages("~/R/MixeR.zip", repos = NULL, type = "win.binary")
Installing package into ‘C:/Users/Documents/R/win-library/3.3’
(as ‘lib’ is unspecified)
Warning in install.packages :
cannot open compressed file 'MixeR/DESCRIPTION', probable reason 'No such file or directory'
Error in install.packages : cannot open the connection
What am I doing wrong, and how can I install this package?
It may very well be that it is actually not a package, but rather a 'library', in which case I would appreciate guidance as to how to make the functions and datasets in it accessible to the R session.
If you download and extract you'll see that that it's some pdfs, a .dat file, and a single R script that contains a bunch of functions.
Likely you just want to source the script so you want to extract the zip archive and then use source on MixeR.R
source("path/to/MixeRArchive/MixeR.R")
Alternatively they do host that file on their website directly so you could just read it from the internet directly
source("http://vlado.fmf.uni-lj.si/pub/MixeR/MixeR.R")
Either way will read the functions directly into your global environment since it isn't an actual package with its own namespace.

Did the subdirectory structure of package repositories change as of R 2.15.2?

Kind of embarrassing / a no-go, but since it hasn't been that long that I've moved from "pure user" to "beginner-developer", I've never actually read the CHANGELOG when a new R version came out - well until today (and I have the feeling I should make this a habbit) ;-)
Yet I'm not sure if the supposed change actually occurred since I couldn't find anything about it at a first glimpse at the CHANGELOG of R R 2.15.2:
Actual question
Is it possible that the (subdirectory) structure of package repositories changed from
./bin/windows/contrib/2.xx/
to
./src/contrib/2.xx/ or even ./src/contrib?
Or at least that the PACKAGES file now needs to live here: ./src/contrib/PACKAGES?
Background info
Up to version 2.15.1, the following path worked to install packages from my local package repository:
path.repos <- "L:/R/packages"
repos <- file.path("file://", path.repos)
Function contrib.url would take repos and expand it to the right subdirectory:
> contrib.url(repos)
[1] "file:///L:/R/packages/bin/windows/contrib/2.15"
But when I try to run install.packages(), I get the following error for R 2.15.2:
> install.packages("mypkg",
+ lib=file.path(R.home(), "library"),
+ repos=repos,
+ type="win.binary"
+ )
Error in read.dcf(file = tmpf) : cannot open the connection
In addition: Warning message:
In read.dcf(file = tmpf) :
cannot open compressed file 'L:/R/packages/src/contrib/PACKAGES', probable reason 'No such file or directory'
>
When I do the same with R 2.15.1, everything works smoothly.
Due dilligence
There are some references with respect to repositories in the CHANGELOG, but the only section I found that gives me some evidence that the supposed change occurred is this:
PACKAGE INSTALLATION
For a Windows or Mac OS X binary package install, install.packages() will check if a source package is available on the same repositories, and report if it is a later version or there is a source package but no binary package available.
Just had a look at the official documentation again and got the idea that maybe arg type is not passed along to contrib.url() correctly as it seems to me install.package() is looking at the place for type="source" packages?
This particular bug isn't the same as mine.
This issue relates to checks install.packages() now runs before installing. As Rappster said, it tries to find a source package to compare the binary version with:
For a Windows or Mac OS X binary package install, install.packages() will check if a source package is available on the same repositories, and report if it is a later version or there is a source package but no binary package available.
So a simple way of squashing this message is creating the R/src/contrib directory and running tools::write_PACKAGES() in that directory to create (an empty) PACKAGES file.
And of course, the reason you aren't getting this message in 2.15.1 is that it doesn't do the checking (see R NEWS quote above) that 2.15.2 performs.
I have submitted a bug report of my issues. No news yet. May post it to the R mailing list as well.

Resources