尝试将reactiveFileReader与AWS S3文件一起使用来更新Shiny应用程序

时间:2017-10-12 03:29:01

标签: r amazon-s3 shiny

我正在尝试构建一个从S3存储桶中提取动态更新数据的Shiny App。我现在可以提取数据,但它不会自行更新。我查看了reactiveFileReaderexamples的文档,但无法弄清楚吗?非常感谢!!!

以下代码:

getFile<-function(){
  my_bucket <- 'globalrss'
  file <- paste0(as.character(getwd()),"/tmp")
  r <- aws.s3::save_object("bodytype.csv", my_bucket, file=file)
}

shinyServer(function(input, output, session) {
  fileReaderData <- reactiveFileReader(500, session, getFile(), readLines)

  output$fileReaderText <- renderText({
    text <- fileReaderData()
    length(text) <- 14
    text[is.na(text)] <- ""
    paste(text, collapse = '\n')
  })
})`

1 个答案:

答案 0 :(得分:0)

我认为您可能需要将文件读取部分代码放在observer中。也许是这样的:

shiny::shinyServer(function(input, output, session){

getFile<-function(){
  my_bucket <- 'globalrss'
  file <- paste0(as.character(getwd()),"/tmp")
  r <- aws.s3::save_object("bodytype.csv", my_bucket, file=file)
}

theFile<- getFile() # do this once, just so you have the data right away

# Now setup an observer that surrounds invalidate later and the file read code

observe({

shiny::invalidateLater(millis=30000, session=session) # run every 30 seconds
theFile<<- getFile() # get the contents of the s3 bucket, replace data
cat(file=stderr(), "updating data", "\n") # have this report actions to the console, can be removed later

})



output$fileReaderText <- renderText({
    text <- theFile
    length(text) <- 14
    text[is.na(text)] <- ""
    paste(text, collapse = '\n')
  })


    })

希望这能让你更接近你想要的东西。祝好运。干杯,nate