我正在尝试扩展SBista在此处R Shiny: Click Button Within Datatable to Display Popup Twice in a row中给出的答案。我想包括一列动作按钮,这些按钮会触发带有selectInputs的弹出模式,具有存储在reactValues中并在再次单击按钮时会记住的选定选项
这是我的代码:
library(shiny)
library(DT)
library(shinyBS)
shinyApp(
ui <- fluidPage(
shinyjs::useShinyjs(),
#js function to reset a button, variableName is the button name whose value we want to reset
tags$script("Shiny.addCustomMessageHandler('resetInputValue', function(variableName){
Shiny.onInputChange(variableName, null);
});
"),
DT::dataTableOutput("data"),
uiOutput("modal")
),
server <- function(input, output,session) {
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
inputs
}
df <- reactiveValues(data = data.frame(
Assessment = shinyInput(actionButton, 10, 'button_', label = "Assessment", onclick = 'Shiny.onInputChange(\"select_button\", this.id)' ),
Value1 = 1:10,
Value2 = c("A", "B", "C", "D", "E"),
stringsAsFactors = FALSE,
row.names = 1:10
))
observeEvent(input$Enter, {
test$selected[[s$value]] <- input$text
})
output$data <- DT::renderDataTable(
df$data, server = FALSE, escape = FALSE, selection = 'none'
)
test <- reactiveValues(selected = lapply(1:10, function(x) NULL))
s <- reactiveValues(value = NULL)
observeEvent(input$select_button, {
s$value <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
output$modal <- renderUI({
tagList(
bsModal(paste('model', s$value ,sep=''), "Assessment", "select_button", size = "small",
selectInput(paste0("text", s$value), label = h3("Enter Assessment") , choices = c("1", "2"), multiple = TRUE),
actionButton("Enter", "Enter")
),
updateSelectInput(session, paste0("text", s$value), selected = test$selected[[s$value]])
)
})
toggleModal(session,paste('model', s$value ,sep=''), toggle = "Assessment")
##Reset the select_button
session$sendCustomMessage(type = 'resetInputValue', message = "select_button")
})
})
问题是当我单击一个actionButton并“输入”非默认选项时,应用程序中断。例如。选择第三个按钮,然后单击选项“ 2”,然后单击“ Enter”。发生这种情况时没有错误消息。
任何想法都非常感谢!