从AWS S3存储桶读取的R reactFileFileReader

时间:2020-08-31 14:45:53

标签: r amazon-s3 shiny

我可以使用以下代码从S3存储桶中读取csv

aws.s3::s3read_using(read.csv,
                     stringsAsFactors=FALSE,
                     check.names=FALSE,
                     object=paste0(Sys.getenv("BUCKET_PREFIX"), "/a.csv"),
                     bucket = Sys.getenv("AWS_BUCKET_NAME"),
                     opts=bucket_opts
    )

我想将其更改为使用函数reactFileReader。我尝试以下操作均未成功,知道我在做什么错吗?

reactiveFileReader(
      intervalMillis = 10000,
      session= session,
      filePath = paste0(Sys.getenv("BUCKET_PREFIX"), "/a.csv"),
      readFunc = aws.s3::s3read_using,
      FUN = read.csv,
      stringsAsFactors=FALSE,
      check.names=FALSE,
      object=paste0(Sys.getenv("BUCKET_PREFIX"), "/a.csv"),
      bucket = Sys.getenv("AWS_BUCKET_NAME"),
      opts=bucket_opts
      )

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并通过一些解决方法解决了该问题:

global.R,您从闪亮的应用程序开始读取数据。

data_obj <- aws.s3::s3read_using(read.csv,
                     stringsAsFactors=FALSE,
                     check.names=FALSE,
                     object=paste0(Sys.getenv("BUCKET_PREFIX"), "/a.csv"),
                     bucket = Sys.getenv("AWS_BUCKET_NAME"),
                     opts=bucket_opts
    )

#bucket information
content_bucket <- aws.s3::get_bucket(bucket = Sys.getenv("AWS_BUCKET_NAME"))

在server.R中,您设置了一个计时器来检查S3上的数据是否已更改。

  shiny::observe({

shiny::invalidateLater(3600e3) #timer for every hour to check the following

#temporary bucket information
bucket_content_check <- aws.s3::get_bucket(bucket = Sys.getenv("AWS_BUCKET_NAME"))

#reload data if timestamp differs
    if (bucket_content$Contents$LastModified != bucket_content_check$Contents$LastModified) {
      
data_obj <<- aws.s3::s3read_using(read.csv,
                     stringsAsFactors=FALSE,
                     check.names=FALSE,
                     object=paste0(Sys.getenv("BUCKET_PREFIX"), "/a.csv"),
                     bucket = Sys.getenv("AWS_BUCKET_NAME"),
                     opts=bucket_opts
    )

#overwrite global bucket_content for next check
content_bucket <<- aws.s3::get_bucket(bucket = Sys.getenv("AWS_BUCKET_NAME"))

message('S3 data reloaded')
      }

    message('S3 bucket checked')
})