闪亮:打印控制台输出到文本对象而无需等待功能完成

Mic*_*man 12 r shiny

我是Shiny的新手并且相当挣扎.

我需要让我的Shiny用户能够下载数据文件(基本上是查询数据库).查询从一次到另一次.Shiny本地支持日期,但不支持时间,因此我必须为用户提供带有文本字段的文本字段submitButton

问题是我需要让提交按钮对两个文本输入执行验证测试,并且:或者:如果任一输入无效,则返回错误消息2)下载数据,同时提供状态更新(数据可以采用小时轻松 - 我不想让用户挂起).

我发现了一些renderPrint看起来像是一个很好的选择,因为它声称输出打印到控制台的内容 - 然后我可以打印错误消息或显示数据下载过程中的正常控制台输出.但是它保持打印输出直到整个过程完成.

我想另一个可能的解决方案是返回到renderText然后直接从queryMagic函数中呈现文本- 当它经历数据下载过程时,它可以定期output$text用新文本更新.但我不确定如何做到这一点.

ui.R:

shinyUI(fluidPage(

  # Application title
  titlePanel("Demo Market Report"),

  fluidRow(

    column(4,
           h3("Extract Data"),
           helpText("Enter a start and end date/time of data to download. Be aware it takes about 10 minutes to download one hour of data.", strong("Date/time should be entered in yyyy-mm-dd hh:mm:ss format.")),
           textInput("fromDatetime", "From:", value = paste(with_tz(Sys.time(), "EST")-3600 )),
           textInput("toDatetime", "To:", value = paste(with_tz(Sys.time(), "EST"))),
           submitButton("Download Data Extract")
    ),
    column(4,
           textOutput("text1")
    )
  )


))
Run Code Online (Sandbox Code Playgroud)

server.R:

shinyServer(
  function(input, output) {

    logText <- reactive({
      if (input$fromDatetime == "a") {
        data = queryMagic(blah,blah,blah) #this just gets the data, function is already used in production, I'll feed the shiny input into it but that seems straightforward
        return("victory")
      }
      else return("invalid")
    })

    output$text1 <- renderPrint({
      paste(logText())
    })


  }
)
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助.

Mik*_*nen 4

我认为capture.output这是从控制台捕获文本的好解决方案。

 server <- function(input, output) {
  values <- reactiveValues()

  queryMagic <- function() {
    print("Warning")

    return("Data")
  }
  output$console <- renderPrint({
    logText()
    return(print(values[["log"]]))
    # You could also use grep("Warning", values[["log"]]) to get warning messages and use shinyBS package
    # to create alert message
  })

  logText <- reactive({
    values[["log"]] <- capture.output(data <- queryMagic())


  })
}

ui <- shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
    ),
    mainPanel(verbatimTextOutput("console"))
  )
))

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

  • 嗨@Mikael。看起来这仍然保留 queryMagic() 的所有输出,直到 queryMagic 完成。一旦 queryMagic 完成所有打印输出,就会出现在 UI 中。` queryMagic &lt;- function() { print("Warning1") Sys.sleep(3) print("Warning2") Sys.sleep(3) print("Warning3") return("Data") }` (2认同)