Skip to content

Commit

Permalink
Use observeEvent() to only update country on click
Browse files Browse the repository at this point in the history
  • Loading branch information
mawds committed Aug 9, 2018
1 parent ee34c67 commit d3fc11f
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion worked_example/app.R
Expand Up @@ -50,6 +50,17 @@ ui <- fluidPage(
# Define server logic required to draw a histogram
server <- function(input, output) {

# Create a reactive value to store the country we seleect
activeCountry <- reactiveVal()

# Update the value of activeCountry() when we detect an input$plotClick event
# (Note how we update a reactiveVal() )
observeEvent(input$plotClick,
{
nearCountry <- nearPoints(plotData(), input$plotClick, maxpoints = 1)
activeCountry(as.character(nearCountry$country)) # Extract just the country name and assign it to activeCountry()
})

plotData <- reactive({
gapminder %>%
filter(year == input$year) %>%
Expand All @@ -67,7 +78,7 @@ server <- function(input, output) {
})

output$clickData <- renderPrint(({
nearPoints(plotData(), input$plotClick, maxpoints = 1)
activeCountry()
}))
}

Expand Down

0 comments on commit d3fc11f

Please sign in to comment.