我尝试使用logging
库向我的应用添加日志记录,但似乎很难将错误写入日志。假设我们有这个代码:
observeEvent(input$btn, {
write.csv(data, file = filename, row.names = FALSE)
})
如果发生错误,我们如何写一些东西来记录? 我设法做到了这样:
observeEvent(input$btn, {
tryCatch({
write.csv(data, file = filename, row.names = FALSE)
loginfo(paste0("Saved data to Server: ", input$DP_dataset_name))
}, error=function(err) {
logerror(paste0("Error when saving data - incorrect file name: ", input$file_name))
})
})
虽然它看起来并不优雅。
完整示例:
library(shiny)
library(datasets)
library(logging)
ui <- fluidPage(
textInput("dataset_name", "Dataset name", "testName"),
actionButton("Save", "Save data")
)
server <- function(input, output, session) {
basicConfig()
observeEvent(input$Save, {
tryCatch({
write.csv(mtcars, file = paste0(getwd(), "/", input$dataset_name, ".csv"), row.names = FALSE)
loginfo(paste0("Saved data: ", paste0(getwd(), "/", input$dataset_name, ".csv")))
}, error = function(err) {
logerror(paste0("Error when saving data - incorrect file name: ", input$dataset_name))
})
})
}
shinyApp(ui, server)
任何建议如何以更好的方式获得所需的结果?