r语言 - 上传闪亮日志文件到WebDAV服务器



我们在shinyapps.io上运行了几个闪亮的教育应用程序。为了跟踪使用情况,我们在本地试用了shinylogs包,效果很好。现在的目标是将shinylogs创建的日志文件上传到shinyapps上。

文件上传本身可以通过以下代码实现。请注意,出于安全原因,我没有透露我的真实身份。所以这个请求实际上不会为你工作。

username <- "xxx"
password <- "yyy"
file <- upload_file("test.txt")
PUT("https://fernuni-hagen.sciebo.de/public.php/webdav/test.txt", authenticate(username, password), body = file)

作为下一步,我用它创建了一个函数,它也运行良好。

upload <- function(filename){
body <- upload_file(filename)
PUT(paste0("https://fernuni-hagen.sciebo.de/public.php/webdav/", filename), authenticate(username, password), body = body)
}
upload("test.txt")

最后,我尝试将这段代码与shinylogs的track_usage命令结合起来。根据文档,store_custom模式应该是合适的。在网上找不到任何工作的例子,我无法找出正确的语法,虽然。

该函数应该将任何新的日志文件上传到WebDAV服务器,而不是预先指定一个文件。为了明确我的需求,我创建了这个简单的演示应用程序。
library(shiny)
library(shinylogs)
library(httr)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),

fluidRow(
# Sidebar with a slider input for number of bins  
sidebarLayout(
sidebarPanel(
sliderInput("bins1",
"Number of bins:",
min = 1,
max = 50,
value = 30)),
mainPanel(
# Show a plot of the generated distribution
plotOutput("distPlot1")
))),

fluidRow(
# Sidebar with a slider input for number of bins (and action button)
sidebarLayout(
sidebarPanel(
sliderInput("bins2",
"Number of bins:",
min = 1,
max = 50,
value = 30),
actionButton("go", "Update Plot")),
mainPanel(
# Show a plot of the generated distribution
plotOutput("distPlot2")
)))
)
# Define server logic required to draw a histogram
server <- function(input, output) {

username <- "xxx"
password <- "yyy"

track_usage(storage_mode = store_custom(FUN = function(logs){
body <- upload_file(logs)
url <- paste0("https://fernuni-hagen.sciebo.de/public.php/webdav/", logs)
PUT(url, authenticate(username, password), body = body)
}))

output$distPlot1 <- renderPlot({
# generate bins based on input$bins from ui.R
x    <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins1 + 1)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})

output$distPlot2 <- renderPlot({
# generate bins based on input$bins from ui.R (only upon click)     
input$go
x    <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = isolate(input$bins2) + 1)

# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
}
# Run the application 
shinyApp(ui = ui, server = server)

当我在本地运行这个闪亮的应用程序时,R控制台中没有错误消息,但文件永远不会到达WebDAV服务器。如果您能帮忙解决这个问题,我将不胜感激。

这就是我自己的解决方案。

# Configuration of logging directory
logsdir <- "~/logs"
# Create directory for log files, if not existent
if(!dir.exists(logsdir)) dir.create(logsdir)
# Usage tracking with JSON file being saved on Shiny and WebDAV Servers
track_usage(
storage_mode = store_custom(FUN = function(logs) {
jsondata <- toJSON(logs)
filename <- paste0("shinylogs_", session$token, ".json")
filepath <- paste0(logsdir, "/", filename)
write(jsondata, file = filepath)
body <- upload_file(filepath)
url <- paste0("https://fernuni-hagen.sciebo.de/public.php/webdav/", filename)
PUT(url, authenticate(username, password), body = body)
})
)

最新更新