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

selectInput not working anymore within renderDataTable in 1.8.0 #3965

Open
Arthfael opened this issue Jan 5, 2024 · 2 comments
Open

selectInput not working anymore within renderDataTable in 1.8.0 #3965

Arthfael opened this issue Jan 5, 2024 · 2 comments

Comments

@Arthfael
Copy link

Arthfael commented Jan 5, 2024

System details

Browser Version:

Output of sessionInfo():

> sessionInfo()
R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2022 x64 (build 20348)

Matrix products: default


locale:
[1] LC_COLLATE=English_United Kingdom.utf8  LC_CTYPE=English_United Kingdom.utf8    LC_MONETARY=English_United Kingdom.utf8
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.utf8    

time zone: Europe/Vienna
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] DT_0.31     shiny_1.8.0

loaded via a namespace (and not attached):
 [1] digest_0.6.33     later_1.3.2       R6_2.5.1          httpuv_1.6.13     fastmap_1.1.1     magrittr_2.0.3    cachem_1.0.8      memoise_2.0.1    
 [9] htmltools_0.5.7   lifecycle_1.0.4   promises_1.2.1    cli_3.6.2         xtable_1.8-4      sass_0.4.8        jquerylib_0.1.4   compiler_4.3.1   
[17] rstudioapi_0.15.0 tools_4.3.1       bslib_0.6.1       mime_0.12         ellipsis_0.3.2    yaml_2.3.8        Rcpp_1.0.11       crayon_1.5.2     
[25] jsonlite_1.8.8    htmlwidgets_1.6.4 rlang_1.1.2       crosstalk_1.2.1

Example application or steps to reproduce the problem

require(shiny)
require(DT)

# Stand alone:
ui <- fluidPage(
  titlePanel(tag("u", "Test"), "Test"),
  h3("Now what is happening this time!?"),
  h3("Is this working?"),
  selectInput("Test", "", 1:5, 1, width = "100px"),
  br(),
)
server <- function(input, output, session) {
  observeEvent(input$Test, {
    cat(input$Test, "\n")
    Value <<- input$Test
  }, ignoreNULL = FALSE)
  session$onSessionEnded(function() { stopApp() })
}
print(shinyApp(ui, server, options = list(launch.browser = TRUE)))
Value
# -> Yes, this works...

# ... but as part of sapply: no!
tstTbl <- data.frame(Sample = 1:5, Choice = 1)
ui <- fluidPage(
  tags$head(tags$style(HTML("table {table-layout: fixed;"))), # So table widths can be properly adjusted!
  titlePanel(tag("u", "Test2"), "Test2"),
  DTOutput("tstTbl"),
  h3("And now!?"),
  br(),
)
server <- function(input, output, session) {
  tstTbl2 <- tstTbl
  tstTbl2$Choice <- sapply(seq_len(nrow(tstTbl2)), function(x) {
    as.character(selectInput(paste0("Test_", x), "", 1:5, 1, width = "100px"))
  })
  output$tstTbl <- renderDataTable(server = FALSE, escape = FALSE, selection = "none", editable = TRUE,
                                    rownames = FALSE,
                                    options = list(
                                      dom = 't', paging = FALSE, ordering = FALSE,
                                      autowidth = TRUE,
                                      scrollX = TRUE,
                                      preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                                      drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
                                    ), {
                                      tstTbl2
                                    })
  sapply(seq_len(nrow(tstTbl)), function(x) {
    id <- paste0("Test_", x)
    observeEvent(input[[id]], {
      cat(input[[id]], "\n")
      #tstTbl$Choice[x] <<- input[[id]]
    }, ignoreNULL = FALSE)
  })
  session$onSessionEnded(function() { stopApp() })
}
print(shinyApp(ui, server, options = list(launch.browser = TRUE)))

Describe the problem in detail

I am using a shiny app to edit tables of a variable number of samples, where for each sample I have to select one of several possible values. Up to now, I have been able to use sapply to create a selectInput() per sample, and similarly to create all corresponding observeEvent() using a second sapply. However, this has suddenly stopped working, see reprex above.
EDITS:

  • As tested in my comment below, the issue is not the interaction between selectInput and multiple observeEvent instances generated through sapply (which works), but between selectInput and renderDataTable.
  • I can now confirm that this is a recently introduced issue: downgrading to shiny_1.7.5 and DT_0.29 removes the issue - I have not investigated further.
@Arthfael
Copy link
Author

Arthfael commented Jan 9, 2024

Clearly I had not diagnosed the problem well: this is a problem of how selectInput interacts not with sapply,
but with DT:

# Testing interaction with sapply without table:
Values <- list(Test_1 = NA, Test_2 = NA)
ui <- fluidPage(
  titlePanel(tag("u", "Test"), "Test"),
  h3("Is just sapply working?"),
  selectInput("Test_1", "", as.character(1:5), "1", width = "100px"),
  selectInput("Test_2", "", as.character(1:5), "1", width = "100px"),
  actionButton("saveBtn", "Save"),
  br(),
)
server <- function(input, output, session) {
  sapply(1:2, function(x) {
    id <- paste0("Test_", x)
    observeEvent(input[[id]], {
      cat(input[[id]], "\n")
      Values[[id]] <<- input[[id]]
    }, ignoreNULL = FALSE)
  })
  observeEvent(input$saveBtn, { stopApp() })
  session$onSessionEnded(function() { stopApp() })
}
print(shinyApp(ui, server))
Values
# -> This works.

As a consequence, I have renamed the thread.

@Arthfael Arthfael changed the title selectInput not working anymore within sapply selectInput not working anymore within renderDataTable Jan 9, 2024
@Arthfael Arthfael changed the title selectInput not working anymore within renderDataTable selectInput not working anymore within renderDataTable in 1.8.0 Jan 9, 2024
@stla
Copy link

stla commented Feb 13, 2024

See #3979
There are two options: either you set selectize=FALSE in selectInput. Or if you want to use selectize, you have to get and include the HTML dependency and to initialise the select inputs - see the above linked issue.

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

2 participants