dreamRs / datamods

Shiny modules to import and manipulate data into an application or addin
https://dreamrs.github.io/datamods/
GNU General Public License v3.0
138 stars 35 forks source link

Passing `select_group_ui`'s `params` in Modal window #80

Closed radovan-miletic closed 11 months ago

radovan-miletic commented 11 months ago

Hi Victor,

I'm having trouble with passing select_group_ui's params options to Modal window. I was wondering what I'm missing here.

Shiny app reprex code:

library(shiny)
library(shiny.fluent)
library(datamods)
library(shinyWidgets)

ui_mod <- function(id) {
    ns <- NS(id)
    tagList(
        reactOutput(ns("modal")),
        PrimaryButton.shinyInput(ns("showModal"), text = "Show modal")        
    )
}

server_mod <- function(id) {
    moduleServer(id, function(input, output, session) {
        ns <- session$ns
        modalVisible <- reactiveVal(FALSE)
        observeEvent(input$showModal, modalVisible(TRUE))
        observeEvent(input$hideModal, modalVisible(FALSE))
        output$modal <- renderReact({
            Modal(isOpen = modalVisible(),
                  Stack(tokens = list(padding = "15px", childrenGap = "10px"),
                        div(style = list(display = "flex"),
                            Text("Title", variant = "large"),
                            div(style = list(flexGrow = 1)),
                            IconButton.shinyInput(
                                ns("hideModal"),
                                iconProps = list(iconName = "Cancel")
                            ),
                        ),
                        div(
                            fluidRow(
                                column(
                                    width = 10, offset = 1,
                                    tags$h3("Filter data with select group module"),
                                    shinyWidgets::panel(
                                        select_group_ui(
                                            id = "my-filters",
                                            # params = NULL,
                                            params = list(
                                                list(inputId = "Manufacturer", label = "Manufacturer:"),
                                                list(inputId = "Type", label = "Type:"),
                                                list(inputId = "AirBags", label = "AirBags:"),
                                                list(inputId = "DriveTrain", label = "DriveTrain:")
                                            )
                                        ),
                                        status = "primary"
                                    ) # ,
                                   # reactable::reactableOutput(outputId = "table") 
                                )
                            )
                        )
                  )
            )
        })
    })
}

ui <- bslib::page(
    ui_mod("test"),
    reactable::reactableOutput(outputId = "table")    
)

server <- function(input, output, session) {
    server_mod("test")
    res_mod <- select_group_server(
        id = "my-filters",
        data = reactive(MASS::Cars93),
        vars = reactive(c("Manufacturer", "Type", "AirBags", "DriveTrain"))
    )
    output$table <- reactable::renderReactable({
        reactable::reactable(res_mod())
    })
}

shinyApp(ui, server)

This will be the output if we switch params options to params = NULL: modal

Thank you !

pvictor commented 11 months ago

Hello,

I'm not very familiar with package shiny.fluent, your example produce a JavaScript error for me, I have :

 shiny.fluent  * 0.3.0      2023-01-24 [1] CRAN (R 4.3.2)
 shiny.react     0.3.0      2022-12-25 [1] CRAN (R 4.3.2)

Which version are you using ? Does this work with a simple selectInput ?

radovan-miletic commented 11 months ago

Thank you for a hint. Yes, a JavaScript error points to {shiny.react}. In fact, according to this issue "selectInput() doesn't work in React context."... "named lists won't work (...) in Stack() or other components. This is due to how R objects are translated to JS for the underlying JS library: named vectors/lists are translated to JS objects, unnamed vectors/lists are translated to JS arrays." Also, see open issue where the problem is narrowed. This limitation seems relevant for select_group_ui() too. I believe we can close this issue, if you agree. I hope I didn't waste your time.

pvictor commented 11 months ago

Thank you for the feedback. Closing the issue since I cannot do much here. Don't worry, feel free to open issue as you want.

Victor