仅当绘图类型是条形图时才显示这些面板。 R 闪亮

Posted

技术标签:

【中文标题】仅当绘图类型是条形图时才显示这些面板。 R 闪亮【英文标题】:Only show these panels if the plot type is a barplot. R shiny 【发布时间】:2022-01-19 20:20:19 【问题描述】:

我希望用户选择要显示的绘图类型。

如果绘图类型是“条形图”,则应出现新的选择变量。在这种情况下,“binning”和“breaks”。

但是,下面的代码不能正常工作。在 conditionalPanel 之后不显示新变量。

这里有 RepEx。

# Shiny
library(shiny)
library(shinyWidgets)
library(shinyjqui)

# Data
library(readxl)
library(dplyr)
library(arules) # Discretization

# Plots
library(ggplot2)

not_sel <- "Not Selected"

# main page display in the shiny app where user will input variables and plots will be displayed
main_page <- tabPanel(
  title = "Plotter",
  titlePanel("Plotter"),
  sidebarLayout(
    sidebarPanel(
      title = "Inputs",
      fileInput("xlsx_input", "Select XLSX file to import", accept = c(".xlsx")),
      selectInput("num_var_1", "Variable X axis", choices = c(not_sel)),
      selectInput("num_var_2", "Variable Y axis", choices = c(not_sel)), uiOutput("binning"),
      selectInput("graph", "Choose a graph to view:", 
                  choices = c("Boxplot", "Barplot")), #Choose type of plot to be displayed
      
      # Only show these panels if the plot type is a barplot
      conditionalPanel(condition = "graph == 'Barplot'",
                       checkboxGroupButtons(
                         inputId = "bin_sce",
                         label = "Binning Scenario:",
                         choices = c("Frequency", "Interval"),
                         direction = "vertical"),
      ),
      conditionalPanel(condition = "graph == 'Barplot'",
                       radioGroupButtons(
                         inputId = "breaks",
                         label = "Breaks",
                         choices = c("2", "3", "4", "5"),
                         checkIcon = list(
                           yes = icon("ok",
                                      lib = "glyphicon"))
                       )
      ),
      actionButton("run_button", "Run Analysis", icon = icon("play"))
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "Plot",
          br(),
          plotOutput("")
        )
      )
    )
  )
)


# User interface

ui <- navbarPage(
  main_page
)

# Server

server <- function(input, output)
  
  # Dynamic selection of the data. We allow the user to input the data that they want 
  data_input <- reactive(
    #req(input$xlsx_input)
    #inFile <- input$xlsx_input
    #read_excel(inFile$datapath, 1)
    iris
  )
  
  # We update the choices available for each of the variables
  observeEvent(data_input(),
    choices <- c(not_sel, names(data_input()))
    updateSelectInput(inputId = "num_var_1", choices = choices)
    updateSelectInput(inputId = "num_var_2", choices = choices)
    updateSelectInput(inputId = "biomarker", choices = choices)
  )
  
  num_var_1 <- eventReactive(input$run_button, input$num_var_1)
  num_var_2 <- eventReactive(input$run_button, input$num_var_2)
  biomarker <- eventReactive(input$run_button, input$biomarker)
  
   

# Connection for the shinyApp
shinyApp(ui = ui, server = server)

如您所见,没有显示任何新内容。

【问题讨论】:

【参考方案1】:

您必须将"input.graph == 'Barplot'" 添加到条件中。这些条件不是R代码而是JS作为字符串传递,然后由函数自己转换。

      # Only show these panels if the plot type is a barplot
      conditionalPanel(condition = "input.graph == 'Barplot'",
                       checkboxGroupButtons(
                         inputId = "bin_sce",
                         label = "Binning Scenario:",
                         choices = c("Frequency", "Interval"),
                         direction = "vertical"),
      ),
      conditionalPanel(condition = "input.graph == 'Barplot'",
                       radioGroupButtons(
                         inputId = "breaks",
                         label = "Breaks",
                         choices = c("2", "3", "4", "5"),
                         checkIcon = list(
                           yes = icon("ok",
                                      lib = "glyphicon"))
                       )

另一种方法是使用shinyjs:: 中的show()hide(),在这种情况下,您可以在R 代码中使用条件。

在 ui 和服务器内部调用 useShinyjs() 将观察者与 show()hide() 配对。

应用:

# Shiny
library(shiny)
library(shinyWidgets)
library(shinyjqui)
library(shinyjs)

# Data
library(readxl)
library(dplyr)
library(arules) # Discretization

# Plots
library(ggplot2)

not_sel <- "Not Selected"

# main page display in the shiny app where user will input variables and plots will be displayed
main_page <- tabPanel(
  useShinyjs(),
  title = "Plotter",
  titlePanel("Plotter"),
  sidebarLayout(
    sidebarPanel(
      title = "Inputs",
      fileInput("xlsx_input", "Select XLSX file to import", accept = c(".xlsx")),
      selectInput("num_var_1", "Variable X axis", choices = c(not_sel)),
      selectInput("num_var_2", "Variable Y axis", choices = c(not_sel)), uiOutput("binning"),
      selectInput("graph", "Choose a graph to view:", 
                  choices = c("Boxplot", "Barplot")), #Choose type of plot to be displayed
      
      # Only show these panels if the plot type is a barplot
      
                       shinyjs::hidden(checkboxGroupButtons(
                         inputId = "bin_sce",
                         label = "Binning Scenario:",
                         choices = c("Frequency", "Interval"),
                         direction = "vertical")),
                       shinyjs::hidden(radioGroupButtons(
                         inputId = "breaks",
                         label = "Breaks",
                         choices = c("2", "3", "4", "5"),
                         checkIcon = list(
                           yes = icon("ok",
                                      lib = "glyphicon")))),
      actionButton("run_button", "Run Analysis", icon = icon("play"))
    ),
    mainPanel(
      tabsetPanel(
        tabPanel(
          title = "Plot",
          br(),
          plotOutput("")
        )
      )
    )
  )
)


# User interface

ui <- navbarPage(
  main_page
)

# Server

server <- function(input, output)
  
  #hide or shor barplot options
  observe(
    if(input$graph == 'Barplot') 
      shinyjs::show('bin_sce')
      shinyjs::show('breaks')
     else 
      shinyjs::hide('bin_sce')
      shinyjs::hide('breaks')
    
  )
  
  # Dynamic selection of the data. We allow the user to input the data that they want 
  data_input <- reactive(
    #req(input$xlsx_input)
    #inFile <- input$xlsx_input
    #read_excel(inFile$datapath, 1)
    iris
  )
  
  # We update the choices available for each of the variables
  observeEvent(data_input(),
    choices <- c(not_sel, names(data_input()))
    updateSelectInput(inputId = "num_var_1", choices = choices)
    updateSelectInput(inputId = "num_var_2", choices = choices)
    updateSelectInput(inputId = "biomarker", choices = choices)
  )
  
  num_var_1 <- eventReactive(input$run_button, input$num_var_1)
  num_var_2 <- eventReactive(input$run_button, input$num_var_2)
  biomarker <- eventReactive(input$run_button, input$biomarker)
  
  

# Connection for the shinyApp
shinyApp(ui = ui, server = server)

【讨论】:

以上是关于仅当绘图类型是条形图时才显示这些面板。 R 闪亮的主要内容,如果未能解决你的问题,请参考以下文章

如何在闪亮中为侧边栏面板添加滚动条?

R闪亮过滤反应输入

在闪亮仪表板的选项卡面板中调整ggplot条形图的高度

如何使用 Shinyauthr 库在闪亮中创建登录而不在 R 中显示主面板?

Tableau数据可视化

R Shiny 不会在闪亮的应用程序中显示输出,而只会在控制台中显示输出(以及如何正确计算子组的平均值)?