关于检查具有多个消息的函数日志的相关答案。
library(shiny)
ui <- fluidPage(
titlePanel("produce output or message"),
sidebarLayout(
sidebarPanel(
radioButtons(inputId = "object",
label = "Generate output (or message)",
choices = c("cars", "iris")
),
radioButtons(inputId = "type",
label = "Type of capture",
choices = c("message", "output")
,selected = "output"
),
),
mainPanel(
uiOutput("main")
)
)
)
server <- function(input, output, session) {
values<-reactiveValues()
observeEvent(input$object,{
filename <- tempfile(fileext=".txt")
filenamePath <- normalizePath(filename, mustWork = F)
values[["outfile"]] <- filenamePath
})
observeEvent(c(input$object,input$type),{
capture.output(
get(input$object)
,file= (outfile <- file(values[["outfile"]],"w"))
,type=input$type
)
close(outfile)
message(values[["outfile"]])
})
filenameR <- eventReactive(c(input$object, input$type),{
f<-values[["outfile"]]
})
output$log <- renderText({
rawText <- filenameR()
validate(need(try(readLines(rawText) ), message = FALSE) )
replacedText <- paste(readLines(rawText), collapse = "\n")
replacedText <- gsub("\\\033\\[[[:digit:]]{2}m","",replacedText)
return(replacedText)
}
)
output$main <- renderUI({
wellPanel(
h2("log")
,verbatimTextOutput("log")
)
})
}
shinyApp(ui = ui, server = server)
![enter image description here](https://istack.dev59.com/cynzX.webp)
capture.output(...)
获取 R 控制台输出的内容。 - NicE