I want to send the form data to server only when the submit button is clicked, so I use eventReactive
method. I also render the form elements u
Using updateRadioButtons()
unfortunately just updates the radio button in the ui without affecting the actual input$
value. To actually set the input$
value to a NULL
we can use Shiny.addCustomMessageHandler
.
To do this we can add a script to ui.R
tags$script("
Shiny.addCustomMessageHandler('resetValue', function(variableName) {
Shiny.onInputChange(variableName, null);
});
")
and then we utilize this message handler while updating the radio buttons in server.R
session$sendCustomMessage(type = "resetValue", message = "inputid")
Below are full implementations of this that I think address your question. Additionally I converted the observe
s to observeEvent
s since they have a specific event they're reacting to.
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
tags$script("
Shiny.addCustomMessageHandler('resetValue', function(variableName) {
Shiny.onInputChange(variableName, null);
});
"),
# Application title
titlePanel("Hello Shiny!"),
# Sidebar with a slider input for the number of bins
sidebarLayout(
sidebarPanel(
uiOutput("plot1Ui"),
uiOutput("plot2Ui"),
actionButton('goPlot', 'Enter')
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("plot")
)
)
))
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output, session) {
output$plot1Ui <- renderUI({
radioButtons(
inputId = "plot1",
label = "Plot 1:",
choices = c(
"Option 1" = "1",
"Option 2" = "2",
"Option 3" = "3"
),
selected = character(0),
inline = FALSE
)
})
output$plot2Ui <- renderUI({
radioButtons(
inputId = "plot2",
label = "Plot 2:",
choices = c(
"Option A" = "A",
"Option B" = "B",
"Option C" = "C"
),
selected = character(0),
inline = FALSE
)
})
observeEvent(input$plot1, {
updateRadioButtons(session,
inputId = "plot2",
label = "Plot 2:",
choices = c(
"Option A" = "A",
"Option B" = "B",
"Option C" = "C"
),
selected = character(0),
inline = FALSE)
session$sendCustomMessage(type = "resetValue", message = "plot2")
})
observeEvent(input$plot2, {
updateRadioButtons(session,inputId = "plot1",
label = "Plot 1:",
choices = c(
"Option 1" = "1",
"Option 2" = "2",
"Option 3" = "3"
),
selected = character(0),
inline = FALSE)
session$sendCustomMessage(type = "resetValue", message = "plot1")
})
# Call this only when the button is pressed.
eventPlot <- eventReactive(input$goPlot, {
cat('\n')
cat('Plot 1:')
str(input$plot1)
cat('\n')
cat('Plot 2:')
str(input$plot2)
plot(rnorm(100))
})
output$plot <- renderPlot({
# render the plot from eventReactive.
eventPlot()
})
})