Reputation: 897
I am new to Rshiny
and practising how to use reactive values
, reactive expressions
and selectizeInput
. I would like to have all brands printed at once after pressing the button without the sentence "The brands selected are as follows: " to be printed multiple times:
here is my code:
ui <- fluidPage(
titlePanel("This is a Test"),
sidebarLayout(
sidebarPanel(
selectizeInput('brand', label = 'Car brand',
multiple = T, choices = mtcars %>% rownames(),
selected = NULL, width = '100%',
options = list('plugins' = list('remove_button')))
),
mainPanel(
actionButton("show_brands", "Show brands"),
textOutput("brands")
)
)
)
server <- function(input, output, session) {
values <- reactiveValues(
brandname = NULL,
mpgdata = NULL
)
output$brands <- renderText({
allbrands <- values$brandname()
paste("The brands seleted are as follows: ", allbrands)
})
values$brandname <- eventReactive(input$show_brands, {
input$brand
})
}
shinyApp(ui, server)
and here is the output after selecting three of the brands:
Upvotes: 0
Views: 26
Reputation: 33442
We can wrap input$brand
in another paste()
call:
library(shiny)
ui <- fluidPage(titlePanel("This is a Test"),
sidebarLayout(
sidebarPanel(
selectizeInput(
'brand',
label = 'Car brand',
multiple = TRUE,
choices = rownames(mtcars),
selected = NULL,
width = '100%',
options = list('plugins' = list('remove_button'))
)
),
mainPanel(
actionButton("show_brands", "Show brands"),
textOutput("brands")
)
))
server <- function(input, output, session) {
output$brands <- renderText({
paste("The brands seleted are as follows: ", paste(input$brand, collapse = ", "))
}) |> bindEvent(input$show_brands)
}
shinyApp(ui, server)
PS: There is no need to use reactiveValues
Upvotes: 1