我希望用户选择要显示的绘图类型。
如果绘图类型为"条形图",则应显示新的选择变量。在这种情况下;装箱";以及";断裂";。
但是,下面的代码工作不正常。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)