简体   繁体   中英

How to share objects in vignettes documentation in a R package?

I'm writing documentation for my R package using vignettes (which will be embedded in a pkgdown website.
My question is : if I create an R object in a chunk within a first "aa" vignette.

myobject <- mypkg::myfct()

How to reuse this object in a second vignette called "bb"?

verif <- myobject[myfilters,]

I get this error : myobject not found

This doesn't seem like a good idea. You can save this data in an internal RDA file, and load it in the second vignette. See Chapter 14 External data, R Pakcages .

So you'd have to:

  • Create a data_raw folder, and have a script that creates this object, then saves it. This is facilitated by usethis::use_data_raw , with like "myfct_example".
  • Then, at the bottom of this script in data_raw ensure that internal = TRUE ; This will save the object in R/sysdata.rda , instead of data/myfct_example (if you call that object that).
  • Ensure that you run the data_raw/myfct_example script every time you change myfct_example , so the new version get stored in R/sysdata.rda .
  • In both vignettes, you'll have this data object available, by mypkg:::myfct_example .

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