根据R中的selectinput值更改图表

Posted

技术标签:

【中文标题】根据R中的selectinput值更改图表【英文标题】:Change chart based on selectinput value in R 【发布时间】:2019-10-07 16:56:21 【问题描述】:

我已将窗口分成两部分。在这两个部分我都有一个下拉列表和一个旭日形图。现在我想用下拉菜单改变旭日形。我有 data_pie 和 data_pie2 之类的数据框,这应该从下拉值中更改。我知道我应该使用响应式函数,但我不知道如何更改数据帧。

服务器.R

  output$sunburst <- renderSunburst(sunburst(data_pie, count =  TRUE, legend = list(w = 150,h = 50, s = 15 , t = 1 ), breadcrumb = list(w = 150,h = 75, s = 15 , t = 10)))

  output$sunburst2 <- renderSunburst(sunburst(data_pie2, count =  TRUE, legend = list(w = 150,h = 50, s = 15 , t = 1 ), breadcrumb = list(w = 150,h = 75, s = 15 , t = 10)))

ui.R

tabItem(tabName = "sunbrust",
        h2("Sunbrust Visualization"),
        fluidRow(

          box(
            selectInput("input_pie_links", choices = c("Model 1", "Model 2", "Model 3"), label = "Model Auswahl"),
            sunburstOutput("sunburst", width = "1200", height = "1200")),

          box(
            selectInput("input_pie_rechts", choices = c("Model 1", "Model 2", "Model 3"), label = "Model Auswahl"),
            sunburstOutput("sunburst2", width = "1200", height = "1200"))

【问题讨论】:

【参考方案1】:

我不确定您要做什么。通过使用 slideInput 选择数据集只显示一个图表而不是两个?也许下面的代码会有所帮助。如果不是,请指定您要执行的操作,并提供更多代码以便您的仪表板可以被复制。

ui.R

  tabItem(tabName = "sunbrust",
               h2("Sunbrust Visualization"),
               fluidRow(

                 box(
                   selectInput("input_pie", choices = c("Model 1" = 1, "Model 2" = 2),
                               label = "Model Auswahl"),
                   sunburstOutput("sunburst", width = "1200", height = "1200"))
               )

服务器.R

    data = reactive(

    if (input$input_pie == 1) 
      data <- data_pie
     else 
        data <- data_pie2
       

    )

    output$sunburst <- renderSunburst(sunburst(data(), count =  TRUE, legend = list(w = 150,h = 50, s = 15 , t = 1 ), breadcrumb = list(w = 150,h = 75, s = 15 , t = 10)))

【讨论】:

【参考方案2】:

我已经创建了以下解决方案:

ui.R

tabItem(tabName = "sunbrust",
        h2("Sunbrust Visualization"),
        fluidRow(
          #column(10, align="center",
          #       sunburstOutput("sunburst", width = "1200", height = "1200")

      box(
        uiOutput("sunburst_auswahl_links"),
        sunburstOutput("sunburst", width = "1200", height = "1200")),

      box(
        uiOutput("sunburst_auswahl_rechts"),
        sunburstOutput("sunburst2", width = "1200", height = "1200"))

    )

服务器.R

  output$sunburst_auswahl_links <- renderUI(
    selectInput("auswahl_sunburst_links", "Model Auswahl:", c(1,2,3)) 
  )

  sunbrylinks <- reactive(sunburst(datapielist[as.integer(input$auswahl_sunburst_links)][[1]], count =  TRUE, legend = list(w = 150,h = 50, s = 15 , t = 1 ), breadcrumb = list(w = 150,h = 75, s = 15 , t = 10)))

  output$sunburst <- renderSunburst(sunbrylinks())


  output$sunburst_auswahl_rechts <- renderUI(
    selectInput("auswahl_sunburst_rechts", "Model Auswahl:", c(1,2,3)) 
  )

  sunbryrechts <- reactive(sunburst(datapielist[as.integer(input$auswahl_sunburst_rechts)][[1]], count =  TRUE, legend = list(w = 150,h = 50, s = 15 , t = 1 ), breadcrumb = list(w = 150,h = 75, s = 15 , t = 10)))

  output$sunburst2 <- renderSunburst(sunbryrechts())

【讨论】:

以上是关于根据R中的selectinput值更改图表的主要内容,如果未能解决你的问题,请参考以下文章

未选择值时如何更改 R Shiny 'selectInput' 值选择显示空间背景颜色?

在 R 中更改链式 selectInputs 后如何保持值?

selectInput 选择依赖于 R Shiny Flexdashboard 的另一个 selectInput

R Shiny、Leaflet 的问题 --> SelectInput 以更改下拉菜单中的选择

根据 R Shiny 中的其他选择动态更新两个 selectInput 框

如何在 selectInput R Shiny 中为一长串选项设置值