Does R's package support unicode rd file? - r

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

Related

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.

How to wrap data.table::fread in your own package, with bit64 capability?

I have a package containing a function that calls fread from data.table. data.table has the bit64 package in the Suggests field of its DESCRIPTION file, which gives fread the capability to import large integers as
integer64 rather than numeric. I need this capability by default in my package.
Here's a reproducible example, under R 3.1.3 (earlier versions don't have this issue).
Attempt 1
Vectorize(dir.create)(c("test", "test/R", "test/man"))
cat(
"Package: test
Title: Test pkg
Description: Investigate how to use suggested package
Version: 0.0-1
Date: 2015-03-10
Author: Richie Cotton
Maintainer: Richie Cotton <a#b.com>
Imports: data.table
Suggests: bit64
License: Unlimited
",
file = "test/DESCRIPTION"
)
cat(
"#' Read data
#'
#' Wrapper to \\code{fread} that loads bit64 first
#' #param ... Passed to fread.
#' #return A data frame of uniformly distributed random numbers and their index.
#' #importFrom data.table fread
#' #export
read_data <- function(...)
{
library(bit64)
fread(...)
}",
file = "test/R/read_data.R"
)
When I run R CMD check,
library(roxygen2)
library(devtools)
roxygenize("test")
check("test")
I get the following NOTE:
* checking dependencies in R code ... NOTE
'library' or 'require' call to 'bit64' in package code.
Please use :: or requireNamespace() instead.
See section 'Suggested packages' in the 'Writing R Extensions' manual.
Attempt 2
The documentation suggests replacing library with requireNamespace. This checks to see if the package exists, but doesn't load it onto R's search path.
If I update the definition of read_data to:
read_data <- function(...)
{
if(!requireNamespace('bit64'))
{
warning('bit64 not available.')
}
fread(...)
}
then R CMD check runs smoothly, but since bit64 is now not loaded, fread doesn't have the ability to read long integers.
Attempt 3
If I change the DESCRIPTION so that bit64 is in the Depends section (instead of Suggests, and keep read_data as in attempt 2, or simplify it to
read_data <- function(...)
{
fread(...)
}
then R CMD check gives the NOTE:
* checking dependencies in R code ... NOTE
Package in Depends field not imported from: 'bit64'
These packages need to be imported from (in the NAMESPACE file)
for when this namespace is loaded but not attached.
I'm not quite sure what I should be importing in this case.
Attempt 4
If I keep bit64 in the Depends section, and use the original definition of read_data,
read_data <- function(...)
{
library(bit64)
fread(...)
}
then R CMD check gives the NOTE:
* checking dependencies in R code ... NOTE
'library' or 'require' call to 'bit64' which was already attached by Depends.
Please remove these calls from your code.
Package in Depends field not imported from: 'bit64'
I feel like there should be some magic combination of DESCRIPTION and function definitions that gives me the bit64 functionality and passes R CMD check cleanly; I just can't see what I've missed.
How can I do this?
Attempt 3 was closest; I just needed an extra #import bit64 in the roxygen documentation.
Vectorize(dir.create)(c("test", "test/R", "test/man"))
cat(
"Package: test
Title: Test pkg
Description: Investigate how to use suggested package
Version: 0.0-1
Date: 2015-03-10
Author: Richie Cotton
Maintainer: Richie Cotton <a#b.com>
Depends: bit64
Imports: data.table
License: Unlimited
",
file = "test/DESCRIPTION"
)
cat(
"#' Read data
#'
#' Wrapper to \\code{fread} that loads bit64 first
#' #param ... Passed to fread.
#' #return A data frame of uniformly distributed random numbers and their index.
#' #import bit64
#' #importFrom data.table fread
#' #export
read_data <- function(...)
{
fread(...)
}",
file = "test/R/read_data.R"
)

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

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

Exporting a Data Set (package development)

I have a package (qdap) and an accompanying data only package (qdapDictionaries) that will be used by some of the functions in qdap as seen here in line 41 I use the syn.env from line 454 here.
I have already asked a related question on the R-help list but was told off list it was not appropriate for the list as it uses private notation from roxygen2, however, I could actually export the data sets from qdapDictionaries. I tried simply adding #export to the .R file that describes the data sets but this results in an error when compiling.
> set(qdapDictionaries)
Installing qdapDictionaries
"C:/R/R-3.0.1/bin/i386/R" --vanilla CMD INSTALL \
"C:\Users\trinker\GitHub\qdapDictionaries" --library="C:/R/R-3.0.1/library" \
--with-keep.source --install-tests
* installing *source* package 'qdapDictionaries' ...
** R
** data
*** moving datasets to lazyload DB
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
*** arch - i386
Error in namespaceExport(ns, exports) :
undefined exports: abbreviations, action.verbs, adverb, amplification.words, BuckleySaltonSWL, contractions, deamplification.words, DICTIONARY, emoticon, env.pol, env.syl, env.syn, interjections, labMT, NAMES, NAMES_LIST, NAMES_SEX, negation.words, negative.words, OnixTxtRetToolkitSWL1, positive.words, preposition, SYNONYM, Top100Words, Top200Words, Top25Words
Error: loading failed
Execution halted
*** arch - x64
Error in namespaceExport(ns, exports) :
undefined exports: abbreviations, action.verbs, adverb, amplification.words, BuckleySaltonSWL, contractions, deamplification.words, DICTIONARY, emoticon, env.pol, env.syl, env.syn, interjections, labMT, NAMES, NAMES_LIST, NAMES_SEX, negation.words, negative.words, OnixTxtRetToolkitSWL1, positive.words, preposition, SYNONYM, Top100Words, Top200Words, Top25Words
Error: loading failed
Execution halted
ERROR: loading failed for 'i386', 'x64'
* removing 'C:/R/R-3.0.1/library/qdapDictionaries'
* restoring previous 'C:/R/R-3.0.1/library/qdapDictionaries'
Error: Command failed (1)
Clearly I am not exporting these data sets correctly. How can I export the data sets from qdapDictionaries so that I can then use #importFrom in the parent qdap package?
Here is what one of the documentations for the data sets looks like:
#' Fry's 100 Most Commonly Used English Words
#'
#' A stopword list containing a character vector of stopwords.
#'
#' #details Fry's Word List: The first 25 make up about one-third of all printed
#' material in English. The first 100 make up about one-half of all printed
#' material in English. The first 300 make up about 65\% of all printed
#' material in English."
#'
#'
#' #docType data
#' #keywords datasets
#' #name Top100Words
#' #usage data(Top100Words)
#' #format A character vector with 100 elements
#' #export
#' #references Fry, E. B. (1997). Fry 1000 instant words. Lincolnwood, IL:
#' Contemporary Books.
NULL
Note that I have the qdapDictionaries listed in the Depends section of the DESCRIPTION file but with R dev version a CRAN check reveals I need to still import the datasets from qdapDictionaries that are used within the parent qdap 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