Package Dependencies - r

I have an R package, that depends on the base64enc library. When I run the source file in the package with Rscript:
Rscript analyzer.R
it runs just fine.
The first line in analyzer.R is:
library(base64enc);
However, when I run a function from the package in the repl:
library(analyzer)
analyze()
It complains that base64enc is not installed.
Error in rawToChar(base64decode(mark[1])) :
could not find function "base64decode"
Calls: analyze ... collect.marks -> lapply -> FUN -> lapply -> FUN -> rawToChar
However, when in the REPL I manually include base64enc:
library(base64enc)
library(analyzer)
analyze()
It works fine. Is there anyway I can tell my analyzer package to use the base64enc library without having to include it in every script every time I use the library?

(Note that a script is not a package.)
When you run your script analyzer.R it explicitly loads base64enc so the package is in your load path.
But your package may just have Imports: base64enc with a corresponding NAMESPACE statement -- that makes the code from base64enc available in you package but does not load it.
Back in the day we used to do Depends: base64enc which would load it too -- but clutters the search path. Imports: is better, but has the very side-effect you observe here. So just load the other package at the REPL.

Related

Installation of non-CRAN package requires CRAN mirror

I'm trying to install the INLA package of the R-INLA project on a Linux based computing cluster. The package is not on CRAN. According to their tutorial, it suffices to use
install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/stable"), dep=TRUE)
for installation, which works perfectly fine on my local machine. However, on the computing cluster, seemingly a CRAN mirror is required to run this command and I get the following error:
Error in contrib.url(repos, type) :
trying to use CRAN without setting a mirror
Calls: install.packages -> startsWith -> contrib.url
Execution halted
As an alternative, I tried to install the package directly from the corresponding GitHub repository using devtools. For some reason, this gives me the obviously incorrect version number INLA_99.99.9999. This prohibits me from manually adding the necessary binaries via INLA:::inla.binary.install() as the version number is not found. Any help is appreciated!
You are relying on the (R global) options() having a valid repos entry on the cluster.
Which you ... cannot as base R ships (in source from) without such as base R Core feels, rightly or wrongly, that they cannot play favourites and set one. Some of us think that is wrong (as it diminishes the user experience -- like yours here) so in the Debian (and hence Ubuntu) package I set this to the 'cloud' mirror everybody is close to as it is on a CDN:
edd#rob:~$ tail -6 /usr/lib/R/etc/Rprofile.site
## We set the cloud mirror, which is 'network-close' to everybody, as default
local({
r <- getOption("repos")
r["CRAN"] <- "https://cloud.r-project.org"
options(repos = r)
})
edd#rob:~$
I suggest you do the same, maybe in ~/.Rprofile, on the cluster.

install.packages fails in knitr document: "trying to use CRAN without setting a mirror"

Using the following code I got the data I wanted, but for some reason I can't figure out knitr doesn't let me compile a PDF document, as shown further below:
My code:
install.packages("weatherData")
library(weatherData)
istanbul <- getWeatherForDate("Istanbul",
start_date = Sys.Date() - 41,
end_date = Sys.Date())
Works out with no problem but I get the following message trying compile the PDF:
Quitting from lines 3-31 (ist_weather.spin.Rmd)
Error in contrib.url(repos, type) :
trying to use CRAN without setting a mirror
Calls: <Anonymous> ... eval -> eval -> install.packages -> grep -> contrib.url
Execution halted
Knitr produces a R session, without a default cran mirror unless you specifically asked for one. We tend to forget we need to set up CRAN for every R session when we use Rstudio because it takes care of it, but only for interactive use, not for knitr.
You could try specifying a mirror as a install.packages argument:
install.packages("weatherData",repos = "http://cran.us.r-project.org")
Alternatively, you could set up your default CRAN mirror in your .Rprofile. See this answer.
That said, it is not a good idea to install packages through a knitr document that you will probably compile several times. You should assume people know how to install a missing package if needed, or at least test whether the package is installed before installing it again
if(!require(weatherData)) install.packages("weatherData",repos = "http://cran.us.r-project.org")
You must set the CRAN repository in your R. To do so, launch R or RStudio. in the R terminal run following codes.
r = getOption("repos")
r["CRAN"] = "http://cran.us.r-project.org"
options(repos = r)
install.packages("weatherData")
Above code defines CRAN repository in the R and in next package installation no need to define again.
Alternative way is to simply run install.packages("weatherData", repos="http://cran.us.r-project.org"). However, with the second solution the repository not set and you must pass it as a parameter in every package installation.
Honestly,
It would not work for me because I installed packages, and these lines of code were interrupting the knit function. When I removed all lines containing installing packages (and used the most updated R and R Markdown available) this error went away.

Could not find function "year" after installation from zip file R

I'm using R and working on a server without internet connection. So I had to search how to install packages from a zip file. I wanted to use the lubridate package.
install.packages("V:/R/lubridate_1.3.3.zip", repos=NULL)
Then I tried to use
library(lubridate)
year(data$date)
but I received an error that package or namespace load failed for "lubridate". And the function year could not be found.
Did I forgot any step?
If you install a package with install.packages from a CRAN mirror, the imports and dependency tree is installed automatically. If you download the zip and do an offline installation that's obviously not possible. Thus, you have to download and install all these packages, too.
You can find the primary dependencies and imports from lubridate's CRAN page and then follow the links to get the whole trees. Or you can get it even more easily from MRAN.
If I didn't miss anything, you need plyr, stringr, memoise, Rcpp, stringi, magrittr, and digest.
This method is also probably not really feasible for packages with big dependency trees. You could use the function from this SO answer in such a case.

Package "Imports" not loading in R development package

I am building a package in R in a windows environment using Rstudio, devtools roxygen2 and Rtools.
The package is showing no problems in R CMD CHECK. However when I try to load the package using library("mypkg"), the packages specified under Imports in DESCRIPTION are not being loaded (Loading required package: message is not there). On using pkgDepends("mypkg"), the $Depends is shown as character(0).
I have to load the required packages using library() for mypkg to function.
I am using namespace imports instead of package::function() syntax. All the required packages are there in the NAMESPACE as imports().
Why is this happening? How to solve this?
That's the correct behaviour. Imports just means that code inside your package can see the functions that you import from other packages. The other packages aren't placed on the search path like with Depends.
Further reading:
Better explanation of when to use Imports/Depends

R devtools:document Dependency package not available

Hi I am following the tutorial here from Hilary and here from Hadley Wickham trying to create a dummy package.
However, my package need some external dependencies XML and RCurl in this case, when I run the command document, it will complain that:
> setwd('/home/datafireball/projects/Rprojects/rgetout/rgetout')
> document()
Error: could not find function "document"
> library(devtools)
> document()
Updating rgetout documentation
Loading rgetout
Loading required namespace: XML
Error in (function (dep_name, dep_ver = NA, dep_compare = NA) :
Dependency package XML not available.
>
Here is my DESCRIPTION file.
Package: rgetout
Title: A R package to get all the outlinks for a given URL
Version: 0.1
Authors#R: "Eric Cartman <Eric.Cartman#gmail.com> [aut, cre]"
Description: This package is intended to include as much web extraction functionality as much as possible. It starts with one function. getout will extract
all the outlinks for a given URL with a user-agent that you can customize.
Depends: R (>= 3.0.2)
Imports:
XML,
RCurl
License: MIT
LazyData: true
Here is the source code github repo if you want to get more info.
If you are having problems with this, even when you have the packages installed and loaded, I suggest you to do the following.
Delete the Imports: and Suggests: entries of your DESCRIPTION file.
Make sure you have usethis working by doing library(usethis)
Now start adding the libraries to your DESCRIPTION file, by running the following command on your console: usethis::use_package("dplyr") for any Imports: you need. Repeat this step for every library that is required.
In my case, dplyr was the one refusing to load. You can decide where the package will be located by doing: usethis::use_package("dplyr", "Suggests").
It is assumed that you will have the required tools / dependencies for developing a package when you are
doing so.
utils::install.packages has a dependencies argument that will attempt to install uninstalled packages on which a package depends / (in whichever way they are dependent (suggests/ depends/linkingTo).
devtools::install_github will perform similarly.
Installing a package and documenting it as a component of development are quiet different activities
.

Resources