0

こんにちは、いくつかの 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)
4

2 に答える 2

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

}))
于 2015-11-09T12:59:51.230 に答える