首页 > 解决方案 > R闪亮单选按钮中的开关输出

问题描述

我正在开发一个闪亮的应用程序,我正在使用radioGroupButtonsShinyWidgets。因此,对于每个按钮,我都试图切换到不同的输出,如表格或绘图。如何将单选按钮链接到输出

library(shinyWidgets)
library(shinipsum)
library(htmlwidgets)

  ui <- navbarPage(
    div(
      id = "section1-1",
      radioGroupButtons(
        inputId = "Id069",
        # label = "Choose a graph :",
        choices = c(
          `<i class='fa fa-bar-chart'></i>` = "bar",
          `<i class='fa fa-line-chart'></i>` = "line",
          `<i class='fa fa-pie-chart'></i>` = "pie"
        ),
        justified = TRUE
      )
    )
  )

  server <- function(input, output, session) {
    # observe({
    #   x <- input$inRadioButtons
    #   
    #   # Can also set the label and select items
    #   updateRadioButtons(session, "inRadioButtons2",
    #                      label = paste("radioButtons label", x),
    #                      choices = x,
    #                      selected = x
    #   )
    # })
    output$plot <- renderPlot({
      random_ggplot()
    })
  }

  shinyApp(ui, server)

标签: rshiny

解决方案


在服务器端,您可以像这样访问输入 ID:

         Value = input$Id069

因此,将这样的逻辑添加到服务器端(在 output$plot 中):

        If (Value == x) {
          Plot1()
        } else {
          Plot2()
        }

您可能想查看 UI 上的条件面板。


推荐阅读