I encountered an unexpected behavior. What I intend to do is: - when users click "Select All," all rows in "Summary Table" get selected. This WORKS. However, the code below doesn't get called.
data <- eventReactive(input$selectAll,{
print("Select All - restore data")
rawdata
})
- on the other hand, when users click "Deselect All," all rows in "Summary Table" get deselected. This WORKS and the code below GETS called.
# Restore data when users click 'Deselect All'
data <- eventReactive(input$deselectAll,{
print("Deselect All - restore data")
rawdata
})
Any idea why?
Here is my full code:
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"))
)
)
)
UPDATED Server.R #2: @Mike and @HubertL, I think you are right: the issue is caused by eventReactive having cached values. In this updated version, observeEvent corresponding to Select All and Deselect All work as expected. However, now eventReactive corresponding to input$inputVars_rows_selected NEVER gets called. Any idea why?
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)
})
}