Beans On Toast
Beans On Toast

Reputation: 1091

Capturing select input in R Shiny?

I am trying to capture the url that has been selected when a person presses the "GET URLS" button on the app.

What should happen is that the event reactive() should look at the input$go_button and see that it has been pressed - it should then perform the expression to take the chosen url from the select input- unfortunately it does nothing.

I have tried debugging with browser() but still had no affect.

All i am trying to do is capture the url that has been selected when a person "presses" the "GET URLS" button.

my sample code is below:

library(shiny)

# Use a fluid Bootstrap layout
ui <- fluidPage(    

  # Give the page a title
  titlePanel("testing select"),

  # Generate a row with a sidebar
  sidebarLayout(      

    # Define the sidebar with one input
    sidebarPanel(
      selectInput("url_selection", "select url:", 
                  choices = c(
                    '/multi-task/',
                    "/personal-account",
                    "/paperless"
                  )

                    ), 
      actionButton(inputId = "go_button", label = "Get URLS")
    ),

    # Create a spot for the barplot
    mainPanel(
      textOutput(outputId = "urls_selected_print")
    )

  )
)


server <- function(input, output) {

  url_capture <- reactive({eventReactive(eventExpr = input$go_button,
                               valueExpr = {
                                 message("capturing url chosen in selectize input")

                                 chosen_url <- input$url_selection
                                 browser()
                                 return(chosen_url)


                               })
  })

}




shinyApp(ui, server)

Upvotes: 3

Views: 1712

Answers (2)

bretauv
bretauv

Reputation: 8587

Small alternative to Ben's answer (you just have to remove reactive, because eventReactive is already reactive):


server <- function(input, output) {

  url_capture <- eventReactive(eventExpr = input$go_button,
                                         valueExpr = {
                                           message("capturing url chosen in selectize input")

                                           chosen_url <- input$url_selection
                                           return(chosen_url)


                                         })

  output$urls_selected_print <- renderPrint({
    url_capture()
  })

}

Upvotes: 1

Ben
Ben

Reputation: 30549

You can use observeEvent to capture event when go_button has been pressed. You can store the selection in a reactiveVal which can be displayed in your output.

server <- function(input, output) {

  rv <- reactiveVal(NULL)

  observeEvent(input$go_button, {
               message("capturing url chosen in selectize input")
               rv(input$url_selection)
  })

  output$urls_selected_print <- renderText({rv()})

}

Upvotes: 3

Related Questions