highcharts 在R/Shiny中如何使用开关量输入来显示图表

mzsu5hc0  于 2022-11-10  发布在  Highcharts
关注(0)|答案(1)|浏览(185)

Bellow是我的闪亮应用程序。这里的想法是当我点击切换按钮时,图表出现(在这个例子中是France图表)。
我认为这里的想法是让开关输入成为“主要”输入,一旦用户点击它,使滑块输入没有效果。滑块输入将只会回来“工作”,如果开关输入关闭。
惨叫是我的代号:

library("shiny")
library("shinyWidgets")
library("gapminder")
library(tidyverse)
library(highcharter)
hc_data_1 <- gapminder %>% filter(country == 'Chile') 

hc_data_2 <- gapminder %>% filter(country == 'Argentina')
hc_data_3 <- gapminder %>% filter(country == 'France')

country_list <- list(hc_data_1,hc_data_2,hc_data_3)

ui <- fluidPage(

  sliderTextInput(inputId =  "slider_new",
                  label = "Projections Range",
                  width = '100%',
                  choices = c('Chile','Chile-Argentina'), 
                  selected = 'Chile-Argentina'
  ),

  materialSwitch(
    inputId ='swithc_slider',
    label =tags$p("Switch"),
    value  =FALSE
  ),

  htmlOutput('chart_1')
)

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

  output$chart_1<- renderUI({
    if(input$slider_new == "Chile"){

      gapminder %>% filter(country == 'Chile') %>% hchart(
        "line",
        hcaes(x = year, y = pop),
        showInLegend = TRUE,
        color = "#63696b",
        name = "Title 1"
      )

    }else{

      if(input$slider_new == "Chile-Argentina"){

        hc_data_1 %>%

          hchart(
            "line",
            hcaes(x = year, y = pop),
            showInLegend = TRUE,
            color = "#63696b",
            name = "Title 1"
          ) %>%

          hc_add_series(hc_data_2,
                        "spline",
                        color = "gray",

                        hcaes(x = year, y = pop),
                        showInLegend = TRUE)
      }else{

        gapminder %>% filter(country == 'France') %>% hchart(
          "line",
          hcaes(x = year, y = pop),
          showInLegend = TRUE,
          color = "#63696b",
          name = "Title 1")

      }
    }
  })
}

shinyApp(ui = ui, server = server)

所以我们的想法是:一旦我点击开关输入,法国图表出现。和独立的滑块输入位置。然后使滑块'工作'再次我关闭,然后'智利'或'智利阿根廷'图表出现。

azpvetkf

azpvetkf1#

试试这个

library("shiny")
library("shinyWidgets")
library("gapminder")
library(tidyverse)
library(highcharter)
hc_data_1 <- gapminder %>% filter(country == 'Chile') 

hc_data_2 <- gapminder %>% filter(country == 'Argentina')
hc_data_3 <- gapminder %>% filter(country == 'France')

country_list <- list(hc_data_1,hc_data_2,hc_data_3)

ui <- fluidPage(

  sliderTextInput(inputId =  "slider_new",
                  label = "Projections Range",
                  width = '100%',
                  choices = c('Chile','Chile-Argentina'), 
                  selected = 'Chile-Argentina'
  ),

  materialSwitch(
    inputId ='switch_slider',
    label =tags$p("Switch"),
    value  =FALSE
  ),

  htmlOutput('chart_1')
)

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

  CA <- reactive({
    if(input$slider_new == "Chile"){
      gapminder %>% filter(country == 'Chile') %>% hchart(
        "line",
        hcaes(x = year, y = pop),
        showInLegend = TRUE,
        color = "#63696b",
        name = "Chile"
      )
    }else{
      hc_data_1 %>%

        hchart(
          "line",
          hcaes(x = year, y = pop),
          showInLegend = TRUE,
          color = "#63696b",
          name = "Chile"
        ) %>%

        hc_add_series(hc_data_2,
                      "spline",
                      color = "blue",
                      name = "Argentina",
                      hcaes(x = year, y = pop),
                      showInLegend = TRUE)
    }
  })

  Fr <- reactive({
    gapminder %>% filter(country == 'France') %>% hchart(
      "line",
      hcaes(x = year, y = pop),
      showInLegend = TRUE,
      color = "red", # "#63696b",
      name = "France")
  })

  output$chart_1<- renderUI({
    if (input$switch_slider){
      Fr()
    }else {
      CA()
    }
  })
}

shinyApp(ui = ui, server = server)

相关问题