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

Usage of select_group_ui in a Shiny module #63

Open
shosaco opened this issue Mar 13, 2023 · 3 comments
Open

Usage of select_group_ui in a Shiny module #63

shosaco opened this issue Mar 13, 2023 · 3 comments

Comments

@shosaco
Copy link

shosaco commented Mar 13, 2023

Hi,

I cannot get select_group_ui() and select_group_server() get to work inside a shiny module.

I added ns() to the IDs but possibly the virtualSelectInput()s can't communicate properly due to wrong namespaces. I suggest this feature is not implemented, or am I missing something important?

The following minimal example is the standard example from the help page, with the difference that I change the code so that the select_group_ui() and -server() are used inside a module.

Any help would be appreciated.

library(shiny)
library(datamods)
library(shinyWidgets)

myModuleUI <- function(id){
  ns <- NS(id) 
  fluidRow(
    column(
      width = 10, offset = 1,
      tags$h3("Filter data with select group module"),
      shinyWidgets::panel(
        select_group_ui(
          id = ns("my-filters"),
          params = list(
            list(inputId = ns("Manufacturer"), label = "Manufacturer:"),
            list(inputId = ns("Type"), label = "Type:"),
            list(inputId = ns("AirBags"), label = "AirBags:"),
            list(inputId = ns("DriveTrain"), label = "DriveTrain:")
          )
        ),
        status = "primary"
      ),
      reactable::reactableOutput(outputId = ns("table"))
    )
  )
}
myModuleServer <- function(id){
  moduleServer(id, function(input, output, session){
    res_mod <- select_group_server(
      id = session$ns("my-filters"),
      data = reactive(MASS::Cars93),
      vars = reactive(c("Manufacturer", "Type", "AirBags", "DriveTrain"))
    )
    output$table <- reactable::renderReactable({
      reactable::reactable(res_mod())
    })
  })
  
}

ui <- fluidPage(
  # theme = bslib::bs_theme(version = 5L),
  myModuleUI("id")
)

server <- function(input, output, session) {
  myModuleServer("id")
}
shinyApp(ui, server)
@feddelegrand7
Copy link
Contributor

The following will work:

library(shiny)
library(datamods)
library(shinyWidgets)

myModuleUI <- function(id){
  ns <- NS(id) 
  fluidRow(
    column(
      width = 10, offset = 1,
      tags$h3("Filter data with select group module"),
      shinyWidgets::panel(
        select_group_ui(
          id = ns("my-filters"),
          params = list(
            Manufacturer = list(inputId = "Manufacturer", label = "Manufacturer:"),
            Type = list(inputId = "Type", label = "Type:"),
            AirBags = list(inputId = "AirBags", label = "AirBags:"),
            DriveTrain = list(inputId = "DriveTrain", label = "DriveTrain:")
          )
        ),
        status = "primary"
      ),
      reactable::reactableOutput(outputId = ns("table"))
    )
  )
}
myModuleServer <- function(id){
  moduleServer(id, function(input, output, session){
    res_mod <- select_group_server(
      id = "my-filters",
      data_r  = reactive(MASS::Cars93),
      vars_r  = reactive(c("Manufacturer", "Type", "AirBags", "DriveTrain"))
    )
    output$table <- reactable::renderReactable({
      reactable::reactable(res_mod())
    })
  })
  
}

ui <- fluidPage(
  # theme = bslib::bs_theme(version = 5L),
  myModuleUI("change")
)

server <- function(input, output, session) {
  myModuleServer("change")
}
shinyApp(ui, server)

@pvictor maybe it's a good idea to display the following example in the docs, in case one wants to apply the Select Group Module in the context of a Shiny Module?

@shosaco
Copy link
Author

shosaco commented Mar 13, 2023

The following will work:

Thanks! So the following points are important:

  • The inputId of the params list elements need to be exactly the same as the column names (that is the tricky one!)
  • no ns() for params list member inputIDs
  • ns() for select_group_ui(), but no ns() for select_group_server()
    The following is a minimal example
library(shiny)
library(datamods)
library(shinyWidgets)

myModuleUI <- function(id){
  ns <- NS(id) 
  tagList(shinyWidgets::panel(
    select_group_ui(
      id = ns("my-filters"),
      params = list(
        # inputIds must be column names of the data, without ns() function!
        list(inputId = "Manufacturer", label = "Manufacturer:"),
        list(inputId = "Type", label = "Type:")
      )      
    ),
    reactable::reactableOutput(outputId = ns("table")))
  )
  
}
myModuleServer <- function(id){
  moduleServer(id, function(input, output, session){
    res_mod <- select_group_server(
      id = "my-filters",
      data_r  = MASS::Cars93,
      vars_r  = c("Manufacturer", "Type")
    )
    output$table <- reactable::renderReactable({
      reactable::reactable(res_mod())
    })
  })
  
}

ui <- fluidPage(
  myModuleUI("id")
)

server <- function(input, output, session) {
  myModuleServer("id")
}
shinyApp(ui, server)

@pvictor
Copy link
Member

pvictor commented Mar 22, 2023

Thank you both
if you have ideas to improve the interface, I'm interested. In the meantime I will add the example for use in a shiny module.

Victor

johnsonra added a commit to IDSS-NIAID/Chemotaxis-Dashboard that referenced this issue Dec 4, 2023
See dreamRs/datamods#63 for differences in how this works in modularized code. Not intuitive.
johnsonra added a commit to IDSS-NIAID/Chemotaxis-Dashboard that referenced this issue Dec 5, 2023
See dreamRs/datamods#63 for differences in how this works in modularized code. Not intuitive.
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