I was wondering if it is possible to create a popup dialog box interactive by using shiny (and shinyBS).
For example, I have a string and I want to change it and before doing a dialog box shows up asking if I really want to change it. In case I say "yes", it does it otherwise it discards the change. Here's my try but I found two issues: 1. if you click "yes" or "no", nothing changes 2. you always need to close the box by the bottom "close".
rm(list = ls())
library(shiny)
library(shinyBS)
name <- "myname"
ui =fluidPage(
textOutput("curName"),
br(),
textInput("newName", "Name of variable:", name),
br(),
actionButton("BUTnew", "Change"),
bsModal("modalnew", "Change name", "BUTnew", size = "small",
textOutput("textnew"),
actionButton("BUTyes", "Yes"),
actionButton("BUTno", "No")
)
)
server = function(input, output, session) {
output$curName <- renderText({paste0("Current name: ", name)})
observeEvent(input$BUTnew, {
output$textnew <- renderText({paste0("Do you want to change the name?")})
})
observeEvent(input$BUTyes, {
name <- input$newName
})
}
runApp(list(ui = ui, server = server))
Other proposals are more than welcome!!
Here's a proposition, I mainly changed the
server.R
:A couple of points:
I created a
reactiveValues
to hold the name that the person currently has. This is useful because you can then update or not update this value when the person clicks on the modal button. You can access the name usingvalues$name
.You can use
toggleModal
to close the modal once the user has clicked on yes or noYou can do something like this using
conditionalPanel
, I would further suggest adding a button to ask for confirmation oppose to instant update.@NicE provided a nice solution. I'm going to offer an alternative solution using the
shinyalert
package instead, which I believe results in easier to understand code (disclaimer: I wrote that package so may be biased).The main difference is that the modal creation is no longer in the UI and is now done on the server when the button is clicked. The modal uses a callback function to determine if "yes" or "no" were clicked.