RenderPrint不会在observeEvent上更新

时间:2016-09-14 10:40:28

标签: shiny shinydashboard

点击操作按钮时,我可以在控制台中看到google工作表下载和日期,但renderPrint中的文件时间没有更新?

library(httr)
set_config( config( ssl_verifypeer = 0L ) )
library(googlesheets)
suppressMessages(library(dplyr))
library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    fluidRow(
      #One action button to download data from google spreadsheet
      actionButton("refreshbutton", label = "Refresh"),
      #two textoutput to show date of downloaded file
      textOutput("refreshdate")

      )
    )
  )
)

server <- function(input, output) {

observeEvent(input$refreshbutton, {
  #On click download data from google spreadsheet
  pulldata <- gs_key("19bPhlp7MjDZFNJcDUmHGJDxkh2h2U5j05S0c18HfBgE") %>% 
    gs_read_csv(ws="vs working", col_names=TRUE)
  #Write data in csv
  write.csv(pulldata, file = "attrition.csv")
  data <- read.csv(file="attrition.csv", header = TRUE)
  #capture modified time from csv
  filetime <- file.mtime("attrition.csv")
  print(filetime)
  #inform on completion after refresh
})

#print filetime in refreshdate1
output$refreshdate <- renderPrint({ 
  filetime # <- This is not updating???
})
}


shinyApp(ui, server)

除上述代码外,当谷歌电子表格下载时,我认为该网站应该进入灰色模式,表示刷新 - 这也没有发生?我的意思是它应该以某种方式显示新数据正在进行中直到完成?

1 个答案:

答案 0 :(得分:1)

它不起作用的原因是因为SELECT t1.reportID FROM REPL_SEND t1 (nolock) LEFT JOIN Repl_Finish t2 (nolock) ON t2.reportID = t1.reportID 变量的范围在filetime之内。您无法使用observeEvent在其范围外指定变量,而是使用observeEvent

刚检查一下,eventReactive即使在R控制台中也会给我一个错误,否则这就是你要寻找的有关反应性的解决方案。

gs_key

错误讯息:

server <- function(input, output) {
  observeEvent(input$refreshbutton, {
    #On click download data from google spreadsheet
    pulldata <- gs_key("19bPhlp7MjDZFNJcDUmHGJDxkh2h2U5j05S0c18HfBgE") %>%
      gs_read_csv(ws="vs working", col_names=TRUE)
    #Write data in csv
    write.csv(pulldata, file = "attrition.csv")
    #read.csv(file="attrition.csv", header = TRUE)
  })

  filetime <- eventReactive(input$refreshbutton, {
    file.mtime("attrition.csv")
  })

  #print filetime in refreshdate1
  output$refreshdate <- renderPrint({ 
    filetime()
  })
}