對不起,我是Shiny新手。我最近試圖用反應式程序來完成一個項目。但是現在我面臨的一個問題無法弄清楚爲什麼會發生這種情況。反應式表達結果不同
首先,我創建一個反應性表達式,列出一個文件夾中的所有文件。
fullFilenames <- reactive({list.files(workDir, pattern="*.csv.gz", full.names=TRUE)})
然後,我觀察到一個按鈕事件來觸發重新掃描文件夾中的文件和更新updateCheckboxGroupInput
。
我在"print("File Scaned")"
設置了一個斷點。
當我們啓動應用程序時,複選框組正確顯示文件夾下的所有文件。
然後我刪除/添加工作文件夾下的文件,然後單擊重新掃描按鈕。但複選框組不會更新。當程序停在斷點處時,我檢查了值「fullFilenames」和「fullFilenames()」,發現fullFilenames已更新,但不是fullFilenames()。我試圖理解兩個值背後的邏輯是不同的,並找到了如何以正確的方式更新Checkbox組的方法。
你能否就這個問題給點幫助?
非常感謝。
請參閱下面的代碼:
library(shiny)
library(shinydashboard)
workDir<-c("c:/files")
setwd(workDir)
ui <- dashboardPage(
dashboardHeader(title = "Test Tool"),
dashboardSidebar(
sidebarMenu(
menuItem("Setup", tabName = "setup", icon = icon("cogs"))
)
),
dashboardBody(
tabItem(tabName = "setup",
fluidRow(
box(width = 12,
h1("Setup before generate dashboard"),
p(class = "text-muted",
br(),
"Default all files under working directory been selected, you also can choice your desired files.",
br(),
br()
),
fluidRow(
column(12, align="center",
actionButton("rescanFilesBtn","Rescan CSV Files")
)
)
)
),
fluidRow(
column(width = 12,
box(width = NULL, style = "max-height: 500px", status = "info", solidHeader = TRUE,
title = "Change raw CSV files as you like",
textInput("csvFileFilter","Desired CSV Files", placeholder = "File names, separate by semicolon"),
checkboxGroupInput("selectedFiles", "",
choiceValues = NULL,
choiceNames= NULL,
selected = NULL
)
)
)
)
)
)
)
server <- function(input, output, session) {
fullFilenames <- reactive({list.files(workDir, pattern="*.csv.gz", full.names=TRUE)})
observeEvent(input$rescanFilesBtn, {
print("Scan Files")
fullFilenames <- list.files(workDir, pattern="*.csv.gz", full.names=TRUE)
updateCheckboxGroupInput(session, "selectedFiles", choices = fullFilenames)
print("File Scaned")
})
}
shinyApp(ui, server)
你能提供一個可重複的例子嗎? – SBista
@SBista,已更新。非常感謝你。 –