简体   繁体   中英

In R/Shiny how to pre select the point click event in treemap

I have a treemap in Shiny that have click event point. So once I click the name of the country, the name appears bellow.

As my code bellow:

library(shiny)
library(highcharter)
library(tidyverse)
library(gapminder)

ui <- fluidPage(

  highchartOutput(outputId = 'hcontainer'),

htmlOutput('countries')

)

server <- function(input, output, session) {

  click_js <-
    JS("function(event) {Shiny.onInputChange('treemapclick', event.point.name);}")


  output$hcontainer <- renderHighchart({
    gapminder::gapminder %>%
      dplyr::filter(year  == 2007) %>%

      highcharter::data_to_hierarchical(group_vars = c(continent, country),

                                        size_var = pop) %>%
      hchart(type = "treemap") %>%
      hc_plotOptions(treemap = list(events = list(click = click_js)))

  })

  output$countries <- renderUI({
    h1(input$treemapclick)
  })

}

shinyApp(ui, server)

My question is how do I start my Shiny app with a country already chosen? For example 'Chile'?

Becaus the input$treemapclick will only have a value when I click over the country, right?

ny help?

you could define a reactive value, eg reactive_country , set a default and use it in your code like so:

  selected_country = reactive(ifelse(is.null(input$treemapclick), 
              "default country", 
              input$treemapclick)
              )

  output$countries <- renderUI({
    ## note the parentheses to retrieve the reactive value!
    h1(selected_country()) 
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM