2015-11-02 56 views
-2

你好我正在用几个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) 
+0

你可以用tools :: md5sum来计算md5,当这个文件改变时你可以更新你的UI。看看'tcltk2 :: tclTask​​Schedule'在计时器上运行任务,或者'invalidateLater'来检查数据是否发生了变化。 –

回答

1

为了监视文件的变化,你可以使用的文件这样的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 
    }) 
    }) 

})) 
+0

我试过你的例子,但我无法再对文件进行修改。而且我不确定如何在代码中使用它。我几乎是一个初学者。 – Damata

+0

它适用于我,所以我不知道这是怎么回事。尝试将失效时间更改为5秒'invalidateLater(5000,session)'也许读取文件阻止了它的打开。你能打开应用程序中的文件吗? –

+0

现在它也适用于我。我会尝试在我的代码中使用它。 – Damata

1

如果我理解正确的问题,我说你需要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 。