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

Support package data #219

Closed
kamilzyla opened this issue Jul 2, 2021 · 5 comments
Closed

Support package data #219

kamilzyla opened this issue Jul 2, 2021 · 5 comments

Comments

@kamilzyla
Copy link
Contributor

It seems that box doesn't handle datasets exported from packages:

> box::use(datasets[mtcars])
Error in attach_list(spec, names(mod_exports)) : 
  Name “mtcars” not exported by datasets
> datasets::mtcars # Works fine

Perhaps the problem is lazy loading of data? This happens on other packages as well, e.g. box::use(zipcodeR[zip_code_db]).

@klmr
Copy link
Owner

klmr commented Jul 3, 2021

Yep, it’s because of lazy loading, which works completely different from regular exports, and is currently not supported.

It’s low priority but there should be a ticket to track it, so this is now it.

@klmr
Copy link
Owner

klmr commented Oct 24, 2021

Implemented now. To preview before the next release, install the dev build of ‘box’ via

install.packages('box', repos = 'https://klmr.r-universe.dev')

@nvelden
Copy link

nvelden commented Jun 26, 2023

Hello,

I'm encountering an issue where I'm able to load data in a Shiny app, but unable to do the same within a Shiny module. Here's a minimal working example:

My main app:

# Load necessary libraries
library(shiny)
library(shinydashboard)
library(SBGNview)

box::use(./table_module[table_module_UI, table_module])

# Define UI for application
ui <- dashboardPage(
  dashboardHeader(title = "Displaying First 10 Rows of Pathways.info"),
  dashboardSidebar(),
  dashboardBody(
    box(
      title = "Data", 
      status = "primary", 
      solidHeader = TRUE, 
      width = 12,
      table_module_UI("data_table")
    )
  )
)

# Define server logic
server <- function(input, output, session) {
  
  data("pathways.info", package = "SBGNview")
  
  # Call the table_module
  callModule(table_module, "data_table", pathways.info[1:10,])
}

# Run the application 
shinyApp(ui = ui, server = server)

And my table_module.R:

box::use(
  shiny[NS],
  SBGNview[...],
  DT[renderDT, DTOutput],
  utils[data])

# Define the UI for the table module
table_module_UI <- function(id) {
  ns <- NS(id)
  DTOutput(ns("table"))
}

# Define the server for the table module
table_module <- function(input, output, session, data) {
  output$table <- DT::renderDT({
    # Load pathways.info data
    data
  })
}

In the main app, I'm able to load the pathways.info dataset from the SBGNview package. However, when I try to load the data in the table_module, the data does not load.

# Load necessary libraries
library(shiny)
library(shinydashboard)
library(SBGNview)

box::use(./table_module[table_module_UI, table_module])

# Define UI for application
ui <- dashboardPage(
  dashboardHeader(title = "Displaying First 10 Rows of Pathways.info"),
  dashboardSidebar(),
  dashboardBody(
    box(
      title = "Data", 
      status = "primary", 
      solidHeader = TRUE, 
      width = 12,
      table_module_UI("data_table")
    )
  )
)

# Define server logic
server <- function(input, output, session) {
  
  # Call the table_module
  callModule(table_module, "data_table")
}

# Run the application 
shinyApp(ui = ui, server = server)
# Load necessary libraries
box::use(
  shiny[NS],
  SBGNview[...],
  DT[renderDT, DTOutput],
  utils[data])

# Define the UI for the table module
table_module_UI <- function(id) {
  ns <- NS(id)
  DTOutput(ns("table"))
}

# Define the server for the table module
table_module <- function(input, output, session) {
  output$table <- DT::renderDT({
    
    # Load pathways.info data
    data("pathways.info", package = "SBGNview")
    
    # Extract first 10 rows
    pathways_data <- pathways.info[1:10,]
    
    output$table <- DT::renderDT({
      pathways_data
    })
  })
}

Warning: Error in exprFunc: object 'pathways.info' not found

Any ideas as to why this is happening and how I could fix it?

@klmr
Copy link
Owner

klmr commented Jul 11, 2023

I can’t reproduce this because I can’t install ‘SBGNview’ (in particular, its transitive dependency ‘Rgraphviz’ fails to compile).

But there are a few issues with the second code you’ve posted that need to be fixed:

  1. data() will load the data independently of box::use() (in fact, it should work even if you didn’t load the package via box::use()).
  2. But data() loads the data in the global environment. This is a deficiency in data(), but it’s unrelated to ‘box’ (in fact, you get the same effect without using ‘box’!). So to load the data inside a Shiny module you’d need to use data("pathways.info", package = "SBGNview", envir = environment()).
  3. You have an error in table_module: you have output$table <- DT::renderDT() twice, one nested inside the other. Remove the outer invocation.

To summarise, the issue is three-fold:

  1. ‘SBGNview’ uses LazyData: false, which means that data is not loaded when the package is loaded. It needs to be explicitly loaded via data().
  2. But data() loads data into the global namespace by default, unless envir = environment() is specified.
  3. And Shiny modules are intentionally isolated from the global environment, so they don’t see objects that are loaded into the global environment.

This is unrelated to ‘box’, you’d have the same issue if you tried loading the package via library() instead of box::use() (and in fact neither of these expressions loads the package data).

@nvelden
Copy link

nvelden commented Jul 11, 2023

Thanks a lot! Loading the data using data("pathways.info", package = "SBGNview", envir = environment()) in the Shiny box module solved my issue.

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

No branches or pull requests

3 participants