2017-10-12 43 views
0

我在嘗試構建一個從S3存儲桶中獲取動態更新數據的Shiny應用程序。我目前可以提取數據,但不會自行更新。我已經通過reactiveFileReaderexamples的文檔,但無法弄清楚?幫助非常感謝!下面試圖將reactiveFileReader與AWS S3文件一起使用來更新Shiny應用

代碼:

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') 
    }) 
})` 

回答

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') 
    }) 


    }) 

希望這可以讓你更接近你想要什麼。祝你好運。 Cheers,nate