【问题标题】:How to update UI on file change如何在文件更改时更新 UI
【发布时间】:2015-11-02 14:42:59
【问题描述】:

您好,我正在使用几个 excel 文件构建一个闪亮的仪表板。

我在框的页脚中插入了指向这些文件的链接,并且我想在我的 excel 文件中更改某些内容时刷新闪亮仪表板。 我不想每次都运行整个 R 代码。

文件内容更改后如何重新渲染输出?

这里是一个例子:

sidebar <- dashboardSidebar(
sidebarMenu( menuItem("Hello", tabName = "Hello", icon = icon("dashboard"))
          ))

body <- dashboardBody(
 tabItems(

tabItem(tabName = "Hello",


        box(title = "my file", 
            footer = a("df.xlsx", href="df.xlsx" ) ,
            DT::dataTableOutput("df1"),style = "font-size: 100%; overflow: auto;",
            width = 12, hight = NULL, solidHeader = TRUE, collapsible = TRUE, collapsed = TRUE, status = "primary")
)))


ui <- dashboardPage(
 dashboardHeader(title = "My Dashboard"),
 sidebar,
body)


server <- function(input, output) {
  output$df1 <- renderDataTable({ 
df <- read_excel("df.xlsx")
DT::datatable(df, escape = FALSE, rownames=FALSE,class = "cell-border",
              options =list(bSort = FALSE, paging = FALSE, info = FALSE)
  )
  })
}



shinyApp(ui, server)

【问题讨论】:

  • 您可以使用 tools::md5sum 计算 md5,当文件更改时,您可以更新您的 UI。查看tcltk2::tclTaskSchedule 以在计时器上运行任务,或者查看invalidateLater 以检查数据是否已更改。

标签: r shinydashboard


【解决方案1】:

要监视文件中的更改,您可以像这样使用文件的校验和:

library(shiny)
library(digest)

# Create data to read
write.csv(file="~/iris.csv",iris)

shinyApp(ui=shinyUI(
  fluidPage(
    sidebarLayout(
      sidebarPanel(
        textInput("path","Enter path: "),
        actionButton("readFile","Read File"),
        tags$hr()
      ),
      mainPanel(
        tableOutput('contents')
      )))
),
server = shinyServer(function(input,output,session){
  file <- reactiveValues(path=NULL,md5=NULL,rendered=FALSE)

  # Read file once button is pressed
  observeEvent(input$readFile,{
    if ( !file.exists(input$path) ){
      print("No such file")
      return(NULL)
    }
    tryCatch({
      read.csv(input$path)
      file$path <- input$path
      file$md5  <- digest(file$path,algo="md5",file=TRUE)
      file$rendered <- FALSE
    },
    error = function(e) print(paste0('Error: ',e)) )
  })

  observe({
    invalidateLater(1000,session)
    print('check')

    if (is.null(file$path)) return(NULL)

    f   <- read.csv(file$path)
    # Calculate ckeksum
    md5 <- digest(file$path,algo="md5",file=TRUE)

    # If no change in cheksum, do nothing
    if (file$md5 == md5 && file$rendered == TRUE) return(NULL)

    output$contents <- renderTable({

      print('render')
      file$rendered <- TRUE
      f
    })
  })

}))

【讨论】:

  • 我尝试了您的示例,但无法再对文件进行更改。而且我不确定如何在我的代码中使用它。我几乎是个初学者。
  • 它对我有用,所以我不确定这是怎么回事。稍后尝试将无效更改为 5 秒 invalidateLater(5000,session) 可能是文件的读取阻止它打开。您可以在应用程序中打开文件吗?
  • 现在它也适用于我。我会尝试在我的代码中使用它。
【解决方案2】:

如果我正确理解了这个问题,我会说您需要 reactiveFileReader 函数。

来自function's reference page的描述:

给定文件路径和读取函数,返回响应式数据源 文件的内容。

文件阅读器将轮询文件的更改,一旦检测到更改,UI 就会响应式更新。

使用gallery example 作为指导,我将您示例中的服务器功能更新为以下内容:

server <- function(input, output) {                                                                                                                                                                                                                                   
  fileReaderData <- reactiveFileReader(500,filePath="df.xlsx", readFunc=read_excel)
  output$df1 <- renderDataTable({                                                                                                                                                                                                                                              
    DT::datatable(fileReaderData(), escape = FALSE, rownames=FALSE,class = "cell-border",                                                                                                                                                                                      
              options =list(bSort = FALSE, paging = FALSE, info = FALSE)                                                                                                                                                                                                       
  )                                                                                                                                                                                                                                                                            
  })                                                                                                                                                                                                                                                                           
}

这样,我保存到“df.xlsx”的任何更改几乎都会立即传播到 UI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多