Martin
Martin

Reputation: 1201

Change a reactiveVal twice within one observeEvent handler

My Shiny application runs a script build.R when the user clicks the action button. I would like to inform the user not to close the app while the script is being run. When done, the user is informed that building was successful.

Here's my minimal reproducible code:

library(shiny)

ui <- fluidPage(
  actionButton("build", "run the buildscript"),
  textOutput("rstatus")
)

server <- function(input, output, session) {

  reloadstatus <- reactiveVal("")

  observeEvent(input$build, {

    reloadstatus("building, do not close the app")

    # in the actual app would be source("build.R")
    Sys.sleep(1)

    reloadstatus("successfully built")
  })

  output$rstatus <- renderText({
    reloadstatus()
  })
}

shinyApp(ui, server)

I guess it does not work because Shiny tries to first run the observeEvent till the end before altering the reactiveVal. How can I achieve my desired output (first "reloading..." second "successfully...")?

Upvotes: 0

Views: 119

Answers (1)

Geovany
Geovany

Reputation: 5687

You can use shinyjs to update the content of message.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  actionButton("build", "run the buildscript"),
  p(id = "rstatus", "")
)

server <- function(input, output, session) {

  observeEvent(input$build, {

    shinyjs::html("rstatus", "building, do not close the app")

    # in the actual app would be source("build.R")
    Sys.sleep(1)

    shinyjs::html("rstatus", "successfully built")
  })

}

shinyApp(ui, server)

Upvotes: 2

Related Questions