6

I have written most of an R package and now wish to write a vignette that uses my own data, that is already in the package. The data is correctly stored as my_data.Rda in the Data folder, and when the package is loaded I can access it in the Console, for instance by using data(my_data).

My problem comes when, using usethis::use_vignette("my_vignette") , I want to include something like this (much more complex in practice, of course) in the vignette:

The mean of my_data is given by

```{r} data(my_data)
mean(my_data)
```

When I knit the vignette I get the message

"Error in assert_engine(is_numeric, x, .xname = get_name_in_parent(x), : object 'my_data' not found"

I have looked at this post: How to add external data file into developing R package? but that addresses external data.

What am I doing wrong?

I have created a minimal R package with the relevant Rmd file in the vignettes folder. link to Github

JeremyC
  • 445
  • 2
  • 14
  • 1
    Is this on github or anything? It would be much easier to help with a proper [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) Ideally it would be a minimal reproducible example. – MrFlick Nov 05 '18 at 16:45
  • @MrFlick I am attempting to make a reproducible example package available on Github, but my question is actually perfectly general. I have a package that I have written, it contains data, I want to use that data in a vignette to be written in knitr, but knitr works in its own environment that does not contain my package's data. What do I do? – JeremyC Nov 05 '18 at 23:03
  • Just looked at your vignette. Might be worth putting `library(my_package)` at the start of it ... – Russ Hyde Nov 06 '18 at 10:15
  • It worked without doing that. Besides, I read in Hadley's R Packages book that "...when building a package you should never use [library] inside a package". And I think that writing a vignette counts as being inside a package. Anyway, it turns out not to be needed. – JeremyC Nov 06 '18 at 10:37
  • I meant at the start of your vignette, not inside your package; I think you'll find your packages functions aren't available if you don't load your package into your vignette's session – Russ Hyde Nov 06 '18 at 11:02
  • For example, you have a function called `hello` in your basic package. Have a check whether this function is available to your vignette by adding a code block that calls `hello()` into your current vignette. The vignette can't be knitted if you don't also add `library(mre)` at the start of the vignette. The Wickham book is right however, your package code (stuff in `R/` or `tests/` etc) should not use `library()`. – Russ Hyde Nov 06 '18 at 11:10
  • Yes. I agree that the package must be loaded. I hesitated to load it in the Rmd document itself and therefore called library at the start of the session. – JeremyC Nov 06 '18 at 11:16

1 Answers1

5

I think you are supposed to use

data(my_dataset, package = "my_package")

to load your package's data into the session where your vignette is built.

Could you confirm that your datasets are stored inside the ./data directory of your package as *.rda files

Russ Hyde
  • 2,154
  • 12
  • 21