Hello,
I am a beginner in R, building my first Shiny app. I ran into a problem: when I run the app, it doesn't display any data - all valueboxes and plots are empty, the datatable is empty. No error shows up though. This happened after I modified the dataset and broke down the datetime column in day, month, year, dayofweek, hour, minute (I did the modification in a separate Rmd file and saved the dataframe successfully). The new dataframe imports successfully in Rstudio. Could someone please point me to what I'm doing wrong? Here is my code:
if(!require(plotly)){install.packages(c("plotly"))}
library(tidyverse)
library(shiny)
library(shinydashboard)
library(DT)
library(lubridate)
library(fontawesome)
library(scales)
library(readxl)
library(tmap)
library(maps)
library(sf)
library(mapsf)
library(viridis)
library(leaflet)
load("ufo_my_project1.RData")
ui <- dashboardPage(
dashboardHeader(title = "UFO Sightings - USA"),
dashboardSidebar(
sliderInput("years", h4("Years"),
min = 1950, max = 2010,
value = c(1950, 2010), step = 5, sep = ""),
selectInput("state", h4("State"),
choices = c("All states", as.character(unique(ufo$state)) %>% sort()),
selected = "All states", multiple = TRUE)
),
dashboardBody(
tabBox(
height='920px', width = 12,
tabPanel('Welcome', valueBoxOutput('box1'), valueBoxOutput('box2'), valueBoxOutput('box3'),
'INSERT WELCOME TEXT HERE'),
tabPanel('States Analysis', "INSERT WELCOME TEXT HERE", plotOutput('plot1', height = "510px"), plotOutput('plot2', height = "510px")),
tabPanel('Shapes Exploration', plotOutput('plot3')),
tabPanel('Time & Date', plotOutput('plot4')),
tabPanel('Map of Occurences', plotOutput('plot5')),
tabPanel('Sentiment Analysis', plotOutput('plot6')),
tabPanel('Data', DT::dataTableOutput ("table_1"))
)
)
)
server <- function(input, output){
data <- reactive({
if(input$state == "All states"){
ufo %>%
filter(year >= input$years[1],
year <= input$years[2])
} else {
ufo %>%
filter(year >= input$years[1],
year <= input$years[2],
state %in% input$state)
}
})
output$table_1 <- DT::renderDataTable({data()},
options = list(
lengthMenu = list(c(10, 12, 19), c('10 lines', '12 rows', '19 items')),
pageLength = 10
))
output$box1 <- renderValueBox({
ufo <- data()
valueBox(
value = nrow(ufo),
subtitle = "Total UFO Sightings",
icon = icon("satellite", lib = "font-awesome"),
color = "olive"
)
})
output$box2 <- renderValueBox({
ufo <- data()
valueBox(
value = n_distinct(ufo$ufo_shape),
subtitle = "Different UFO Shapes Observed",
icon = icon("shapes", lib = "font-awesome"),
color = "olive"
)
})
output$box3 <- renderValueBox({
ufo <- data()
valueBox(
value = n_distinct(ufo$city),
subtitle = "US Cities Where UFOs Were Seen",
icon = icon("city", lib = "font-awesome"),
color = "olive"
)
})
output$plot1 <- renderPlot(
data() %>%
group_by(state) %>%
na.omit()%>%
summarize(nb_occurences=n()) %>%
ggplot(aes(nb_occurences, reorder(state, nb_occurences))) + geom_col(stat ='identity') + geom_col(fill = "#A4B9C2", alpha = 0.5)
)
output$plot2 <- renderPlot(
data() %>%
group_by(city) %>%
na.omit()%>%
summarize(nb_occurences=n()) %>%
top_n(20, nb_occurences) %>%
ggplot(aes(nb_occurences, reorder(city, nb_occurences))) + geom_col(stat ='identity') + geom_col(fill = "#A4B9C2", alpha = 0.5)
)
output$plot3 <- renderPlot(
data() %>%
group_by(ufo_shape) %>%
na.omit()%>%
summarize(nb_occurences=n()) %>%
ggplot(aes(x=ufo_shape, y=nb_occurences)) +
geom_point(size=3) +
geom_segment(aes(x=ufo_shape,
xend=ufo_shape,
y=0,
yend=nb_occurences)) +
labs(title="Lollipop Chart",
subtitle="Make Vs Avg. Mileage",
caption="source: mpg") +
theme(axis.text.x = element_text(angle=30, vjust=0.6)))
output$plot4 <- renderPlot(
data() %>%
group_by(ufo_shape, state) %>%
summarise(count = n()) %>%
top_n(n = 5, wt = count) %>%
ggplot(aes(x = state, y = count) + geom_col(stat ='identity') + facet_grid()
))
output$plot5 <- renderPlot(
data() %>%
group_by(year) %>%
na.omit()%>%
summarize(nb_occurences=n()) %>%
ggplot(aes(nb_occurences, reorder(year), nb_occurences))) + geom_col(stat ='identity') + geom_col(fill = "#A4B9C2", alpha = 0.5)
}
# Run the app ----
shinyApp(ui = ui, server = server)