I have been going round in circles with my code, I have checked it line by line and don't have any errors, it is also reading from my data table but my scatter plot isn't rendering. My data has over 700 rows and 5 columns so I have added 3 filters (1 filter contains 3 columns) to reduce the code, which you'll see are their living status, the date and the region of incident (which I will probably change to multiple choice as it is currently all or one selected region). The code is below, any help would be greatly appreciated:
library(DT)
library(shiny)
library(ggplot2)
library(lubridate)
getwd()
migrants <- read.csv("MissingMigrants2019v3.csv", header = TRUE, sep = ',', stringsAsFactors = FALSE)
names(migrants) <- c("Region_of_Incident", "Reported_Date", "Number_Dead", "Estimated_Number_of_Missing", "Number_of_Survivors")
migrants$Reported_Date <- format(as.Date(migrants$Reported_Date, format = "%d/%m/%Y"), "%Y-%m-%d")
Define UI for application that draws a scatter plot.
ui <- fluidPage(
Application title
titlePanel("Missing Migrants 2019"),
sidebarLayout(
sidebarPanel(
radioButtons( #y axis living status
inputId = "Living_Status",
label = "Living Status",
choices = c("Number_Dead", "Estimated_Number_of_Missing", "Number_of_Survivors"),
selected = "Number_Dead",
inline = FALSE
),
dateRangeInput( #x axis
inputId = "Reported_Date",
label = "Date Range",
start = min(migrants$Reported_Date),
end = max(migrants$Reported_Date),
format = "yyyy-mm-dd",
separator = "-"
),
selectInput( # region filter
inputId = "Region_Filter",
label = "Filter by Region of Incident:",
choices = c("All", unique(migrants$Region_of_Incident))
),
submitButton(text = "Apply changes", icon = NULL)
),
mainPanel(
plotOutput(outputId = "plot",width = "900px", height = "600px"),
em("This graph shows how many individuals fell under the selected living status on the date given on the x axis"),
h1("Missing Migrants Data"),
DT::dataTableOutput(outputId = "migrationtable")
)
)
)
Define server logic required to draw a plot, everything above is UI
server <- function(input, output) {
Define a reactive value for filtered data
filtered_data <- reactive({
migrants$Reported_Date <- as.Date(migrants$Reported_Date, format = "%Y-%m-%d")
if(input$Region_Filter == "All") {
subset(
migrants,
input$Living_Status %in% migrants[[input$Living_Status]] >0 &
Reported_Date >= input$Reported_Date[1] &
Reported_Date <= input$Reported_Date[2]
)
}else{
subset(
migrants,
input$Living_Status %in% migrants[[input$Living_Status]] >0 &
Reported_Date >= input$Reported_Date[1] &
Reported_Date <= input$Reported_Date[2] &
Region_of_Incident == input$Region_Filter
)
}
})
Add reactive values for minimum and maximum values of selected variable
min_val <- reactive({
min(filtered_data()[[input$Living_Status]])
})
max_val <- reactive({
max(filtered_data()[[input$Living_Status]])
})
Render the plot
output$plot <- renderPlot({
ggplot(
filtered_data(),
aes(x = Reported_Date, y = !!sym(input$Living_Status))
) +
geom_point(alpha = 0.5)+
labs(y = "Number_of_People")
})
Render the table
output$migrationtable <- DT::renderDataTable({
migrants$Reported_Date <- as.Date(migrants$Reported_Date, format = "%Y-%m-%d")
subset(
migrants,
Reported_Date >= input$Reported_Date[1] &
Reported_Date <= input$Reported_Date[2]
)
})
React to the submit button click
observeEvent(input$Apply changes
, {
# Update the plot and table when the submit button is clicked
output$plot <- renderPlot({
ggplot(
filtered_data(),
aes(x = Reported_Date, y = !!sym(input$Living_Status),color = Region_of_Incident)
) +
geom_point(alpha = 0.5)+
labs(y = "Number_of_People")+
scale_color_discrete(name = "Region of Incident")
})
output$migrationtable <- DT::renderDataTable({
migrants$Reported_Date <- as.Date(migrants$Reported_Date, format = "%Y-%m-%d")
subset(
migrants,
Reported_Date >= input$Reported_Date[1] &
Reported_Date <= input$Reported_Date[2]
)
})
})
}
Run the application
shinyApp(ui = ui, server = server)