Home > Enterprise >  How to share objects in vignettes documentation in a R package?
How to share objects in vignettes documentation in a R package?

Time:05-31

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

CodePudding user response:

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