Create an R package with dependencies
Asked Answered
B

1

8

I'm trying to write my first R package. The functions in the package depend on the getURL() function from the RCurl package. I followed the tutorials on: http://r-pkgs.had.co.nz/ and http://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/

I installed RTools, devtools and roxygen2 for writing the documentation and building the package.

The name of my package is "waterml". In my package I have the folder R with 3 files GetSites.R , GetVariables.R, GetValues.R. Each file has one function:

#' GetSites
#' @import XML
#' @importFrom RCurl getURL
#' This function gets the table of sites from the WaterML web service
#' @param server The URL of the web service ending with .asmx,
#'  for example: http://worldwater.byu.edu/interactive/rushvalley/services/cuahsi_1_1.asmx
#' @keywords waterml
#' @export
#' @examples
#' GetSites("http://worldwater.byu.edu/interactive/rushvalley/services/cuahsi_1_1.asmx")

GetSites <- function(server) {
  sites_url <- paste(server, "/GetSitesObject", sep="")
  text <- RCurl::getURL(sites_url)
  doc <- xmlRoot(xmlTreeParse(text, getDTD=FALSE, useInternalNodes = TRUE))
  return(doc)
}

Now, I try to build the package:

library(devtools)
document()

The document() step completes without error. Now I run:

setwd("..")
install("waterml")

But I get the error:

* installing *source* package 'waterml' ...
** R
** preparing package for lazy loading
Error : object 'function' is not exported by 'namespace:RCurl'
ERROR: lazy loading failed for package 'waterml'
* removing 'C:/Program Files/R/R-3.1.2/library/waterml'

When I checked my NAMESPACE file, it contains some strange lines:

# Generated by roxygen2 (4.0.2.9000): do not edit by hand

export(GetSites)
export(GetValues)
export(GetVariables)
import(RCurl)
import(XML)
importFrom(RCurl,"function")
importFrom(RCurl,This)
importFrom(RCurl,WaterML)
importFrom(RCurl,data)
importFrom(RCurl,from)
importFrom(RCurl,getURL)
importFrom(RCurl,gets)
importFrom(RCurl,of)
importFrom(RCurl,series)
importFrom(RCurl,service)
importFrom(RCurl,sites)
importFrom(RCurl,table)
importFrom(RCurl,the)
importFrom(RCurl,time)
importFrom(RCurl,values)
importFrom(RCurl,variables)
importFrom(RCurl,web)

I think that the error is in the statement:

importFrom(RCurl, "function")

Any ideas what could be the problem? Am I using the @importFrom in the documentation of my function correctly?

Barela answered 8/12, 2014 at 18:36 Comment(0)
I
8

Change:

#' GetSites
#' @import XML
#' @importFrom RCurl getURL
#' This function gets the table of sites from the WaterML web service
#' @param server The URL of the web service ending with .asmx,

To:

#' GetSites
#'
#' This function gets the table of sites from the WaterML web service
#'
#' @import XML
#' @importFrom RCurl getURL
#' @param server The URL of the web service ending with .asmx,

roxygen2 is reading the line following @importFrom and assuming each word is a function you want to import.

Innate answered 8/12, 2014 at 18:46 Comment(6)
Then remove @importFrom RCUrl getURL since the OP is doing RCurl::getURL()Pasteurizer
@hadley, wouldn't it be better to remove the RCurl::? Frankly, I hadn't even seen the RCurl::getURL. Unless I'm missunderstanding, it seems what you suggest introduces an implicit dependency (i.e. no imports(RCurl) in NAMESPACE).Innate
Or maybe OP has an @imports someplace else?Innate
I think it's always better to be explicit and use RCurl::getURL(). Then you don't need anything in NAMESPACE (but you still need Imports: RCurl in the DESCRIPTION). You might want to read r-pkgs.had.co.nz/namespace.html, which is my attempt to explain it all.Pasteurizer
@hadley, makes sense, I was getting the NAMESPACE and DESCRIPTION requirements mixed up.Innate
@hadley, one problem with using Rcurl::getURL is the Rprof records that as <anonymous>. I had forgotten about that until I tried profiling the dplyr calls.Innate

© 2022 - 2024 — McMap. All rights reserved.