Help with Shiny App Publishing

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)

Hi, are you able to provide a reproducible example of LowBirthbyRace? Without a sample, the app won't run.

At what point do I have to do it?

Are you able to provide the output of dput(head(LowBirthbyRace))?

It is so others can run your app and test it for the issues you have described.

Highly appreciate your help!

structure(list(State = c("Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado"), All = c(10, 5.8, 6.9, 8.8, 6.8, 8.8
), White = c(8.1, 5.5, 6.4, 7.7, 6, 8.3), Hispanic = c(6.5, 6.6,
6.7, 5.9, 6.4, 8.7), Black = c(14.6, 6.5, 11.2, 14, 11.4, 14.6
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

It seems to be fine once you get rid of this line as col_grid isn't defined anywhere.

theme(panel.grid = element_line(color = col_grid))+

This is using your example data:

YES!! Thank you so much. You have been so helpful.
I went back and defined the color and it works!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.