1

I'd like to create a situation where actionButton and selectInput influence each other. For example, I'd like to create a situation when the value of the selectInput changes the value of the actionButton.


library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("select", "Select Input", choices = c(" ", "A", "B")),
      actionButton("action", "Action Button")
    ),
    mainPanel(
      textOutput("out1"),
      textOutput("out2")
    )
  )
)

server <- function(input, output, session) {
  
  rv <- reactiveValues(count = 0)
  output$out1 <- renderText({
    paste("Action Button: ", input$action)
  })
  
  observeEvent(input$select, {
    if (input$select == "A") rv$count <- rv$count + 1
  })
  
  output$out2 <- renderText({
    paste("Select Input: ", rv$count)
  })
}

options(shiny.autoreload = TRUE)
shinyApp(ui = ui, server = server)

In this example, (thanks to this answer), the values change independently. I've tried to create a common value, but it doesn't seem to update.

I attempted to try updating the value using this updateActionButton(session, "action", "Action Button")

But selectInput does not change the value of the actionButton. I'd like to find a way to make that happen.

2
  • So action button value should always copy select input value ? Commented Jun 27, 2021 at 3:32
  • Yes, that's what I'm looking for. Commented Jun 27, 2021 at 3:32

1 Answer 1

1

Why not just use rv$count in Action button text as well?

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("select", "Select Input", choices = c(" ", "A", "B")),
      actionButton("action", "Action Button")
    ),
    mainPanel(
      textOutput("out1"),
      textOutput("out2")
    )
  )
)

server <- function(input, output, session) {
  
  rv <- reactiveValues(count = 0)
  output$out1 <- renderText({
    paste("Action Button: ", rv$count)
  })
  
  observeEvent(input$select, {
    if (input$select == "A") rv$count <- rv$count + 1
  })
  
  output$out2 <- renderText({
    paste("Select Input: ", rv$count)
  })
}

options(shiny.autoreload = TRUE)
shinyApp(ui = ui, server = server)
Sign up to request clarification or add additional context in comments.

2 Comments

This works! I was wondering whether updateSelectInput would do it.
You mean updateActionButton ? I don't know if that can be used to change the count here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.