简体   繁体   中英

Building an R package

I am currently building an R package.

I wish to provide example data. I am aware of the data directory created by package.skeleton() and how to load datasets, etc.

The problem is, I designed the functions to read files as input - the reason being that those using my package will typically be loading large datasets. As such, my functions read files line by line.

I am having an issue getting my package to pass the check stage as I am unclear how to get my functions to read the sample data files as opposed to first loading them and then passing the resulting table/data frame as an argument.

I hope this is clear.

Thanks in advance!

Edit ### Response to comment

My function takes a file as argument, ie,

example <- function(test_file){

test <- readLines(testFile)

....Do something to each line ...

}

I am unclear how to pass a file as opposed to a dataframe to my function with my R documentation code.

My test code in the documentation is like below

\examples{
library(new_package)
tester <- test(testfile=somefile)
}

This fails to execute as it appears R is expecting a data structure like:

data(somedataset)
testet <- example(somedataset)

The error results from the R CMD check process not being able to locate and read my files. Is there a way that I can construct my package so that the arguments for functions are files and not data structures and if so, how do I pass this files as arguments in my example code in the documentation.

What about this example function:

#' Example function
#' @param test_file file path
#' @examples \dontrun{
#' example(system.file('example_data.txt', package='package_name'))
#' }
example <- function(test_file) {
    test <- readLines(testFile)
    # whatever you wish to do
}

In details:

  • moving the example dataset from data dir (which is to hold datasets to be loaded without custom functions if I am right) to eg inst directory, which would end up placing your example file in the installed package's directory,
  • referencing the demo file with system.file in the docs,
  • and specifying the example with \\dontrun to eliminate any warnings caused by running those automatically but still showing up in the docs (although that latter is not needed IMHO if the above is OK).

I understand your dilemma with big data (I'm a biologist dealing with the same issues) so I would suggest not running the example. That would allow you to pass checks but you should also add a little note (as a comment or elsewhere in the function documentation) indicating why these are not straightforward example to run.

\examples{
\dontrun{
# Throw in your examples here
 }
}

This should allow you to pass checks (at least get around this issue) and make your package installable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM