I want the user to select the type of plot to display.
In case the type of plot is 'barplot' new selection variables should appear. In this case "binning" and "breaks".
However, the code below is not working properly. No new variables are display after conditionalPanel.
Here you have the 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)
As you can see, nothing new is displayed.
CodePudding user response:
You have to add "input.graph == 'Barplot'"
to the conditions. These conditions are not R code but JS passed as a string and then converted by the function itself.
# 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"))
)
An alternative is to use show()
and hide()
from shinyjs::
, in that case you can use conditions in R code.
Call useShinyjs() inside the ui and inside the server pair an observer with show()
and hide()
.
App:
# 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)