Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accessing data in golem shiny app #413

Closed
novica opened this issue Apr 20, 2020 · 2 comments
Closed

accessing data in golem shiny app #413

novica opened this issue Apr 20, 2020 · 2 comments

Comments

@novica
Copy link
Contributor

novica commented Apr 20, 2020

I have a golem shiny app that reads from a dataset and returns a table.

So in the ui I have tableOutput(outputId = "head"), and in the server I have

dataset <- head(x)
output$head <- renderTable({ dataset }) 

where x is the iris dataset stored in the data folder with usethis::use_data.

I am installing it with devtools::install(). When I do myapp::run_app() I get an error:

Listening on http://127.0.0.1:5108
Warning: Error in head: object 'x' not found
  53: head
  52: server
Error in head(x) : object 'x' not found

When I change the line
dataset <- head(x)
to
dataset <- head(myapp::x) and reinstall myapp::run_app() runs successfully.

Again, I am not sure if maybe I am doing something wrong with the golem setup, or this is how it is supposed to be.

@ColinFay
Copy link
Member

ColinFay commented Jun 21, 2024

Hey,

Thanks for reporting.

Yes, this is not an issue with golem itself but with R packages.

I've written a reprex here: https://github.com/ColinFay/datapackagewithoutexplicitnamespacing

The TLDR is: add explicit namespacing pkg::dataset to your dataset, because pkg::function() doesn't have access to the dataset before you library("pkg").

@moodymudskipper
Copy link

Hey Colin I think it's because datasets under data/ are not stored in the package's namespace but in the lazydata env.
When calling library you make them visible because they're copied in the package env on the search path, this is brittle however if you use data from the package, because the global environment could override those.

I believe defining a dataset as internal should solve this problem more robustly, see Hadley's take there: https://r-pkgs.org/data.html#sec-data-sysdata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants