Search code examples
rr-package

How to add a list to exported data when building up R packages?


I save two lists as ".RData" in the my_package/data and have their documentation in /R. But after installation, I cannot load those two lists but it shows the help documentation. Is it possible to export a list in a R package? Or just able to matrix or something like that. I am confused by "one object in one .Rdata file"


Solution

  • Hadley's online book R Packages (and particularly the chapter on data) does a more thorough job of describing how to include data, but I'll walk through how to make arbitrary objects useful in a package.

    Up front:

    • one has private data (accessible to same-package functions without change, accessible outside of the package using :::) and public data (data available to both internal functions and whomever loads the package, optionally lazy-loaded)
    • public data is stored in the ./mypackage/data/ directory, and it shall use the *.rda extension (*.Rdata) does not work; a common convention (for version-control, convenience, maintainability) though not a strict requirement is for one object per .rda file, and named as the object name; below, I stored mypubliclist in ./mypackage/data/mypubliclist.rda;
    • private data should all be saved in one file, ./mypackage/R/sysdata.rda.

    Here's a sample package (I'm using devtools, but there are other ways to do so):

    devtools::create("~/StackOverflow/14489611/mypackage")
    # v Creating 'C:/Users/r2/StackOverflow/14489611/mypackage/'
    # v Setting active project to 'C:/Users/r2/StackOverflow/14489611/mypackage'
    # v Creating 'R/'
    # v Writing 'DESCRIPTION'
    # Package: mypackage
    # Title: What the Package Does (One Line, Title Case)
    # Version: 0.0.0.9000
    # Authors@R (parsed):
    #     * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
    # Description: What the package does (one paragraph).
    # License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
    #     license
    # Encoding: UTF-8
    # LazyData: true
    # Roxygen: list(markdown = TRUE)
    # RoxygenNote: 7.1.1
    # v Writing 'NAMESPACE'
    # v Changing working directory to 'C:/Users/r2/StackOverflow/14489611/mypackage/'
    

    I wrote a small script ./data-raw/mylist.R to create some random data. This file is merely for reproducibility, it is not referenced during package production or loading.

    set.seed(42) # R-4.0.2
    myprivatelist <- list(a = sample.int(10), b = sample.int(1e5))
    mypubliclist <- list(a=1:10, b=11:20)
    save(mypubliclist, file="~/StackOverflow/14489611/mypackage/data/mypubliclist.rda")
    save(myprivatelist, file="~/StackOverflow/14489611/mypackage/R/sysdata.rda")
    

    I also created an R file ./mypackage/R/data.R (you can name this file anything, but it must be under ./mypackage/R) to contain simple documentation for these lists:

    #' My private list
    #' @format list with random numbers
    "myprivatelist"
    
    #' My public list
    #' @format list with not-so-random numbers
    "mypubliclist"
    

    And now generate documentation:

    devtools::document("C:/Users/r2/StackOverflow/14489611/mypackage")
    # Updating mypackage documentation
    # Loading mypackage
    # Writing myprivatelist.Rd
    # Writing mypubliclist.Rd
    

    I'll build the package, restart R, install and then load the datasets.

    devtools::build("~/StackOverflow/14489611/mypackage")
    # v  checking for file 'C:\Users\r2\StackOverflow\14489611\mypackage/DESCRIPTION'
    # -  preparing 'mypackage':
    # v  checking DESCRIPTION meta-information
    # -  checking for LF line-endings in source and make files and shell scripts
    # -  checking for empty or unneeded directories
    #      NB: this package now depends on R (>= 3.5.0)
    #      WARNING: Added dependency on R >= 3.5.0 because serialized objects in  serialize/load version 3 cannot be read in older versions of R.  File(s) containing such objects: 'mypackage/R/sysdata.rda'  'mypackage/data/mypubliclist.rda'
    # -  building 'mypackage_0.0.0.9000.tar.gz'
    #    
    # [1] "C:/Users/r2/StackOverflow/14489611/mypackage_0.0.0.9000.tar.gz"
    
    ## restart R
    install.packages("C:/Users/r2/StackOverflow/14489611/mypackage_0.0.0.9000.tar.gz")
    # Installing package into 'C:/Users/r2/R/win-library/4.0'
    # (as 'lib' is unspecified)
    # inferring 'repos = NULL' from 'pkgs'
    # * installing *source* package 'mypackage' ...
    # ** using staged installation
    # ** R
    # ** data
    # *** moving datasets to lazyload DB
    # ** byte-compile and prepare package for lazy loading
    # ** help
    # *** installing help indices
    #   converting help for package 'mypackage'
    #     finding HTML links ... done
    #     myprivatelist                           html  
    #     mypubliclist                            html  
    # ** building package indices
    # ** testing if installed package can be loaded from temporary location
    # *** arch - i386
    # *** arch - x64
    # ** testing if installed package can be loaded from final location
    # *** arch - i386
    # *** arch - x64
    # ** testing if installed package keeps a record of temporary installation path
    # * DONE (mypackage)
    

    And let's see what worked:

    str(mypubliclist)
    # Error in str(mypubliclist) : object 'mypubliclist' not found
    #     x
    #  1. \-utils::str(mypubliclist)
    library(mypackage)
    str(mypubliclist)
    # List of 2
    #  $ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
    #  $ b: int [1:10] 11 12 13 14 15 16 17 18 19 20
    str(myprivatelist)
    # Error in str(myprivatelist) : object 'myprivatelist' not found
    #     x
    #  1. \-utils::str(myprivatelist)
    str(mypackage:::myprivatelist)
    # List of 2
    #  $ a: int [1:10] 1 5 10 8 2 4 6 9 7 3
    #  $ b: int [1:100000] 47128 16740 61605 73236 9091 62041 33700 59359 54789 54586 ...
    

    And its documentation, if you're curious. (I use print because otherwise it'll pop up in the other pane; this just dumps it to the console.)

    print(help("mypubliclist"))
    # mypubliclist             package:mypackage             R Documentation
    # My public list
    # Description:
    #      My public list
    # Usage:
    #      mypubliclist
    #      
    # Format:
    #      list with not-so-random numbers
    print(help("myprivatelist"))
    # myprivatelist            package:mypackage             R Documentation
    # My private list
    # Description:
    #      My private list
    # Usage:
    #      myprivatelist
    #      
    # Format:
    #      list with random numbers