R 如何在文件更改时更新UI

R 如何在文件更改时更新UI,r,shinydashboard,R,Shinydashboard,您好,我正在使用几个excel文件构建shinydashboard 我在框的页脚插入了指向这些文件的链接,在更改excel文件中的某些内容时,我希望刷新shinydashboard。 我不想每次都运行整个R代码 文件内容更改后,如何重新呈现输出 这里有一个例子: sidebar <- dashboardSidebar( sidebarMenu( menuItem("Hello", tabName = "Hello", icon = icon("dashboard"))

您好,我正在使用几个excel文件构建shinydashboard

我在框的页脚插入了指向这些文件的链接,在更改excel文件中的某些内容时,我希望刷新shinydashboard。 我不想每次都运行整个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)

侧边栏要监视文件中的更改,您可以使用文件的cheksum,如下所示:

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

}))
库(闪亮)
图书馆(摘要)
#创建要读取的数据
write.csv(file=“~/iris.csv”,iris)
shinyApp(ui=shinyUI(
流动摄影(
侧边栏布局(
侧栏面板(
textInput(“路径”,“输入路径:”),
操作按钮(“读取文件”、“读取文件”),
标签$hr()
),
主面板(
tableOutput('内容')
)))
),
服务器=shinyServer(功能(输入、输出、会话){

文件如果我正确理解了这个问题,我会说您需要
reactiveFileReader
功能

来自以下网站的说明:

给定一个文件路径和读取函数,返回一个被动数据源 对于文件的内容

文件读取器将轮询文件的更改,一旦检测到更改,用户界面将得到反应性更新

以作为指南,我将示例中的服务器函数更新为以下内容:

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

server您可以使用tools::md5sum计算md5,当文件发生更改时,您可以更新UI。查看
tcltk2::tclTaskSchedule
以在计时器上运行任务,或者查看
invalidaterater
以检查数据是否已更改。我尝试了您的示例,但无法再对文件进行更改。我很抱歉我不知道如何在我的代码中使用它。我基本上是一个初学者。它对我有效,所以我不确定这是怎么回事。请稍后尝试将invalidate更改为5秒
InvalidateRater(5000,会话)
读取文件可能会阻止打开该文件。你能在应用程序中打开该文件吗?现在它也适用于我。我将尝试在代码中使用它。