I hope you are doing well! I am working on my first Rshiny app, and I am having trouble properly referencing "choices" in the selectInput portion of my code to create my plots.
I have a cardiovascular disease dataset, and the columns in the dataset are my "choices" in the selectInput function. I later want to create plots based on if the input is certain variables vs others (some are continuous and some are categorical, thus demanding different plots).
My code is as follows :
Note: that the input$covariate portion of my code when I create the plots is the part that is not working. If I directly specify a variable name, the plot works.
Thank you for any help you can offer! I really appreciate it!
library('shiny')
library('tidyverse')
library('plotly')
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Cardiovascular Risk Factors Data"),
# Sidebar with a slider input for number of bins
selectInput(inputId="covariate", label="Choose Covariate", choices = c(
"Age" = "Age",
"Sex" = "Sex",
"Chest_Pain" = "Chest_Pain",
"Resting_Blood_Pressure" = "Resting_Blood_Pressure",
"Colestrol" = "Colestrol",
"Fasting_Blood_Sugar" = "Fasting_Blood_Sugar",
"Rest_ECG" = "Rest_ECG",
"MAX_Heart_Rate" = "MAX_Heart_Rate",
"Exercised_Induced_Angina" = "Exercised_Induced_Angina",
"ST_Depression" = "ST_Depression",
"Slope" = "Slope",
"Major_Vessels" = "Major_Vessels",
"Thalessemia" = "Thalessemia"),
selected = "Age", multiple = F),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
if (input$covariate == "Resting_Blood_Pressure" | input$covariate == "Colestrol" |
input$covariate == "Age" | input$covariate == "MAX_Heart_Rate" |
input$covariate == "ST_Depression") {
plt <- ggplot(heart_data, aes(y=input$covariate)) +
geom_boxplot() +
facet_wrap(~Target)
}
else {
heart_data2 <- heart_data %>% mutate(Target=factor(Target))
plt <- ggplot(heart_data2 , aes(x=input$covariate, fill=Target)) +
geom_bar()
}
plt
})
}
# Run the application
shinyApp(ui = ui, server = server, options=list(port=8080, host="0.0.0.0"))