gpt4 book ai didi

r - Shiny:如何创建确认对话框

转载 作者:行者123 更新时间:2023-12-02 09:25:50 26 4
gpt4 key购买 nike

我想问是否可以有一个确认对话框,由两个按钮组成, Shiny 。比如说,如果我单击“删除”按钮,则会弹出对话框。用户选择并返回。该应用程序根据用户的选择进行操作。

最佳答案

ShinyBS 和 Javascript 都不是必需的。诀窍是使用 modalDialog 并将页脚设置为多个标签的 tagList,通常是用于删除的 actionButtonmodalButton 取消。下面是 MWE

应用程序.R

library(shiny)

ui = fluidPage(
mainPanel(
actionButton("createfile", "Create"),
actionButton("deletefile", "Delete")
)
)

# Define server logic required to draw a histogram
server = function(session, input, output) {

observeEvent(input$createfile, {
showModal(modalDialog(
tagList(
textInput("newfilename", label = "Filename", placeholder = "my_file.txt")
),
title="Create a file",
footer = tagList(actionButton("confirmCreate", "Create"),
modalButton("Cancel")
)
))
})


observeEvent(input$deletefile, {
showModal(modalDialog(
tagList(
selectInput("deletefilename", label = "Delete a file", choices = list.files(pattern="*.txt"))
),
title="Delete a file",
footer = tagList(actionButton("confirmDelete", "Delete"),
modalButton("Cancel")
)
))
})

observeEvent(input$confirmCreate, {
req(input$newfilename)
file.create(input$newfilename)
removeModal()
})

observeEvent(input$confirmDelete, {
req(input$deletefilename)
file.remove(input$deletefilename)
removeModal()
})
}

# Run the application
shinyApp(ui = ui, server = server)

请注意,如果您使用 Shiny 模块,则必须使用 session$ns("inputID") 而不是 ns("inputID")。请参阅Tobias' answer here .

关于r - Shiny:如何创建确认对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31107645/

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