I have been troubleshooting with this for a long time now and can't figure out why my UI isn't changing based on the two variables selected in the second sidebar panel. When I select two variables in the second sidebar panel, I want a scatterplot to show up in the main panel. After running the document, I see that the sidebar panel is added, but when I select variables and click the 'Generate Scatterplot' button, nothing shows up in the main panel.
Here's my code
shinydata <- s3read_using(FUN = read.csv,
bucket = "ddsproject1",
object = "CaseStudy2-data.csv")
ui <- fluidPage(
titlePanel("Employee Attrition"),
# summary plots of employee attrition
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30),
radioButtons(inputId = "plotType",
label = "Choose variable:",
choices = c("Job Level", "Age", "Environment Satisfaction", "Monthly Income", "Distance from Home"),
selected = "Job Level"),
),
mainPanel(
conditionalPanel(
condition = "input.plotType == 'Job Level'",
plotOutput(outputId = "JobLevel")
),
conditionalPanel(
condition = "input.plotType == 'Age'",
plotOutput(outputId = "Age")
),
conditionalPanel(
condition = "input.plotType == 'Environment Satisfaction'",
plotOutput(outputId = "EnvironmentSatisfaction")
),
conditionalPanel(
condition = "input.plotType == 'Monthly Income'",
plotOutput(outputId = "MonthlyIncome")
),
conditionalPanel(
condition= "input.plotType == 'Distance from Home'",
plotOutput(outputId = "DistanceFromHome")
),
)
),
# scatterplots of employee attrition
sidebarLayout(
sidebarPanel(
selectInput(inputId = "variable1",
label = "Select Variable 1:",
choices = c("Job Level", "Age", "Environment Satisfaction", "Monthly Income", "Distance from Home")),
selectInput(inputId = "variable2",
label = "Select Variable 2:",
choices = c("Job Level", "Age", "Environment Satisfaction", "Monthly Income", "Distance from Home")),
actionButton(inputId = "scatterPlotButton", label = "Generate Scatterplot")
),
mainPanel(
plotOutput(outputId = "scatterplot")
#uiOutput(outputId="scatterplot")
)
)
)
# server -----------
server <- function(input, output) {
output$JobLevel <- renderPlot({
x <- shinydata$JobLevel
if (length(x) == 0 || all(is.na(x))) {
return(NULL)
}
bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Job Level",
main = "Histogram of Job Level of Employee Attrition")
})
output$Age <- renderPlot({
x <- shinydata$Age
if (length(x) == 0 || all(is.na(x))) {
return(NULL)
}
bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Age",
main = "Histogram of Age of Employee Attrition")
})
output$EnvironmentSatisfaction <- renderPlot({
x <- shinydata$EnvironmentSatisfaction
if (length(x) == 0 || all(is.na(x))) {
return(NULL)
}
bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Environment Satisfaction",
main = "Histogram of Environment Satisfaction of Employee Attrition")
})
output$MonthlyIncome <- renderPlot({
x <- shinydata$MonthlyIncome
if (length(x) == 0 || all(is.na(x))) {
return(NULL)
}
bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Monthly Income",
main = "Histogram of Monthly Income of Employee Attrition")
})
output$DistanceFromHome <- renderPlot({
x <- shinydata$DistanceFromHome
if (length(x) == 0 || all(is.na(x))) {
return(NULL)
}
bins <- seq(min(x, na.rm = TRUE), max(x, na.rm = TRUE), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Distance from Home",
main = "Histogram of Employees' Distance From Home of Employee Attrition")
})
# for second panel
observeEvent(input$scatterPlotButton, {
variable1 <- input$variable1
variable2 <- input$variable2
#print(paste("Variable 1:", variable1))
#print(paste("Variable 2:", variable2))
#if (!is.null(variable1) && !is.null(variable2)) {
x <- shinydata[[variable1]]
y <- shinydata[[variable2]]
if (!all(is.na(x)) && !all(is.na(y))) {
scatterplot <- ggplot(data = shinydata, aes(x = x, y = y)) +
geom_jitter() +
labs(x = variable1, y = variable2, title = "Scatterplot of Two Variables")
print(scatterplot)
output$scatterplot <- renderPlot({ scatterplot })
#}
}
})
}
shinyApp(ui, server)
Some of the debugging I've tried: checking syntax, printing out and making sure the selected variables are being read correctly, reloading libraries (specifically, ggplot2).