Help me with my shiny app. First, let me show you my code: EUCTRshiny.R: _______…
- User
Help me with my shiny app. First, let me show you my code:
EUCTR_shiny.R:
EUCTR_function <- function(input_var, status_update_fun) {
[TRUNCATED]
Update progress message
status_update_fun(cat(sprintf("\r%s%d/%d", progress_message, progress_index, total_codes)))
[TRUNCATED]
Save the workbook to the file
saveWorkbook(wb, file.path("outputs", paste0("EUCTR_", format(Sys.Date(), "%Y%m%d"), "_", format(Sys.time(), "%H%M"), ".xlsx")))
status_update_fun(metadata)
}
server.R:
library(shiny)
source("EUCTR_shiny.R")
shinyServer(function(input, output) {
Create a reactive value to store status messages
status_msg <- reactiveVal("")
When the Start button is pressed
observeEvent(input$start, { print("Start button pressed")
# Optional: startProgress for showing progress withProgress(message = 'Processing, please wait...', { # Call the EUCTR_shiny script function and pass the input variable and observe function for status update EUCTR_function(isolate(input$input_var), function(msg) { status_msg(paste0(status_msg(), msg, "\n")) }) }) output$shiny_output <- renderPrint({ status_msg() })}) })__________________________
ui.R:
library(shiny)
User interface definition
shinyUI(fluidPage(
App title
titlePanel("EUCTR Automagic Scraper 🤖🪄"),
Instruction text
p("This is a web scraper for the EU Clinical Trials Register."),
Input box for a one-line variable
textInput("input_var", "Enter the URL for the search query:"),
‘Start’ button
actionButton("start", "Start"),
Output console
verbatimTextOutput("shiny_output")
))
I want the UI to display a real-time message of what's on status_update_fun to inform the user of the progress.
- Assistant
To display real-time messages in your shiny app, you can use reactive values and
invalidateLater(). I've made the necessary changes to your server.R file below:server.R:
library(shiny) source("EUCTR_shiny.R") shinyServer(function(input, output) { # Create a reactive value to store status messages status_msg <- reactiveVal("") # When the Start button is pressed observeEvent(input$start, { print("Start button pressed") progress_index <- 0 total_codes <- 100 # You should replace this with the actual total_codes value # Update progress every 500 milliseconds observe({ invalidateLater(500) output$shiny_output <- renderPrint({ status_msg() }) }) # Optional: startProgress for showing progress withProgress(message = 'Processing, please wait...', { # Call the EUCTR_shiny script function and pass the input variable and observe function for status update EUCTR_function(isolate(input$input_var), function(msg) { progress_index <- progress_index + 1 status_msg(cat(sprintf("\r%s%d/%d", msg, progress_index, total_codes))) }) }) }) })
This code updates the real-time message every 500 milliseconds. Make sure to replace
total_codeswith the actual total number of codes when you update your app.