闪亮:无需等待函数完成即可将控制台输出打印到文本对象 [英] Shiny: printing console output to a text object without waiting for a function to finish
本文介绍了闪亮:无需等待函数完成即可将控制台输出打印到文本对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚开始接触闪亮和相当艰难的生活。
我需要让我的闪亮用户能够下载数据文件(实质上是查询数据库)。查询从一个时间转到另一个时间。SHINY本身支持日期,但不支持时间,因此我必须为用户提供一个文本字段,其中submitButton
我发现名为renderPrint
的内容似乎是个不错的选项,因为它声称要将打印到控制台的内容输出到控制台--然后我可以打印错误消息或显示来自数据下载过程的正常控制台输出。但随后它会保留打印的输出,直到整个过程完成。
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")
)
)
))
服务器.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())
})
}
)
提前感谢您的帮助。
推荐答案
我认为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)
这篇关于闪亮:无需等待函数完成即可将控制台输出打印到文本对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文