0

I'm creating a Shiny app where user input is parsed and then used to create specific plots. These plots should only be created when the user pushes a corresponding button. The text however should be parsed immediatly when user input changes.

Simplified example:

library(shiny)

ui <- fluidPage(
  textAreaInput("txt", "Parse this data:"),
  actionButton("go", "Do something with data"),
  textOutput("out")
)

server <- function(input, output, session) {
  data <- reactive({
    message("Parsing data...")
    toupper(input$txt)
  })
  
  observeEvent(input$go, {
    output$out <- data
  })
}

shinyApp(ui, server)

The "Parsing data..." message initially only executes when pushing the button, not when changing the input of the textAreaInput.

On top of that, after pushing the button once, the input is parsed immediatly when changed but the output is updated immediatly as well. This also shouldn't happen, it should only change when the button is pressed again.


EDIT

With YBS's answer I found a solution, and using isolate() I don't even need an extra variable:

server <- function(input, output, session) {
  data <- reactiveVal()
  
  observeEvent(input$txt, {
    message("Parsing data...")
    data(toupper(input$txt))
  })

  observeEvent(input$go, {
    output$out <- renderText({ isolate( data() ) })
  })
}

1 Answers1

0

Try this

library(shiny)

ui <- fluidPage(
  textAreaInput("txt", "Parse this data:"),
  actionButton("go", "Do something with data"),
  textOutput("out")
)

server <- function(input, output, session) {
  rv <- reactiveVal()
  data <- reactive({
    message("Parsing data...")
    toupper(input$txt)
  })
  
  observeEvent(input$go, {
    rv(data())
  })
  output$out <- renderText({rv()})
}

shinyApp(ui, server)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Thanks, now it only shows the graph (i.e. in this example the text) when the button is clicked. However, it also still only parses the text (in this example toupper()) when the button is pressed. I'd like the parsing to be done immediatly when the input in the textarea is changed, but only show the result when the button is pressed. – Wouter van Snippenburg Mar 02 '21 at 07:16