I am making an R package, and using roxygen2 to create documentation.
I have provided an example for one of the functions using:
#' #examples
#' \dontrun{
#' Train_model()
#' }
I have included an #export tag before this.
But, when I run devtools::check(). I get the following error:
Error in library("package1") :
there is no package called 'package1'
package1 is the name of the package I am creating.
Related
I'm writing a package (called pac) where I have a function with a non-syntactic name (called +f). I'm unable to correctly document and export this function.
Here's a reproducible example:
library(devtools)
setwd("~/yourpath")
create("pac")
Save the following function definition along with its documentation in pac/R/+f.R
#' Add two objects
#'
#' #name `+f`
#'
#' #param x an object
#' #param y an object
#' #return A sum
#' #export
`+f`<- function(x, y) {x + y}
Then run
document()
load_all()
`+f`(2, 2)
Which produces the output
> document()
Updating pac documentation
Loading pac
Writing NAMESPACE
Writing tick-plus-f-tick.Rd
> load_all()
Loading pac
Warning message:
In setup_ns_exports(path, export_all, export_imports) :
Objects listed as exports, but not present in namespace: +f
> `+f`(2, 2)
Error in `+f`(2, 2) : could not find function "+f"
As you can see, the function `+f` is not exported correctly, even though the NAMESPACE-file does have the following line:
export(`+f`)
On the other hand, since the documentation file pac/man/tick-plus-f-tick.Rd exists, I am able to display it by calling ?"`+f`".
How can I define and document a function with a non-syntactic name in a package? What's going on in the above example? Why isn't the function available when pac is loaded, even though the NAMESPACE-file includes an export-statement?
I'm writing an R package where I implement my own 'predict' generic and an S4 method for the signature 'apk', which is an S4 class inside my package as well. I'm using Rstudio as editor and generating the documentation with Roxygen2.
All the problem goes about the autocompletion candidates that Rstudio shows when I type predict once the package is installed and loaded. Just after opening a new R session, if I type predict, I get the following candidates: predict {aPack}, predict {stats}, predict.glm {stats} and predict.lm {stats}. Note that both, the S4 from my package aPack and the one from stats are displayed as candidates. This happens because right before setting the generic, I imported predict from stats as suggested in this SO answer.
The problem: when I load another package with an S4 predict method, I'm no longer able to make my S4 show as autocomplete candidate. For instance, if I load the DiceKriging package and mine in the same session I only get the following candidates: predict {DiceKriging}, predict {stats}, predict.glm {stats}, predict.lm {stats} and predict.km {DiceKriging}. Once I load DiceKriging it is not possible to get my S4 displayed in autocomplete even if I load my package again. The predict method still works without predict {aPack} being listed by the autocompletion system, however, I would like to make it visible so that the user gets directly aware of the availability of my method.
Question: how sould I modify my roxygen documentation so that predict {aPack} gets prompted by the autocompletion system even if I load another package with a predict S4 method?
Minimal reproducible example
#' #title Class: apk model
#' #description To create an apk object, use \link[aPack]{apk}.
#' #slot call Object of class \code{"language"}. User call reminder.
#' #rdname apk-class
#' #import methods
#' #export
setClass("apk", representation(call = "language"), validity = function(object) {T})
#' #title Create an Object of class \code{"apk"}
#' #description Creator function for objects of class \code{"apk"}.
#' #param foo Not used yet.
#' #param ... Not used yet.
#' #export
apk <- function(foo, ...) {
new("apk")
}
#' #name predict
#' #rdname predict-methods
#' #importFrom stats predict
#' #param object An object to predict from.
#' #param ... Further arguments for methods.
#' #export predict
setGeneric(name = "predict", def = function(object, ...) standardGeneric("predict"))
predict.apk <- function(object, bar, ...) {
print("I'm an apk prediction!")
}
#' #title Prediction Method for the apk Class
#' #name predict
#' #rdname predict-methods
#' #aliases predict,apk-method
#' #examples
#' myApk <- apk()
#' predict(myApk)
setMethod("predict", "apk", predict.apk)
GitHub: I also uploaded the project to GitHub so that you can directly make your suggested changes there if you want. The repository is here.
I'm trying to create a S3 method in my package called dimnames. This is a primitive in R, but there should be an S3 in my package with the same name.
I've got the following file dimnames.r
#' S3 overwriting primitive
#'
#' #param x object
#' #export
dimnames = function(x) {
UseMethod("dimnames")
}
#' title
#'
#' #export
dimnames.data.frame = function(x) {
dimnames.default(x)
}
#' title
#'
#' #export
dimnames.list = function(x) {
lapply(x, dimnames)
}
#' title
#'
#' #export
dimnames.default = function(x) {
message("in S3 method")
base::dimnames(x)
}
I then create a package from it (in R=3.3.2):
> package.skeleton("rpkg", code_files="dimnames.r")
> setwd("rpkg")
> devtools::document() # version 1.12.0
And then check the package
R CMD build rpkg
R CMD check rpkg_1.0.tar.gz
I get the following output (among other messages):
Warning: declared S3 method 'dimnames.default' not found
Warning: declared S3 method 'dimnames.list' not found
Loading the package and checking its contents, dimnames.data.frame is exported while dimnames.default and dimnames.list are not. This does not make sense to me. As far as I understand, I declared the exports correctly. Also, the NAMESPACE file looks good to me:
S3method(dimnames,data.frame)
S3method(dimnames,default)
S3method(dimnames,list)
export(dimnames)
Why does this not work, and how to fix it?
(Bonus points for: why do I need #' title in the S3 implementations when they should not be needed with roxygen=5.0.1?)
S3 methods are only exported if it is desired that the user be able to access them directly. If they are always to be invoked via the generic then there is no need to export them.
The problem with R CMD check is likely due to defining your own generic for dimnames. Normally one just defines methods and leverages off the primitive generic already in R. Remove the dimnames generic from dimnames.r.
There should be no problem in adding methods for new classes but you may have problems trying to override the functionality of dimnames for existing classes that R's dimnames handles itself.
I have a own r package that works well. However when I check it, R delete me the export(predict.acb) on the NAMESPACE file.
In my .R file I have:
#' #export
acb<-function(Class,data,lambda,distance=NULL,prior=NULL,info.pred=NULL)
#' #export
predict.acb<-function(modelFit,newdata)
In my NAMESPACE I have
export(acb)
export(predict.acb)
After check I get
S3method(predict,acb)
export(acb)
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?
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.