在shiny服务器上显示R控制台日志

9
我正在构建一个用于数据分析操作的闪亮应用程序。一切都很好。
我想知道是否有办法显示日志,即R Studio背后正在发生什么。像print()消息或任何R控制台正在打印的东西。我需要在闪亮应用程序中交互式地显示所有这些活动。
就像当我们打印进度时,是否有办法附加进度消息而不是显示新消息。
我已经在互联网上搜索了,但没有找到相关的信息。
有人做过这样的事情吗?任何帮助都可以感激。

1
你可以尝试使用 capture.output(...) 获取 R 控制台输出的内容。 - NicE
2个回答

9

也许有更好的方法适用于您的计算机和运行在服务器上的R&R Shiny ->库(log4r)。

library(log4r)
loggerDebug <- create.logger()
logfile(loggerDebug) <- 'data/debugData.log'
level(loggerDebug) <- 'INFO'

loggerServer <- create.logger()
logfile(loggerServer) <- 'data/serverData.log'
level(loggerServer) <- 'INFO'

# examples of levels
# debug(logger, 'A Debugging Message') # Won't print anything
# info(logger, 'An Info Message')
# warn(logger, 'A Warning Message')
# error(logger, 'An Error Message')
# fatal(logger, 'A Fatal Error Message')

请确保您在服务器上拥有正确的读写权限,否则它将无法工作。(请记住,R服务器正在写入而不是您)

# this depends on your security settings and rights
# talk to your UNIX ADMIN first
test <- list()
test$test <- "test"

# to change in linux / unix 
system("chmod a+rwx /...pathToYourApp..../data")
system("chmod a+rwx /...pathToYourApp..../data/debugData.log")

info(loggerDebug, paste('|   TEST   |',test$test,"|"))

# close after write (for security):
system("chmod u=-x,g-x,o-rwx /...pathToYourApp..../data")
system("chmod u=-x,g-x,o-rwx /...pathToYourApp..../data/debugData.log")

为了更加安全,您可以执行以下操作:
system("chattr +a /...pathToYourApp..../data/debugData.log")

这只允许向文件追加内容,因此不能对现有内容进行修改。(可以帮助说服UNIX管理员)

您可以在工作时打开日志文件,但如果使用RStudio或使用更新自身的更动态的软件包(如Sublime Text等),请确保刷新或重新打开文件。

希望这有所帮助,也许您找到了更好的方法,请告诉我们。


你好,我应该把这些代码放在哪里?放在UI还是服务器端? - Mohammad
在 server.r 文件中。更多信息请参见:https://cran.r-project.org/web/packages/log4r/log4r.pdf - irJvV
有人有Shinny脚本模式的示例吗? - gonzalez.ivan90

0

关于检查具有多个消息的函数日志的相关答案。

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(
    # FUNCTION OF INTEREST
    get(input$object)
    ,file= (outfile <- file(values[["outfile"]],"w"))
    ,type=input$type
  )
  close(outfile)
  message(values[["outfile"]]) # for console only
  })
  
  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) # removes ansicolor when present
    return(replacedText)
  }
  )
  
  output$main <- renderUI({     
    wellPanel(
    h2("log")
    ,verbatimTextOutput("log")
  ) 
  }) 
}

shinyApp(ui = ui, server = server)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接