R 闪亮:如何创建确认对话框
我想问一下,是否有可能有一个确认对话框,由两个按钮组成,在屏幕上。R 闪亮:如何创建确认对话框,r,shiny,R,Shiny,我想问一下,是否有可能有一个确认对话框,由两个按钮组成,在屏幕上。 比方说,如果我单击删除按钮,则会弹出对话框。用户选择选项并返回。应用程序根据用户的选择进行操作。使用sweetalert #install_github("timelyportfolio/sweetalertR") library(shiny) library(sweetalertR) runApp(shinyApp( ui = fluidPage( actionButton("go", "Go"), swe
比方说,如果我单击删除按钮,则会弹出对话框。用户选择选项并返回。应用程序根据用户的选择进行操作。使用
sweetalert
#install_github("timelyportfolio/sweetalertR")
library(shiny)
library(sweetalertR)
runApp(shinyApp(
ui = fluidPage(
actionButton("go", "Go"),
sweetalert(selector = "#go", text = "hello", title = "world")
),
server = function(input, output, session) {
}
))
示例1
您可以这样做,注意代码取自
为杏子编辑
rm(list = ls())
library(shiny)
library(shinyBS)
campaigns_list <- letters[1:10]
ui =fluidPage(
checkboxGroupInput("campaigns","Choose campaign(s):",campaigns_list),
actionLink("selectall","Select All"),
bsModal("modalExample", "Yes/No", "selectall", size = "small",wellPanel(
actionButton("yes_button", "Yes"),
actionButton("no_button", "No")
))
)
server = function(input, output, session) {
observeEvent(input$no_button,{
updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list)
})
observeEvent(input$yes_button,{
updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list,selected=campaigns_list)
})
}
runApp(list(ui = ui, server = server))
rm(list=ls())
图书馆(闪亮)
图书馆(shinyBS)
活动列表我修改了部分代码以调用
js_string <- 'confirm("Are You Sure?");'
session$sendCustomMessage(type='jsCode', list(value = js_string))
发送“确认”对话框返回的值。然后我只是检查输入$deleteConfirmChoice的值,以确定要执行的操作。
非常感谢你!我现在了解了如何在R和Javascript之间发送和接收消息。ShinyBS和Javascript都不是必需的。诀窍是使用modalDialog
并将页脚设置为多个标记的tagList
,通常是一个actionButton
用于删除,一个modalButton
用于取消。下面是一个MWE
应用程序R
注意,如果使用闪亮的模块,则必须使用session$ns(“inputID”)
而不是ns(“inputID”)
。看。可能值得一看吗?我不能用两个按钮创建一个窗口。你知道怎么做吗?@Porkchop-Hi我想用你的第二个例子,用闪亮的BS。当我运行这个示例时,我意识到选择的控制仍然在于“selectall”链接,而不是bsModals的“yes”或“No”选项。是否可以将控件传递到“是”或“否”,即使selectall选择了所有选项,如果我在模式窗口中单击“否”,它也会重置复选框的选择。这是这里最清楚、最好的答案。它还具有高度的可扩展性,非常优雅,不需要更多的库,只需要闪亮。我可以确认它也能用,就像一个符咒。谢谢
rm(list = ls())
library(shiny)
library(shinyBS)
campaigns_list <- letters[1:10]
ui =fluidPage(
checkboxGroupInput("campaigns","Choose campaign(s):",campaigns_list),
actionLink("selectall","Select All"),
bsModal("modalExample", "Yes/No", "selectall", size = "small",wellPanel(
actionButton("yes_button", "Yes"),
actionButton("no_button", "No")
))
)
server = function(input, output, session) {
observeEvent(input$no_button,{
updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list)
})
observeEvent(input$yes_button,{
updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list,selected=campaigns_list)
})
}
runApp(list(ui = ui, server = server))
js_string <- 'confirm("Are You Sure?");'
session$sendCustomMessage(type='jsCode', list(value = js_string))
tags$script(
HTML('
Shiny.addCustomMessageHandler(
type = "jsCode"
,function(message) {
Shiny.onInputChange("deleteConfirmChoice",eval(message.value));
})
')
)
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)