在闪亮的服务器上显示R控制台日志

时间:2015-02-18 09:10:20

标签: r shiny

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

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

就像我们打印进度时一样,是否有任何方法可以附加进度消息而不是显示新消息。

我搜查了实习,但在这方面找不到任何东西。

有没有人做过这种事情?任何帮助都可以得到赞赏。

2 个答案:

答案 0 :(得分:6)

那么可能有更好的方法适用于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服务器正在写,而不是你)

# 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或....),请务必刷新或重新打开文件

我希望这有帮助,也许你找到了更好的方法,让我们知道

答案 1 :(得分: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

相关问题