Roxygen2 - "argument is of length zero" error when documenting reference class - r

To demonstrate a minimal case, say I have the following package:
#' #docType package
#' #import methods
#' #title MyTitle
#' #description MyDescription
#' #details MyDetails
#' #export
A <- setRefClass("A")
When I roxygenize (in RStudio, before a 'Build & Reload'). I get:
==> roxygenize('.', roclets=c('rd', 'collate', 'namespace'))
* checking for changes ... ERROR
Error in process.docType(partitum) :
Roclet processing error in block Test1.R:7
argument is of length zero
What's going wrong? How do I resolve this error?
My setup:
Roxygen2 3.1.0
Using roxygen to generate (in RStudio):
Rd files
Collate field
NAMESPACE file
Automatically roxygenising when running (in RStudio):
R CMD check
Source and binary package builds
Build & Reload
R: 3.0.2 (Frisbee Sailing)
IDE: RStudio 0.98.490
OS: Windows 8.1

I had a similar situation where
#' #export
A <- setRefClass("A")
cause the same error, which I resolved using:
A <- setRefClass("A")
#' #export
A
Curiously, this was in a file that I had not modified in a while, so it's still a bit of a mystery...

Related

Examples set using #examples fails devtools::check()

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.

Does R's package support unicode rd file?

My platform is:
Win7 64
Rtudio
R3.1.3
devtools 1.8.0
Rxygen2 4.1.1
I am trying to make a package of my own. I need to describe the function by unicode. I used Roxygen2 to generate the Rd file. The code is very simple(I removed the real function just know how to make a package):
xxxx means comment made by unicode.
#' eastChoice
#' #param fn.kind wind xxxxxx
#' #param estChoice.Path wind
#' #return data.frame
#' #export
#' #examples
#' getIndByEastChoice("20150108.csv")
getIndByEastChoice <- function(fn.kind){
d <- data.frame(a=c(1,2,3), b=c(4,5,6))
dt <- data.table::data.table(d)
}
When I check(devtools) the R code, it always failed. The following error information is given:
* checking PDF version of manual ... WARNING
LaTeX errors when creating PDF version.
This typically indicates Rd problems.
LaTeX errors found:
! Package inputenc Error: Unicode char \u8:��<87> not set up for use with LaTeX.
See the inputenc package documentation for explanation.
Type H <return> for immediate help.
At first I thought that the Roxygen2 does not support unicode other than ASCII, but I read the Rd file generated by Roxygen2, it is OK.
And then I just use build&Reload function(using devtools), to my suprise, it passed.
And then I add more unicode comment in the R file and checked(devtools) it again. It is like the following:
#' eastChoice xxxxxx
#' #param fn.kind wind xxxxx
#' #param estChoice.Path wind xxxxxxxx
#' #return data.frame xxxxxxx
#' #export
#' #examples
#' getIndByEastChoice("20150108.csv")
getIndByEastChoice <- function(fn.kind){
d <- data.frame(a=c(1,2,3), b=c(4,5,6))
dt <- data.table::data.table(d)
}
It failed as I expected, but other information was given and seems more serious:
* checking whether package 'ftools.temp' can be installed ... ERROR
Installation failed.
See 'D:/onedrive/program/R.package/ftools.temp.Rcheck/00install.out' for details.
So I checked the log:
* installing *source* package 'ftools.temp' ...
** R
** preparing package for lazy loading
** help
*** installing help indices
wrong in gsub("&", "&", x, fixed = TRUE) : '<86>'multi byte string is wrong
* removing 'D:/onedrive/program/R.package/ftools.temp.Rcheck/ftools.temp'
And this time build&load also failed.
So I think that maybe the R does not support unicode other ASCII in its RD file? Can any one confirm it? If it is true, I will not waste my time trying to solve this problem.
#hrbrmstr
Thank you. I have tried your advice. First, I added the line "Encoding:UTF-8" in the decription file, and then saved .R and DESCRIPTION with coding UTF-8 to ensure that the encode is correct. And I run the "check " function, but I got the same error:
strsplit(txt, "\n", fixed = TRUE)���о���: �������ַ���1����UTF-8
* checking examples ... OK
* checking PDF version of manual ... WARNING
LaTeX errors when creating PDF version.
This typically indicates Rd problems.
����: ��������'"D:/R/R-31~1.3/bin/x64/Rcmd.exe" Rd2pdf --batch --no-preview --build-dir="C:/Users/kanpu/AppData/Local/Temp/RtmpgFjyKw/Rd2pdf15f2c22b7768d" --no-clean -o ftools.temp-manual.pdf "D:/onedrive/program/R.package/ftools.temp.Rcheck/ftools.temp"'��״̬��1
LaTeX errors found:
! Package inputenc Error: Unicode char \u8:��<97> not set up for use with LaTeX.
The DESCRIPTION is like this:
Package: ftools.temp
Type: Package
Title: Just try to make a package
Version: 0.1
Date: 2015-08-25
Author: None
Maintainer: no one <none#somewhere.net>
Description: Just try to make a package
License: CPL
LazyData: TRUE
Encoding:UTF-8
Depends:
data.table

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.
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.

R package build Undocumented code objects

I have written an R package for integrating with electronic medical records. I think I have correctly added the imports and dependencies in the DESCRIPTION file and documented everything via roxygen2, but on three of my functions (which are all in the same file) I get this warning when I run devtools::check("."):
* checking for missing documentation entries ... WARNING
Undocumented code objects:
'add_to_database' 'database' 'import_CPRD_data'
All user-level objects in a package should have documentation entries.
I think I have documented these in the same way as all of my other functions which are fine. Here is one of the offending functions with the roxygen2 documentation:
#' Wrapper for dbconnect
#'
#' Connects to a SQLite database or creates one if it does not already exist
#'
#' If the '.sqlite' file extension is ommited from the dbname argument it is automatically added.
#'
#' #export
#'
#' #param dbname character name path to database file
#' #return SQLiteConnection object
#' #examples \dontrun{
#' db <- database("mydb")
#' }
database <- function(dbname){
if(!str_detect(dbname, "\\.sqlite$")) {
dbname <- paste(dbname, "sqlite", sep = ".")
}
dbConnect(SQLite(), dbname)
}
How can I get rid of this error? I have added stringr and RSQLite to the depends section of the DESCRIPTION file and they show up in NAMESPACE, so I don't think this is an import problem - but then what am I failing to document? The full package is here and the file with the file with the offending functions is here. I have looked in the writing R extensions manual and can't find the problem - don't know if I am just going blind from looking - but I can't see what I am doing differently in these functions from the others I have written!
You use roxygen, but most likely you don't roxygenize your package on build.
Either call:
roxygen2::roxygenize('.', roclets=c('rd', 'collate', 'namespace'))
or, if you use RStudio, edit Project Options (Tools menu) and in the Build Tools tab check Generate documentation with Roxygen.
I have had a similar problem when doing an R CMD check:
Status: 1 WARNING
checking for missing documentation entries ... WARNING
Undocumented code objects:
‘build.log.output’
After removing all files step-by-step I have found the reason: The .Rbuildignore file! It contained (besides other lines) one line with
^.*\.log
The last line makes R ignoring all files containing a ".log" in its name and my function was named "build.log.output" which caused R to ignore the documentation file "build.log.output.Rd" generated by Roxygen2 when creating the package file.
Therefore R CMD check could not find the documentation in the package file!
Solution:
Improve the regular expression to ignore only real log files:
^.*\.log$
("$" means matching the end of line).
Voila :-)
EDIT:
#R Yoda solved the problem stated in my "answer": It was related to a conflict between .Rbuildignore and the function name (more precisley, the file name of the function's documentation).
Same problem here. And for me, it was related to the name of the function. When the name of the function below is load_rdata (or loadrdata), I receive the warning Undocumented code objects: 'load_rdata'. When I rename the function to load_rda, everything is fine.
I know this is half a question (why is this happening), half an answer (maybe because of the function names), but I thought it might help somebody coming across this question.
#' Load RData file.
#'
#' #param file An RData file saved via \code{\link[base]{save}}.
#' #return The object save in \code{file}.
#' #references \url{http://stackoverflow.com/a/5577647}
#' #export
load_rdata <- function(file = NULL) {
env <- new.env()
nm <- load(file, env)[1]
env[[nm]]
}
This is the output from sesssionInfo() and was reproducible when using devtools 1.12.0 and roxygen2 6.0.0 instead.
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C LC_TIME=German_Germany.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] devtools_1.12.0.9000 roxygen2_6.0.0.9000
loaded via a namespace (and not attached):
[1] R6_2.2.0 magrittr_1.5 tools_3.3.2 withr_1.0.2 memoise_1.0.0
[6] Rcpp_0.12.9 xml2_1.1.1 stringi_1.1.2 pkgload_0.0.0.9000 digest_0.6.12
[11] stringr_1.1.0 pkgbuild_0.0.0.9000 commonmark_1.1
I ran across the undocumented code error when I had multiple functions to export. I think you need to add #' #describeIn. Here's my example.
#' #title Colors Definition
#'
#' #description Define colors with transparency value.
#'
#' #param alpha Transparency value (0-1), default 1.
#'
#' #return hex value of the color
#'
#' #examples
#' Get_Red(0.5)
#' Get_Red()
#'
#' #export Get_Red
#' #export Get_Blue
#'
Get_Red <- function(alpha = 1) {
rgb(228 / 255, 26 / 255, 28 / 255, alpha)
}
#' #describeIn Get_Red Blue color function with transparency value.
Get_Blue <- function(alpha = 1) {
rgb(55 / 255, 126 / 255, 184 / 255, alpha)
}
I was experimenting with the toy foofactors package in Hadley Wickam's book. My data.R file included:
#' A character string
#'
"MyString"
and fbind.R had:
#' Print a string
#'
#' Returns the string MyString
#'
#' #export
#'
myString <- function() {
print(foofactors::MyString)
}
check complained that there was undocumented code and data. I fixed this by chnaging the function name to theString. So it appears that the names are not case sensitive which is odd.
After several frustrating hours, I found that I had man/ in my .Rbuildignore file. 🤦
I fixed this same error message by installing the devtools package.
In my case, I had created a new package on a new laptop and installed all the usual R packages and accessories: installr, testthat, roxygen2 and also the accessory tools like rtools and MiKteX. When building the package I got the same error message "checking for missing documentation entries ... WARNING: Undocumented code objects". But I fully resolved the problem by also installing the devtools package.

devtools roxygen package creation and rd documentation

I am new to roxygen and am struggling to see how to be able to use it to quickly create a new/custom package.
I.e. I would like to know the minimum requirements are to make a package called package1 using devtools, roxygen2/3 so that I can run the commands
require(package1)
fun1(20)
fun2(20)
to generate 2000 and 4000 random normals respectively
So lets take the simplest example.
If I have two functions fun1 and fun2
fun1 <- function(x){
rnorm(100*x)
}
and
fun2 <- function(y){
rnorm(200*y)
}
the params are numeric, the return values are numeric. I'm pretty sure this isn't an S3 method, lets call the titles fun1 and fun2....im not too sure what other info i would need to provide. I can put fun1 and fun2 in separate .R files and add abit of #' but am unsure to include all relevant requirements for roxygen and also am unsure what to include as relevant requiremetns and how to use it to create the rd documentation to go with a package are. I presume the namespace would just have the names fun1 and fun2? and the package description would just be some generic information relating to me...and the function of the package?
any step by step guides would be gladly received.
EDIT: The below is how far I got to start with...
I can get as far as the following to create a pacakge...but cant use roxygen to make the documentation...
package.skeleton(list = c("fun1","fun2"), name = "package1")
and here is where I am not sure if I am missing a bunch of steps or not...
roxygenise("package1")
so when trying to install i get the following error message
system("R CMD INSTALL package1")
* installing to library ‘/Library/Frameworks/R.framework/Versions/2.15/Resources/library’
* installing *source* package ‘package1’ ...
** R
** preparing package for lazy loading
** help
Warning: /path.to.package/package1/man/package1-package.Rd:32: All text must be in a section
*** installing help indices
Error in Rd_info(db[[i]]) :
missing/empty \title field in '/path.to.package/package1/man/fun1.Rd'
Rd files must have a non-empty \title.
See chapter 'Writing R documentation' in manual 'Writing R Extensions'.
* removing ‘/Library/Frameworks/R.framework/Versions/2.15/Resources/library/package1’
I'm surprised #hadley says to not use package.skeleton in his comment. I would use package.skeleton, add roxygen comment blocks, then delete all the files in the "man" directory and run roxygenize. However, since Hadley says "Noooooooooo", here's the minimum you need to be able to build a package that passes R CMD check and exports your functions.
Create directory called "package1". Under that directory, create a file called DESCRIPTION and put this in it (edit it appropriately if you like):
DESCRIPTION
Package: package1
Type: Package
Title: What the package does (short line)
Version: 0.0.1
Date: 2012-11-12
Author: Who wrote it
Maintainer: Who to complain to <yourfault#somewhere.net>
Description: More about what it does (maybe more than one line)
License: GPL
Now create a directory called "R" and add a file for each function (or, you can put both of your functions in the same file if you want). I created 2 files: fun1.R and fun2.R
fun1.R
#' fun1
#' #param x numeric
#' #export
fun1 <- function(x){
rnorm(100*x)
}
fun2.R
#' fun2
#' #param y numeric
#' #export
fun2 <- function(y){
rnorm(200*y)
}
Now you can roxygenize your package
R> library(roxygen2)
Loading required package: digest
R> list.files()
[1] "package1"
R> roxygenize("package1")
Updating collate directive in /home/garrett/tmp/package1/DESCRIPTION
Updating namespace directives
Writing fun1.Rd
Writing fun2.Rd
Since you mentioned devtools in the title of your Q, you could use the build and install functions from devtools
build('package1')
install('package1')
Or you can exit R and use the tools that come with R to build/check/install.
$ R CMD build package1
$ R CMD check package1_0.0.1.tar.gz
$ R CMD INSTALL package1_0.0.1.tar.gz
Now, fire up R again to use your new package.
$ R --vanilla -q
library(package1)
fun1(20)
fun2(20)
But, figuring out the minimum requirements is unlikely to help you (or the users of your package) much. You'd be much better off studying one of the many, many packages that use roxgen2.
Here's a better version of the fun1.R file which still doesn't use all the roxygen tags that it could, but is much better than the bare minimum
Modified fun1.R
#' fun1
#'
#' This is the Description section
#'
#' This is the Details section
#'
#' #param x numeric. this is multiplied by 100 to determine the length of the returned vector
#' #return a numeric vector of random deviates of length \code{100 * x}
#' #author your name
#' #seealso \code{\link{fun2}}
#' #examples
#' fun1(2)
#' length(fun1(20))
#' #export
fun1 <- function(x){
rnorm(100*x)
}
Much later - You could let RoxygenReady prepare your functions with the minimal Roxygen annotation skeleton. It basically brings you from your 2 input functions to GSee's answer, which is the input of Roxygen2.

Resources