R packages - should I import the `methods` package? - r

I'm using setRefClass to create classes and since is part of the methods package, I presumed you need to declare this dependency as an import.
However, the following minimal example fails Rcmd.exe check when importing methods:
#' #docType package
#' #import methods
A <- setRefClass("A")
with the following error (my package is called Test):
==> Rcmd.exe check Test_1.0.tar.gz
<Lots of checks here...>
* checking package dependencies ... ERROR
Namespace dependency not required: 'methods'
See the information on DESCRIPTION files in the chapter 'Creating R
packages' of the 'Writing R Extensions' manual.
Exited with status 1.
So from what I can make out, it appears I'm being told to remove the import for methods and so keep hidden the package's dependency on methods. Is my interpretation correct and if so, why hide the dependency on methods?
My setup:
Roxygen2 3.0.0
R: 3.0.2 (Frisbee Sailing)
IDE: RStudio 0.98.490
OS: Windows 8.1

After more hunting around, I realised that in my haste I forgot to add Imports: methods to my DESCRIPTION file.

Related

Creating an R package with devtools and the function "use_rcpp" is instructing me to copy lines of code into a file that does not exist

I'm attempting to create a small test R package just to get a hang of the workflow. I've been following the tutorial found here: https://r-pkgs.org/index.html Everything has been running smoothly but eventually I want to add rcpp code to my package and this is where I'm running into problems. The function use_rcpp in the devtools package is supposed to set up your package with everything you need to start adding c++ files. However once the command is done running it tells you to copy and paste comments into a file that does not exist. The tutorial I'm following says the same thing here https://r-pkgs.org/src.html#cpp but just says to add the roxygen tags to your package. It doesn't specify where I need to add them.
The output I'm getting is different from the one in the tutorial but I assume thats just because im running a different version.
The output the function is giving:
✔ Setting active project to
'R/packages/rcppTester'
✔ Creating 'src/'
✔ Adding '.o', '.so', '*.dll' to 'src/.gitignore'
● Copy and paste the following lines into
'R/packages/rcppTester/R/rcppTester-package.R':
## usethis namespace: start
#' #useDynLib rcppTester, .registration = TRUE
## usethis namespace: end
NULL
[Copied to clipboard]
✔ Adding 'Rcpp' to LinkingTo field in DESCRIPTION
✔ Adding 'Rcpp' to Imports field in DESCRIPTION
● Copy and paste the following lines into
'R/packages/rcppTester/R/rcppTester-package.R':
## usethis namespace: start
#' #importFrom Rcpp sourceCpp
## usethis namespace: end
NULL
[Copied to clipboard]
Again I have no file named "rcppTester-package.R" and my package is throwing errors when I try and use the cpp files. Any advice would be greatly appreciated.
You do not have to use devtools or usethis if they confuse you -- they do add a layer of obfuscation that sometimes obstruct what is happening.
Rcpp itself has a function Rcpp.package.skeleton(), and the RStudio IDE has something related (but independent) under 'File -> New Project -> Package with Rcpp'. I use both. You could start with either and get a package that compiles. Both will do that.
Take that as a snapshot and then add roxygen2 documentation. If you do it manually, you need to run compileAttributes() to get the roxygen markup from the C++ file to the R file where roxygenize() will see it. RStudio does both for you automatically.
So to sum up: try separating 'package with Rcpp' and 'package using roxygen' if the joint 'package using Rcpp and roxygen' gives you issues.

Add dependencies for my Rcpp package

Rcpp beginner's question:
I want to improve my execution efficiency in R. So I write some code in cpp and use Rcpp to help me compile them.
Question is that I use some other R packages in my .cpp files and I want those packages to be installed and imported automatically when a user installs my package.
e.g. If I use the R package 'gtools' in my files, I don't want the error:
* installing to library 'C:/Program Files/R/R-3.4.1/library'
* installing *source* package 'pkgname' ...
make: Nothing to be done for `all`.
** libs
installing to C:/Program Files/R/R-3.4.1/library/pkgname/libs/i386
** R
** preparing package for lazy loading
Error in library(gtools) : there is no package called 'gtools'
Error : unable to load R code in package 'pkgname'
ERROR: lazy loading failed for package 'pkgname'
* removing 'C:/Program Files/R/R-3.4.1/library/pkgname'
Exited with status 1.
I tried to add depended package name to the DESCRIPTION file. i.e.
Imports: Rcpp (>= 0.12.12),gtools
LinkingTo: Rcpp, gtools
But it gives me following error:
ERROR: dependency 'gtools' is not available for package 'pkgname'
I don't find any similar questions and please tell me if there are.
First, you should probably make sure gtools is installed on your system. I say this because of the following error:
Error in library(gtools) : there is no package called 'gtools'
With this being said, the main issue you are running into is uncertainty between the LinkingTo: and Imports: fields in the DESCRIPTION file. This is covered in Section 1.1.3: Package Dependencies of Writing R Extensions.
Specifically, we have:
The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached. Namespaces accessed by the ‘::’ and ‘:::’ operators must be listed here, or in ‘Suggests’ or ‘Enhances’ (see below). Ideally this field will include all the standard packages that are used, and it is important to include S4-using packages (as their class definitions can change and the DESCRIPTION file is used to decide which packages to re-install when this happens). Packages declared in the ‘Depends’ field should not also be in the ‘Imports’ field. Version requirements can be specified and are checked when the namespace is loaded (since R >= 3.0.0).
And the LinkingTo field:
A package that wishes to make use of header files in other packages needs to
declare them as a comma-separated list in the field ‘LinkingTo’ in the
DESCRIPTION file. For example
LinkingTo: link1, link2
The ‘LinkingTo’ field can have a version requirement which is checked at installation.
Specifying a package in ‘LinkingTo’ suffices if these are C++ headers containing source code or static linking is done at installation: the packages do not need to be (and usually should not be) listed in the ‘Depends’ or ‘Imports’ fields. This includes CRAN package BH and almost all users of RcppArmadillo and RcppEigen.
For another use of ‘LinkingTo’ see Linking to native routines in other packages.
So, the Imports: is meant to specify packages that contain R functions that you wish to import. In particular, the function from a given package or the entire package itself must be specified in the NAMESPACE file. For packages that use Rcpp, you can typically expect R functions to be available if the author has exported the routine from C++.
Now, regarding the LinkingTo:, this is a bit more specific. If an author wishes to make available a C++ API via header files they must explicitly declare the statements as is given in native methods of Writing R Extensions. Generally, packages that proceed in this manner are "header-only". These packages place the header definitions under inst/include, e.g.
|- pkgname
|- inst/
|- include/
|- pkgname.h
|- R/
|- man/
|- DESCRIPTION
|- NAMESPACE
However, another trend is to allow for "non-header" packages. This leads to a bit more complicated of topic as you have to understand shared objects and dynamic libraries. CRAN presents an overview of how to "Link" packages in Section 5.8: Linking to other packages of Writing R Extensions
If the author does not make available a C++ API, then there are four options:
Ask the author nicely to support calling the C++ API or submit a patch that enables access to the C++ API.
Call an R function from C++. (This negates any performance gain from writing your code in C++ though.)
Copy the implementation from the author's package while respecting their intellectual property.
Implement the desired functionality from scratch to avoid licensing issues.
Unfortunately, this is the case for gtools. As the author(s) do not provide a means to "link" to the C++ version of package's code.

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
.

These packages need to be imported from (in the NAMESPACE file)

In trying to create a local R package, I listed some dependent packages as Depends:
...
Description: NA
License: GPL-2
Depends:R (>= 2.15.0),
survival,
PropCIs,
boot,
msm,
reshape2
LazyData: true
But I got these message by run R CMD check:
*checking dependencies in R code ... NOTE
Packages in Depends field not imported from:
‘PropCIs’ ‘boot’ ‘msm’ ‘reshape2’ ‘survival’
These packages need to be imported from (in the NAMESPACE file)
for when this namespace is loaded but not attached.
Then I use manually added these packages to NAMESPACE file, but it does work and the import lines were deleted automatically after checking.
Another weird thing is the checking process showed:
R CMD check succeeded
But the files then disappeared or deleted systematically/automatically.
May somebody know the reasons?
Just add the following lines to your roxygen code:
#import PropCIs boot msm reshape2 survival

Install package from source throws "package not available" in R

I've read many posts here on how to install R packages from local files, from source etc. and still I have troubles installing (my own) package. The package was created using RStudio, Roxygen2 and RTools (Windows).
The package I'm trying to install (I do this in order to give an instruction so other people know how to install the package - I myself just compile and install the package from RStudio) can be downloaded here:
sjPlot-package
I also created a PACKAGE description, that is located on my server in the same directory as the package, using write_PACKAGES().
Now, if I try install.packages("sjPlot_0.1", contrib.url="http://www.strengejacke.de/R-Stuff/sjPlot/") I get following error message:
Warning in install.packages :
package ‘sjPlot_0.1’ is not available (for R version 3.0.2)
If I use install.packages("sjPlot_0.1", repos="http://www.strengejacke.de/R-Stuff/sjPlot/") I get following error message:
source repository is unavailable to check versions
Error in install.packages : Line starting ' ...' is malformed!
Also, a local install via install.packages("sjPlot_0.1", contriburl="C:/Users/Luedeke/Dropbox/R-Statistics/packages/") fails (this directory contains source-package, binary-package and PACKAGE descr. files).
I know there are a lot of postings on how to install R packages, and I read some of them - perhaps I missed the right one, if so, please excuse me for asking this question again.
My question is: How can I (or other people) install my R package (including installation of dependencies would be nice)?
Thanks in advance
Daniel
Your package does not pass R CMD check:
> R CMD check sjPlot_0.1.tar.gz
* using log directory ‘/home/edisz/Downloads/sj_tmp/sjPlot.Rcheck’
* using R version 3.0.2 (2013-09-25)
* using platform: x86_64-pc-linux-gnu (64-bit)
* using session charset: UTF-8
* checking for file ‘sjPlot/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘sjPlot’ version ‘0.1’
* checking package namespace information ... OK
* checking package dependencies ... ERROR
Namespace dependencies not required:
‘HH’ ‘MASS’ ‘car’ ‘faraway’ ‘foreign’ ‘ggplot2’ ‘lmtest’ ‘plyr’
‘reshape2’ ‘scales’ ‘vcd’
See the information on DESCRIPTION files in the chapter ‘Creating R
packages’ of the ‘Writing R Extensions’ manual.
Exited with status 1.
Looking at your DESCRIPTION file, you'll see that the Collate and Import fields are missing.
Roxygen take care of the Collate fields (If you're using RStudio Configure roxygen to do so), however you have to write the Import field manually to the DESCRIPTION.
Looking at one of your functions:
#' #title Import SPSS dataset as data frame into R
[snip]
#'
#' #param path the file path to the SPSS dataset
#' #param enc the file encoding of the SPSS dataset
#' #return a data frame containing the SPSS data. retrieve value labels with \code{\link{sji.getValueLabels}}
#' and variable labels with \code{\link{sji.getVariableLabels}}
[snip]
#' #export
sji.SPSS <- function(path, enc=NA) {
# init foreign package
require("foreign")
# import data as data frame
data.spss <- read.spss(path, to.data.frame=TRUE, use.value.labels=FALSE, reencode=enc)
# return data frame
return(data.spss)
}
You see that there is a require('foreign') call, but no #import foreign tag.
I would suggest to remove the line require('foreign') (it's not needed, if you import the package) and add a #import foreign tag.
Than add to your Description file
Imports:
foreign
Do this with all other functions and packages.
Hope this helps (and is correct),

Resources