【问题标题】:Display R console logs on shiny server在闪亮的服务器上显示 R 控制台日志
【发布时间】:2015-04-19 05:22:14
【问题描述】:

我正在为数据分析操作构建闪亮的应用程序。一切正常。

我想知道有什么方法可以显示日志,即 R Studio 中发生的事情。像 print() 消息或任何 R 控制台正在打印。我需要在闪亮的应用程序中以交互方式显示所有这些活动。

就像我们打印进度一样,有没有办法附加进度消息而不是显示新消息。

我搜索了实习生,但找不到这方面的任何内容。

有人做过这种事吗?任何帮助都将不胜感激。

【问题讨论】:

  • 您可以查看capture.output(...) 以了解 R 控制台正在打印的内容

标签: r shiny


【解决方案1】:

嗯,可能有更好的方法适用于您计算机上的 R & R Shiny 和在服务器上运行的 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 Server 正在编写,而不是您)

# 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 ADMIN)

您可以在工作时打开日志文件,如果您使用 RStudio 或使用更动态的包来更新自身(如 Sublime Text 或 ....),请务必刷新或重新打开该文件

希望对你有帮助,或许你找到了更好的方法,告诉我们

【讨论】:

【解决方案2】:

例如,检查具有多条消息的函数日志的相关答案。

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)

【讨论】:

    猜你喜欢
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多