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

Input elements don't work as expected when in a grid container #2

Open
DivadNojnarg opened this issue Jan 8, 2025 · 1 comment
Open

Comments

@DivadNojnarg
Copy link
Contributor

Hi @pvictor!

When I put input element within a grid, they stop working. That said, I still see the class shiny-bound-input, so I don't see why it should not work:

library(shiny)
library(bslib)
library(ggplot2)
library(gridstackr)
library(sortable)

ui <- page_fillable(
  tags$h2("GridStack example"),
  layout_sidebar(
    sidebar = sidebar(
      width = 300,
      gridstackOutput("bucket")
    ),
    gs_trash(id = "mytrash", label = "Drag here to remove", height = "50px"),
    gridstackOutput("dashboard")
  )
)

server <- function(input, output, session) {

  output$bucket <- renderGridstack({
    gridstack(
      disableResize = TRUE,
      column = 1,
      options = list(
        acceptWidgets = TRUE,
        dragOut = TRUE
      ),
      gs_item(
        selectInput("variable", "Variable:",
                c("Cylinders" = "cyl",
                  "Transmission" = "am",
                  "Gears" = "gear")),
    tableOutput("data")
    ))
  })

  output$dashboard <- renderGridstack({
    gridstack(
      margin = "10px",
      cellHeight = "140px",
      resize_handles = "all",
      float = TRUE,
      options = list(
        acceptWidgets = TRUE
      ),
      trash_id = "mytrash"
    )
  })

  observeEvent(input$dashboard_layout, {
    print(input$dashboard_layout)
  })

  output$data <- renderTable({
    mtcars[, c("mpg", input$variable), drop = FALSE]
  }, rownames = TRUE)

}

shinyApp(ui, server)
@DivadNojnarg
Copy link
Contributor Author

With this second simple example working well:

library(shiny)
library(bslib)
library(ggplot2)
library(gridstackr)
library(sortable)

ui <- page_fillable(
  tags$h2("GridStack example"),
  layout_sidebar(
    sidebar = sidebar(
      width = 300,
      gridstackOutput("bucket")
    ),
    gs_trash(id = "mytrash", label = "Drag here to remove", height = "50px"),
    gridstackOutput("dashboard"),
    verbatimTextOutput("test")
  )
)

server <- function(input, output, session) {

  output$bucket <- renderGridstack({
    gridstack(
      disableResize = TRUE,
      column = 1,
      options = list(
        acceptWidgets = TRUE,
        dragOut = TRUE
      ),
      gs_item(
        radioButtons("dist", "Distribution type:",
               c("Normal" = "norm",
                 "Uniform" = "unif",
                 "Log-normal" = "lnorm",
                 "Exponential" = "exp")),
  plotOutput("distPlot")
    ))
  })

  output$dashboard <- renderGridstack({
    gridstack(
      margin = "10px",
      cellHeight = "140px",
      resize_handles = "all",
      float = TRUE,
      options = list(
        acceptWidgets = TRUE
      ),
      trash_id = "mytrash"
    )
  })

  output$test <- renderPrint(data.table::rbindlist(input$dashboard_layout, fill = TRUE))

  output$distPlot <- renderPlot({
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)

    hist(dist(500))
  })

}

shinyApp(ui, server)

There seem to be some issues with dependencies like selectize, ionrangeslider, ... We can workaround the first example by setting selectize = FALSE. shiny.bindAll, shiny.unbindAll and shiny.initializeInputs don't seem to be of much help.

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

1 participant