Ishan mahajan Ishan mahajan - 10 days ago 5
R Question

Display selectInput and sliderInput values on action button click

Created an App in which i wanted to take sliderInput and selectInput from user & display it when we click on action button. Intially when we run the app code works fine but when we change the values in sliderInput & selectInput output is displayed automatically without clicking on button.

shinyUI(fluidPage(

# Application title

titlePanel("Old Faithful Geyser Data"),

# Sidebar

sidebarLayout(

sidebarPanel(
sliderInput("tm", "select the interval", min = 0, max = 20,value = 10),
selectInput("samples", label = "Select the sample type", c("Sample A","Sample B","Sample C")),
actionButton("act", label = " Update" )
),

mainPanel(
textOutput("val"),
br(),
textOutput("sam")
)


)
))

shinyServer(function(input, output) {

observe(
if(input$act>0){
output$val <- renderText(
paste("You selected the value" ,input$tm)
)

output$sam <- renderText(input$samples)

}


)
})

I want to change the value only when we click action button.

Answer

Instead of observe, you can make your output values as eventReactive.

Here is the server side codes (as nothing in ui side has to be changed).

shinyServer(function(input, output) {

  val = eventReactive(input$act, {
    paste("You selected the value" ,input$tm)
  })

  sam = eventReactive(input$act, {
    input$samples
  })

  output$val = renderText( 
    val()
    )
  output$sam = renderText(
    sam()
  )
})