Reputation: 351
I am trying to get the values selected in the checkboxGroupInput to show in the main panel of multiple tabs after the "submit" button is clicked. Using observeEvent, the first time choices are selected, they don't show in the main panel until the submit button is clicked, but after that, the values show in the main panel when selected without submitting them. How can I make it so submit has to be clicked before the main panel updates?
library(shiny)
ui <- fluidPage(
titlePanel(
"Example"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("choice", label = "Choose Letter",
choices = list("A", "B", "C", "D")),
actionButton("submit", "Submit"),
width=3
),
mainPanel(
tabsetPanel(
navbarMenu("Pages",
tabPanel("Page 1", h3(htmlOutput("choice1"))),
tabPanel("Page 2", h3(textOutput("choice2")))
)
)
)
)
)
server <- function(input, output) {
observeEvent(input$submit,{
lapply(1:2, function(nr){
output[[paste0("choice",nr)]]<-renderText({choose<-paste(input$choice, collapse=", ")
paste(choose)})
})
})
}
shinyApp(ui = ui, server = server)
I also tried eventReactive, which I've never used before, so I'm not sure I understand it. Using the following code in the server, I get the error "Error in as.vector: cannot coerce type 'closure' to vector of type 'character' "
a<-eventReactive(input$submit,{
paste(input$choice, collapse=", ")
})
lapply(1:2, function(nr){
output[[paste0("choice",nr)]]<-renderText(paste(a))
})
Upvotes: 0
Views: 471
Reputation: 13125
I will post the solution but with minimal explanation with a hope that someone can come along and explain more or correct mine.
First: renderText
interact with "or depends on" input$choice
directly hence any update or change in input$choice
will reflect on renderText. To solve this dependency issue you need a placeholder that depends on OR triggered using observeEvent
which is reactiveValues
server <- function(input, output) {
data <- reactiveValues()
observeEvent(input$submit,{
data$nr <- input$choice
lapply(1:2, function(nr){
utput[[paste0("choice",nr)]]<-renderText({choose<-paste(data$nr, collapse=", ")
paste(choose)})
})
})
}
Second: Any object created with eventReactive
is the same as those created with reactive
, they were functions and in the time of invoking they should be called using ()
, e.g. in your case a
should be called as follows a()
a<-eventReactive(input$submit,{
paste(input$choice, collapse=", ")
})
lapply(1:2, function(nr){
output[[paste0("choice",nr)]]<-renderText(paste(a()))
})
For more information on reactivity and dependency in shiny, you can check here
Upvotes: 1