如何遍历包含 Shiny App 的 R 脚本?

How to loop through R script containing a Shiny App?

提问人:Eric G 提问时间:11/17/2023 更新时间:11/17/2023 访问量:55

问:

我的脚本开头有一个闪亮的应用程序。按下按钮(“Los gehts”)后,将处理从剪贴板获取数据的脚本。目前,我需要运行脚本,闪亮的应用程序打开,我填写数据,单击按钮,脚本运行。对于下一个 ID,我必须再次启动 skript。

我希望它改变,以便:

  • 我只需要启动一次脚本,闪亮的应用程序就会打开并等待输入
  • 单击“Los gehts”,处理数据的部分将运行并循环,以便在处理数据后再次打开闪亮的应用程序
  • 这种情况一直持续到我点击“abbrechen”(取消)

我尝试将所有内容从“shinyApp(ui, server)”开始包装到结束的 while 循环。但是,这会阻止闪亮的 UI 加载,并且数据处理部分会自行循环。

那么:如何循环脚本,让闪亮的UI在每次循环时加载并等待用户呢?

library(shiny)

# Define global variables
if (!exists("ADI_ID")) {
  ADI_ID <- ""
}
if (!exists("timepoint")) {
  timepoint <- ""
}
if (!exists("counter")) {
  counter <- 1
}

stop <- FALSE

  ui <- fluidPage(
    textInput('adi_id', 'Enter ADI ID:'),
    textInput('timepoint', 'Enter Timepoint:', value = ""),
    actionButton('start_button', 'Los gehts'),
    actionButton('last_id_button', 'Letzter Proband'),  # Updated button label
    actionButton('cancel_button', 'Abbrechen'),
    textOutput('last_id_output'),
    textOutput('counter_output')  # New text output for displaying the counter
  )
  
  server <- function(input, output, session) {
    
    observeEvent(input$start_button, {
      if (input$adi_id == "" || input$timepoint == "") {
        showModal(
          modalDialog(
            title = "Error",
            "Please enter a valid ADI ID and timepoint before proceeding.",
            easyClose = TRUE
          )
        )
      } else {
        if (ADI_ID == input$adi_id) {
          counter <<- counter + 1  # Increase counter by 1
        } else {
          counter <<- 1  # Set counter to 0
        }
        
        ADI_ID <<- input$adi_id
        timepoint <<- input$timepoint
        stopApp()
      }
    })
    
    observeEvent(input$last_id_button, {
      if (counter >= 4) {
        showModal(
          modalDialog(
            title = "Warning",
            "Counter is 3 or higher. Are you sure you want to use the last ID?",
            footer = tagList(
              modalButton("Cancel"),
              actionButton("confirm_last_id", "Continue")
            ),
            easyClose = TRUE
          )
        )
      } else {
        updateTextInput(session, 'adi_id', value = ADI_ID)
        updateTextInput(session, 'timepoint', value = timepoint)  # Update timepoint input
      }
    })
    
    observeEvent(input$confirm_last_id, {
      updateTextInput(session, 'adi_id', value = ADI_ID)
      updateTextInput(session, 'timepoint', value = timepoint)  # Update timepoint input
      removeModal()
    })
    
    observeEvent(input$cancel_button, {
      stop <<- TRUE
      stopApp()
    })
    
    observe({
      if (stop) {
        stopApp()
      }
    })
    
    output$last_id_output <- renderText({
      paste("Last ID: ", ADI_ID)
    })
    
    output$counter_output <- renderText({
      paste("Counter: ", counter)
    })
  }
  
  shinyApp(ui, server)


  if (stop == TRUE) {
    stop("Abgebrochen")
  } else { 
my_data <- read_clip()
####### Rest of my script processing the data
r 闪亮

评论


答: 暂无答案