RenderPrint 未在 observeEvent 上更新

RenderPrint not updating on observeEvent

单击操作按钮时,我可以在控制台中看到 google sheet 下载和打印日期,但 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)

连同上面的代码,当 Google spreadsheet 正在下载时,我假设站点应该进入灰色模式以指示刷新 - 这也没有发生?我的意思是它应该以某种方式显示新数据正在处理中直到完成?

它不起作用的原因是因为 filetime 变量的范围在 observeEvent 之内。您不能使用 observeEvent 为超出其范围的变量赋值,而是使用 eventReactive

刚刚检查过,gs_key 即使在 R 控制台中也会为我抛出错误,否则这就是您正在寻找的有关反应性的解决方案。

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

错误信息:

Expected content-type:
application/atom+xml; charset=UTF-8
Actual content-type:
text/html; charset=UTF-8