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?

jirikadlec2
  • 1,256
  • 1
  • 23
  • 36

1 Answers1

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.

BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • Then remove `@importFrom RCUrl getURL` since the OP is doing `RCurl::getURL()` – hadley Dec 08 '14 at 22:07
  • @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). – BrodieG Dec 08 '14 at 22:13
  • Or maybe OP has an `@imports` someplace else? – BrodieG Dec 08 '14 at 22:15
  • 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 http://r-pkgs.had.co.nz/namespace.html, which is my attempt to explain it all. – hadley Dec 08 '14 at 22:21
  • @hadley, makes sense, I was getting the NAMESPACE and DESCRIPTION requirements mixed up. – BrodieG Dec 08 '14 at 22:32
  • @hadley, one problem with using `Rcurl::getURL` is the `Rprof` records that as ``. I had forgotten about that until I tried profiling the `dplyr` calls. – BrodieG Dec 17 '14 at 15:12