R 连续读取json文件
我希望连续读取json文件,例如每1000毫秒读取一次R 连续读取json文件,r,file,shiny,reactive,R,File,Shiny,Reactive,我希望连续读取json文件,例如每1000毫秒读取一次 我的一个选择是reactiveFileReader reactiveFileReader(间隔毫秒、会话、文件路径、readFunc等) 描述。 此函数似乎仅适用于csv文件,而不适用于json文件: file_data <- reactiveFileReader(intervalMillis = 1000, NULL, filePath = json_path, readFunc = read.json) observe({
reactiveFileReader(间隔毫秒、会话、文件路径、readFunc等)
描述。
此函数似乎仅适用于csv文件,而不适用于json文件:
file_data <- reactiveFileReader(intervalMillis = 1000, NULL, filePath = json_path, readFunc = read.json)
observe({
View(file_data())
})
getJsonData第一种方式是编写read.json
而不是read\u json
使用第二种方法,您可以将file.info(path)$mtime[1]
替换为runif(1,0,1e6)
。如果runif
连续两次返回相同的数字,您将非常不幸运
最后,第三种方法可以是:
server <- function(input, output, session){
autoInvalidate <- reactiveTimer(1000)
getJsonData <- reactive({
autoInvalidate()
read_json("path/to/file.json")
})
}
server下面是一篇关于如何将reactiveFileReader
与json文件一起使用的报告
我使用了一个future
将编写过程从闪亮的会话中分离出来——您只需用json输入替换它即可
library(shiny)
library(jsonlite)
library(datasets)
library(promises)
library(future)
plan(multisession(workers = 2))
ui <- fluidPage(
uiOutput("printResult")
)
server <- function(input, output, session) {
json_path <- tempfile(fileext = ".json")
write_json(NULL, json_path)
# async file writing process
future({
for(i in seq_len(nrow(iris))){
Sys.sleep(1)
write_json(iris[i,], json_path)
}
})
file_data <- reactiveFileReader(intervalMillis = 1000, NULL, filePath = json_path, readFunc = read_json)
output$printResult <- renderUI({
req(file_data())
})
}
shinyApp(ui, server)
库(闪亮)
图书馆(jsonlite)
图书馆(数据集)
图书馆(承诺)
图书馆(未来)
计划(多段(工人=2))
ui不是很干净,但是您可以返回一个随机数,而不是file.info(path)$mtime[1]
。我认为reactiveFileReader
是正确的方法,但是正如@Stéphanelant已经暗示的那样,base R中没有read.json
函数。请尝试invalidateLater
函数()
server <- function(input, output, session){
autoInvalidate <- reactiveTimer(1000)
getJsonData <- reactive({
autoInvalidate()
read_json("path/to/file.json")
})
}
library(shiny)
library(jsonlite)
library(datasets)
library(promises)
library(future)
plan(multisession(workers = 2))
ui <- fluidPage(
uiOutput("printResult")
)
server <- function(input, output, session) {
json_path <- tempfile(fileext = ".json")
write_json(NULL, json_path)
# async file writing process
future({
for(i in seq_len(nrow(iris))){
Sys.sleep(1)
write_json(iris[i,], json_path)
}
})
file_data <- reactiveFileReader(intervalMillis = 1000, NULL, filePath = json_path, readFunc = read_json)
output$printResult <- renderUI({
req(file_data())
})
}
shinyApp(ui, server)