在闪亮的服务器上显示 R 控制台日志
Display R console logs on shiny server
我正在为数据分析操作构建闪亮的应用程序。一切正常。
我想知道有什么方法可以显示日志,即 R Studio 中发生的事情。就像 print() 消息或任何 R 控制台正在打印的消息。我需要在闪亮的应用程序中以交互方式显示所有这些 activity。
就像我们打印进度一样,有什么方法可以附加进度消息而不是显示新消息。
我在 interned 上搜索过,但没有找到这方面的任何信息。
有人做过这种事吗?任何善意的帮助都将不胜感激。
好吧,可能有更好的方法适用于您计算机上的 R & R Shiny 和运行在服务器上的 R Shiny -> library(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 或 ....),请务必刷新或重新打开文件
希望对您有所帮助,也许您找到了更好的方法,请告诉我们
相关答案,例如检查具有多个消息的功能日志。
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("\3\[[[:digit:]]{2}m","",replacedText) # removes ansicolor when present
return(replacedText)
}
)
output$main <- renderUI({
wellPanel(
h2("log")
,verbatimTextOutput("log")
)
})
}
shinyApp(ui = ui, server = server)
我正在为数据分析操作构建闪亮的应用程序。一切正常。
我想知道有什么方法可以显示日志,即 R Studio 中发生的事情。就像 print() 消息或任何 R 控制台正在打印的消息。我需要在闪亮的应用程序中以交互方式显示所有这些 activity。
就像我们打印进度一样,有什么方法可以附加进度消息而不是显示新消息。
我在 interned 上搜索过,但没有找到这方面的任何信息。
有人做过这种事吗?任何善意的帮助都将不胜感激。
好吧,可能有更好的方法适用于您计算机上的 R & R Shiny 和运行在服务器上的 R Shiny -> library(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 或 ....),请务必刷新或重新打开文件
希望对您有所帮助,也许您找到了更好的方法,请告诉我们
相关答案,例如检查具有多个消息的功能日志。
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("\3\[[[:digit:]]{2}m","",replacedText) # removes ansicolor when present
return(replacedText)
}
)
output$main <- renderUI({
wellPanel(
h2("log")
,verbatimTextOutput("log")
)
})
}
shinyApp(ui = ui, server = server)