Home > Software engineering >  How to add Update (Refresh) Button to the app.R
How to add Update (Refresh) Button to the app.R

Time:11-24

I added the button but the values will automatically change before I hit "Update Order", I don't know how to fix it. Should be like this:enter image description hereBelow is my code:

library(shiny)
ui <- fluidPage(
 titlePanel("My Simple App"),
 
 sidebarLayout(
   sidebarPanel(
     helpText("Controls for my app"),
     
     selectInput("fruitchoice", 
                 label = "Choose a fruit",
                 choices = list("Apples", 
                                "Oranges",
                                "Mangos", 
                                "Pomegranate"),
                 selected = "Percent White"),
     
     sliderInput("amt", 
                 label = "Order Amount:",
                 min=0, max = 100, value=20),
     
     actionButton ("Update","Update Order")
   ),
   
   mainPanel(
     helpText("Fruit Chosen:"),
     verbatimTextOutput("fruit"),
     helpText("Order Amount"),
     verbatimTextOutput("amt")
   )
 )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

   SelectInput <- eventReactive (input$Update , {
   runif(input$fruitchoice,amt)
  })
   output$fruit = renderText(input$fruitchoice)
   output$amt = renderText(input$amt)
}
# Run the application 
shinyApp(ui = ui, server = server)

CodePudding user response:

I will show you, how to rewrite your code to get this update behavior, however I would like to also get you know that this part of code:

SelectInput <- eventReactive (input$Update , {
   runif(input$fruitchoice,amt)
  })

Is wrong for three reasons: (1) object amt do not exists, you probably want input$amt; (2) even if you change amt to input$amt code won't work and you will get error; (3) now you are not using SelectInput in any place in your application, so there is no reason for this part to exists, however as I don't know what is your aim and how will look the final app, I'm not saying this is generally wrong.

Ok, so now about this update button. We will focus on this code:

output$fruit = renderText(input$fruitchoice)
   output$amt = renderText(input$amt)

Here you instruct program to (re)render text when input$fruitchoice or (in second line) when input$amt change, but you want to (re)render text only when user clicks the button, so you need two things - first, be sure that user clicked the button and do not (re)render text when one of input$ changes. This will work:

output$fruit = renderText({
    req(input$Update)
    isolate(input$fruitchoice)
    })
  output$amt = renderText({
    req(input$Update)
    isolate(input$amt)
    })

If I understand Shiny correctly, isolate() makes sure that text is not (re)rendering when input$ changes (however it has internally the new values) and req() makes sure that the input$Update was clicked; and when is clicked again, Shiny recomputes [(re)renders] text. It recomputes, because we didn't use isolate() on input$Update I think.

CodePudding user response:

There's a few things wrong in your code. I will give a bit of explanation for each one:

You are initializing with reactive inputs. By using renderText(input$...) you create a text output that updates automatically when your input updates. Automatically is the problem here, you don't want that. We are going to write an alternative method that stores the inputs in a separate variable that we only allow to be updated when the button is pressed. We initialize that variable like so:

rv <- reactiveValues(fruit = "Apples",
                       amt = 20)

EventReactive creates a reactive variable that can later be used in the code. Generally speaking what you want to use in these kind of scenarios is observeEvent. You can do so like this:

observeEvent (input$Update , {
    rv$fruit <- input$fruitchoice
    rv$amt <- input$amt
  })

We now have a list of variables under the name "rv" and an observeEvent that updates this variable every time the button gets pressed. All that is left to do is create the renderText which you can do like so:

output$fruit <- renderText(rv$fruit)
output$amt <- renderText(rv$amt)
  • Related