Hi All,
I am new to R and Shiny App and have an assignment due which gives me headaches
When I publish my shiny app, I receive an error message and the plot does not show.
Any help is much appreciated
Error: An error has occurred. Check your logs or contact the app author for clarification.
my code is as following:
library(shiny)
library(tidyverse)
library(ggplot2)
#Read Data
LowBirthbyRace<- read_csv("LowBirthbyRace.csv")
names(LowBirthbyRace)[c(1:5)] <- c("State",
"All Females",
"White",
"Hispanic",
"Black")
df <- LowBirthbyRace %>%
pivot_longer (
!c(State),
names_to='Race',
values_to='Rate',
values_drop_na= TRUE)
state <- pull(LowBirthbyRace,State) #Choose variable user can change
# Define UI for application that draws barplot
ui <- fluidPage(
# Application title
titlePanel("How Does Low Birth Weight Vary Across Races?"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
selectInput("state", " Choose a State:", #'state' smuggles selected state to the server side
choices = state),
h6("Low Birth Weight Babies are less than 5 lbs., 8 oz.")
),
mainPanel( # Show a plot of the generated distribution
textOutput("text"), #code to add title to plot as state gets picked
plotOutput("plot")
)
)
)
###Server Side
server <- function(input, output) {
output$plot <- renderPlot({
df %>%
filter(State == input$state) %>%
mutate(Race=factor(Race, levels=c("Hispanic", "Black", "White", "All Females")))%>%
ggplot(df,mapping=aes(Rate,Race,fill=Race, alpha=0.9))+
expand_limits(x=c(0,17))+
geom_col()+
scale_fill_manual(values=c("red", "blue", "green","purple"))+
geom_text(aes(label = Rate), hjust = -0.3, size = 4, color = "black", fontface="bold")+
labs(caption="\nData source: Institute for Women's Policy Research, 2013 \nNote: Racial categories are non-Hispanic. Hispanics may be of any race or two or more races. \nData are not available for Asian/Pacific Islanders, Native Americans, or those who identify with another race/ethnicity or with two or more races.")+
theme_minimal()+
theme(panel.grid = element_line(color = col_grid))+
theme( legend.position="none",
axis.text.y=element_text(size=12),
axis.title.y=element_blank(),
axis.text.x = element_blank(),
axis.title.x=element_blank())
})
output$text <- renderText({ #code to add title to plot as state gets picked
paste0("Low Birth Rates in % for ", input$state) #code to add title to plot as state gets picked
}) #code to add title to plot as state gets picked
}
# Run the application
shinyApp(ui = ui, server = server)