如果绘图类型是条形图,则只显示这些面板。 R闪亮

Only show these panels if the plot type is a barplot. R shiny

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

如果绘图类型为 'barplot',则应出现新的 selection 变量。在这种情况下,“合并”和“中断”。

但是,下面的代码不能正常工作。 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)

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

您必须在条件中添加 "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)