Package creation: loading data into the package

Hi.
I am quite confused by loading data inside package into shiny environment. Yes, I went through Hadley's documentation. Basically I have spatial polygons which I saved as reg.rda to ./data. I need to load these data to my module at the start of the app.

I included an .R file into my ./R directory per Karl Broman's advice :

#' @docType data
#'
#' @usage data(reg)
"reg"

This is I what I though I need to do in order to load data into the global environment when starting the shiny application.
What am I missing?

1 Like

Did you remember to add LazyData: true to the file DESCRIPTION, as recommended in Karl's article? If yes, you should be able to load your package at the top of your Shiny script, and the data reg will be available for use.

As an example, I used RStudio to create a new Shiny project (File->New Project->New Directory->Shiny Web Application). This creates a small example to create a histogram from the data set faithful in the package datasets. Here is the relevant snippet:

        x    <- grav$pheno$T0
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')

To demonstrate how to use this with a non-built in R package, I used Karl's R/qtlcharts package. I loaded the package at the top of the script:

library(shiny)
library(qtlcharts)

And I changed the data set from faithful to grav:

        x    <- grav$pheno$T0

Because Karl set LazyData: true, this worked. Similarly, if you configured your package the same as Karl recommends, you should be able to load your package and then refer specifically to the reg data set in the Shiny code.

If that isn't working for you, could you please share the error message and/or a reproducible example?

1 Like

Thanks John.
Yeah, I did include Lazydata: true to the description. I also tried to be explicit and call the data inside the package with packagename::reg, but got the Warning: Error in : 'reg' is not an exported object from 'namespace:packagename'. I guess I will have to play around it with a bit more. I will need to invest some time in learning how to make a lightweight package example, because I am using golem framework for shiny development, which is a opposite of lightweight :slight_smile:

1 Like

Ah, I see the issue. You aren't using package data inside of a Shiny app. Your Shiny app is inside of a package. That changes things.

What is the name of the data object stored in reg.rda? In other words, when you run load("data/reg.rda"), what object(s) is/are loaded into your R session? If it loads reg, I would expect packagename::reg to work inside a function of the package.

For using data inside a package, I really like the blog post R packages - internal and external data by Mike Kane. The blog post by Karl is specifically dealing with "external" data that users load with data() (or automatically via lazy data). Do you want your users to be able to easily load the data into an R session? If not, then you can save reg in R/sysdata.rda, and then your package functions can simply refer to reg.

1 Like

Hi. Sorry, I have been out for a weekend. Yes, the problem was that the object inside reg.rda was called regions. I shuffled names for a bit, cause I already had regions() reactive object present.

Also, when building shinyapp inside a package, packagename::reg was needed to call the data inside the shiny.

Thanks for your help, John.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.