Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

Help me with my shiny app. First, let me show you my code: EUCTRshiny.R: _______…

  1. User

    Help me with my shiny app. First, let me show you my code:

    EUCTR_shiny.R:


    EUCTR_function <- function(input_var) {

    [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")))

    print(metadata, row.names = FALSE)

    }


    server.R:


    library(shiny)

    source("EUCTR_shiny.R")

    shinyServer(function(input, output) {

    When the Start button is pressed

    observeEvent(inputstart, { print("Start button pressed") # Call the EUCTR_shiny script function and pass the input variable outputshiny_output <- renderPrint({ EUCTR_function(input$input_var) }) })

    })


    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 include a real-time printing of what's printed inside the EUCTR_shiny.R script.

  2. Assistant

    To achieve the real-time printing of what is printed inside the EUCTR_shiny.R script, you can modify your code in the following way:

    server.R:

    library(shiny)
    
    source("EUCTR_shiny.R")
    
    shinyServer(function(input, output) {
      
      # 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
          output$shiny_output <- renderPrint({
            EUCTR_function(isolate(input$input_var))
          })
        })
      })
    })
    

    In the EUCTR_shiny.R script you can use message() instead of print() to show additional messages in the console. For example:

    EUCTR_shiny.R:

    EUCTR_function <- function(input_var) {
    
      # ...
    
      # Replace print() with message()
      message("Starting data scraping...")
    
      # ...
    
      # Replace print() with message()
      message("Scraping completed, preparing the output...")
    
      # 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")))
    
      message("Output saved, displaying the results...")
    
      # Replace print() with message()
      message(metadata, appendLF = FALSE)
    
      # Return the metadata so that it appears in the shiny_output
      return(metadata)
    }
    

    This should update the Shiny app's console output in real-time, as the messages get printed inside the EUCTR_shiny.R script.

  3. User

    print loop counter with \r character to overwrite previous print

    cat(paste0("Extracting EudraCT codes from page ", i, "/", number_pages), "\r")


    That's an example printing from the EUCTR_shiny.R script. How can i transform it to work the way you said?

  4. Assistant

    I see. If you want to print status messages like that within your EUCTR_shiny.R script, you need to create a reactive variable to store the messages, and use it in your script. Here's how you can change your code:

    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()
        })
      })
    })
    

    In the EUCTR_shiny.R script, you can modify the EUCTR_function to accept a new argument for the status update function:

    EUCTR_shiny.R:

    EUCTR_function <- function(input_var, status_update_fun) {
    
      # ...
    
      # Use status_update_fun to update the status_msg reactive variable
      status_update_fun("Starting data scraping...")
    
      # ...
    
      # Example of updating the status message within a loop
      for (i in 1:number_pages) {
        status_update_fun(paste0("Extracting EudraCT codes from page ", i, "/", number_pages))
        # ... Use i here for the processing of page i ...
      }
    
      # ...
    
      # Replace print() with message()
      status_update_fun("Scraping completed, preparing the output...")
    
      # 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")))
    
      # Replace print() with message()
      status_update_fun("Output saved, displaying the results...")
    
      # Append the metadata to the status_msg
      status_update_fun(toString(metadata))
    
      # Return the metadata so that it appears in the shiny_output
      invisible(metadata)
    }
    

    This way, your Shiny app's console output will be updated in real-time as the status_update_fun gets called inside the EUCTR_shiny.R script. Note that you might experience a slight delay in updating the output in the Shiny app depending on the processing time, but the console should be updated with the messages as they are printed inside the script.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.