kanpu
kanpu

Reputation: 261

Error use other library when I use Rstudio and Roxygen2 to build a package

To simplify the problem. I tried the following thing. My goal is to build a simple package which need another library.

I used RStudio and tried to create a new package, and checked the project option to "Generate document with Roxygen". And I get the following code:

#' Title just a test
#'
#' @return nothing
#' @export
#'
#' @examples
#' hello()
hello <- function() {
  print("Hello, world!")
}

and I "check"ed it and "build and reload"ed it by the RStudio, all is OK. Then I tried to add one line in the head of the code:

library("data.table")

#' Title just a test
#'
#' @return nothing
#' @export
#'
#' @examples
#' hello()
hello <- function() {
  print("Hello, world!")
}

Then I failed amd get the following:

* checking whether package 'kanpu.temp' can be installed ... ERROR
Installation failed." 

When I check the log, it says that:

* installing *source* package 'kanpu.temp' ...
** R
** preparing package for lazy loading
Error in library("data.table") : there is no package called 'data.table'
Error : unable to load R code in package 'kanpu.temp'
ERROR: lazy loading failed for package 'kanpu.temp'
* removing 'D:/onedrive/program/R/kanpu.temp.Rcheck/kanpu.temp'

I am sure that data.table is a existed package in my RStudio System. and also tried other package like "ggplot2", "plyr", and get the same result.

So how can I resolve this problem?

The envirement is:

Win7 64
RStudio 0.99.473
R 3.1.3 64

After checking the "Writing R Extensions", I know what's wrong with the code.

I should use "Import" or "Depends" in the "DESCRIPTION" file.

Upvotes: 1

Views: 364

Answers (1)

sdgfsdh
sdgfsdh

Reputation: 37025

Looking at the error message, it seems that you do not have the ggplot2 package installed. This will cause an error when R reaches the line library(ggplot2).

The solution is to install that package:

install.packages("ggplot2")

However, you probably shouldn't be calling library in your packaged code. A package should make as few changes to the external environment as possible.

Instead, mark the package as required in your DESCRIPTION and make fully qualified function calls: SomePackage::someFunction().

See Hadley's pages for further information.

Upvotes: 4

Related Questions