我遇到了意外的行爲。我打算做的是: - 當用戶單擊「全選」時,「彙總表」中的所有行都被選中。這工作。但是,下面的代碼不會被調用。意外的行爲選擇全部並取消選擇所有操作按鈕(R閃亮)
data <- eventReactive(input$selectAll,{
print("Select All - restore data")
rawdata
})
- 在另一方面,當用戶單擊「取消全」,在「彙總表」中的所有行均取消選中。此工程和GETS下面的代碼調用。
# Restore data when users click 'Deselect All'
data <- eventReactive(input$deselectAll,{
print("Deselect All - restore data")
rawdata
})
任何想法爲什麼?
這裏是我的全碼:
DATASET
colA <- c('A','B','C','D','E')
colB <- c(1,2,3,4,5)
rawdata <- as.data.frame(cbind(colA,colB))
View(rawdata)
server.R
function(input, output, session) {
# Activate tab 'Result' when users click 'Run'
observeEvent(input$runButton, {
updateTabsetPanel(session, "allResults", 'result')
})
# Create a dataset based on users' selected variables
data <- eventReactive(input$inputVars_rows_selected,{
print("Select Some Vars")
rawdata[, c(input$inputVars_rows_selected)]
})
# Restore data when users click 'Select All'
data <- eventReactive(input$selectAll,{
print("Select All - restore data")
rawdata
})
# Restore data when users click 'Deselect All'
data <- eventReactive(input$deselectAll,{
print("Deselect All - restore data")
rawdata
})
### VARIABLE SELECTION ####
var <- reactiveValues()
# Select all vars
observeEvent(input$selectAll,{
print("SelectAll ObserveEvent")
var$selected <- 1:nrow(rawdata)
print(var$selected)
})
# Deselect all vars
observeEvent(input$deselectAll,{
print("deselectAll ObserveEvent")
var$selected <- 0
print(var$selected)
print(data())
})
### RESULT TAB ###
result <- eventReactive (input$runButton, {
head(data(),2)
})
### RENDERING FUNCTIONS ###
# Default SummaryTable
output$inputVars <- DT::renderDataTable({
if (input$selectAll==0 & input$deselectAll==0) {
print("Default Summary Table")
DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE))
}
else {
DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE), selection = list(target = 'row', selected = var$selected))
}
})
# Display results
output$result <- DT::renderDataTable({
DT::datatable(result(), options = list(paging = FALSE, searching = FALSE))
})
output$temp <- renderPrint({
print(input$selectAll)
print(input$deselectAll)
})
}
ui.R
fluidPage(
sidebarPanel(
actionButton("runButton", strong("Run!"))
),
mainPanel(
tabsetPanel(id = "allResults",
tabPanel(value='inputVars',title='Variable Selection',
verticalLayout(
DT::dataTableOutput('inputVars'),
br(),
fluidRow(align="bottom",
column(2, actionButton("selectAll" , strong("Select All"))),
column(3, actionButton("deselectAll", strong("Deselect All")))
)
)
),
tabPanel(value='result',title='Result', DT::dataTableOutput('result')),
tabPanel(value='temp',title="TEMP", verbatimTextOutput("temp"))
)
)
)
修訂Server.R#2: @Mike和@H ubertL,我認爲你是對的:問題是由eventReactive擁有緩存值引起的。在此更新版本中,與全選和取消全選相對應的observeEvent按預期工作。但是,現在eventVERO對應於輸入$ inputVars_rows_selected NEVER被調用。任何想法爲什麼?
function(input, output, session) {
# Activate tab 'Result' when users click 'Run'
observeEvent(input$runButton, {
updateTabsetPanel(session, "allResults", 'result')
})
data <- reactiveValues()
# Create a dataset based on users' selected variables
data <- eventReactive(input$inputVars_rows_selected,{
print("Select Some Vars")
print(input$inputVars_rows_selected)
rawdata[, c(input$inputVars_rows_selected)]
})
### VARIABLE SELECTION ####
var <- reactiveValues()
# Select all vars
observeEvent(input$selectAll,{
print("SelectAll ObserveEvent")
data <- rawdata
var$selected <- 1:nrow(rawdata)
print(var$selected)
print(data)
})
# Deselect all vars
observeEvent(input$deselectAll,{
print("deselectAll ObserveEvent")
data <- rawdata
var$selected <- 0
print(var$selected)
print(data)
})
### RESULT TAB ###
result <- eventReactive (input$runButton, {
head(data(),2)
})
### RENDERING FUNCTIONS ###
# Default SummaryTable
output$inputVars <- DT::renderDataTable({
if (input$selectAll==0 & input$deselectAll==0) {
print("Default Summary Table")
DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE))
}
else {
DT::datatable(rawdata, options = list(paging = FALSE, searching = FALSE), selection = list(target = 'row', selected = var$selected))
}
})
# Display results
output$result <- DT::renderDataTable({
DT::datatable(result(), options = list(paging = FALSE, searching = FALSE))
})
output$temp <- renderPrint({
print(input$selectAll)
print(input$deselectAll)
print(input$inputVars_rows_selected)
})
}
名稱有一個對象:不能將多個反應函數分配給同一個變量名,這裏只考慮最後一個分配。考慮做一個單一的eventReactive函數,並使用if語句根據輸入返回不同的選擇 – HubertL
奇怪的是,這似乎不是真的...... –
實際上可能是真的,但很難判斷哪個data()被拉來自「eventReactive」緩存。 –