gpt4 book ai didi

R Shiny : Click Button Within Datatable to Display Popup Twice in a row

转载 作者:行者123 更新时间:2023-12-02 20:50:10 31 4
gpt4 key购买 nike

使用 RStudio 1.0.136 和 R Shiny 0.14.2,我尝试创建一个数据表,每行都有一个操作按钮,单击按钮时会出现一个弹出窗口。我的代码当前正在运行,但您不能连续单击同一按钮两次。我在下面发布了 2 个版本的示例代码:

library(shiny)
library(DT)
library(shinyBS)

shinyApp(
ui <- fluidPage(
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
))

output$data <- DT::renderDataTable(
df$data, server = FALSE, escape = FALSE, selection = 'none'
)


observeEvent(input$select_button, {
s <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
output$modal <- renderUI({
tagList(
bsModal(paste('model', s ,sep=''), "Assessment", "select_button", size = "small",
textAreaInput("text", label = h3("Enter Assessment") , value = "", width = "100%", height = "200px", resize = "none"),
actionButton("Enter", "Enter")
))
})
toggleModal(session,paste('model', s ,sep=''), toggle = "Assessment")

})
})

第二个代码是:

library(shiny)
library(DT)
library(shinyBS)

shinyApp(
ui <- fluidPage(
DT::dataTableOutput("data"),
bsModal("modalnew", "Assessment", "select_button", size = "small",
textAreaInput("text", label = h3("Enter Assessment") , value = "", width = "100%", height = "200px", resize = "none"),
actionButton("Enter", "Enter")
)

),

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
))

output$data <- DT::renderDataTable(
df$data, server = FALSE, escape = FALSE, selection = 'none'
)

observeEvent(input$select_button, {
s <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
toggleModal(session, "modalnew", toggle = "close")
})
})

弹出窗口中输入的内容目前无关紧要。任何有关如何允许连续单击同一按钮两次的建议将不胜感激。

最佳答案

我修改了你的代码。我使用了this中的js函数重置 select_button 值的链接。

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
))

output$data <- DT::renderDataTable(
df$data, server = FALSE, escape = FALSE, selection = 'none'
)


observeEvent(input$select_button, {
s <- as.numeric(strsplit(input$select_button, "_")[[1]][2])
output$modal <- renderUI({
tagList(
bsModal(paste('model', s ,sep=''), "Assessment", "select_button", size = "small",
textAreaInput("text", label = h3("Enter Assessment") , value = "", width = "100%", height = "200px", resize = "none"),
actionButton("Enter", "Enter")
))
})
toggleModal(session,paste('model', s ,sep=''), toggle = "Assessment")
##Reset the select_button
session$sendCustomMessage(type = 'resetInputValue', message = "select_button")
})
})

希望对你有帮助!

关于R Shiny : Click Button Within Datatable to Display Popup Twice in a row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489740/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com