Safeer Ullah
Safeer Ullah

Reputation: 31

How to to update data by clicking actionButton in R in runtime

I want to update output data on update button every time. Here is my code which show the output on update button for the first time I run the code but in runtime if the input is changed, the output is updated automatically.

library(shiny)

ui <- fluidPage(
  titlePanel("My Shop App"),
  
  sidebarLayout(
    sidebarPanel(
      helpText("Controls for my app"),
      
      selectInput("item", 
                  label = "Choose an item",
                  choices = list("Keyboard", 
                                 "Mouse",
                                 "USB",
      
      sliderInput("price", 
                  label = "Set Price:",
                  min=0, max = 100, value=10),
      
      actionButton ("update","Update Price")
    ),
    
    mainPanel(
      helpText("Selected Item:"),
      verbatimTextOutput("item"),
      helpText("Price"),
      verbatimTextOutput("price")
    )
  )
)

server <- function(input, output) {
  
  SelectInput <- eventReactive (input$update , {

    output$item = renderText(input$item)
    output$price = renderText(input$price)

  })

  output$item <- renderText(SelectInput())
  output$price <- renderText(SelectInput())
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Views: 937

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

Either create a dependency and put them into the reactive and return it:

server <- function(input, output) {
  
  SelectInput <- eventReactive(input$update,{
    list(item = input$item, price = input$price)
  })
  
  output$item <- renderText(SelectInput()$item)
  output$price <- renderText(SelectInput()$price)
}

Or you can isolate, but then you have to add the button reaction to each listener

server <- function(input, output) {

  output$item <- renderText({
    req(input$update)
    input$update
    isolate(input$item)
  })
  
  output$price <- renderText({
    req(input$update)
    input$update
    isolate(input$price)
  })
  
}

Upvotes: 1

Related Questions