Multiple Select All buttons causing problems with interactivity in Shiny map - r

I'm coding an interactive map in R with Shiny and Leaflet. I programmed 1 select all button for the months (checkGroup) and it worked fine, but adding the select all button for the other inputs has caused none of the map to work properly.
#import data
data <- structure(list(Area = c("Scarborough", "Etobicoke", "East York",
"North York", "North York", "Etobicoke", "Downtown Core (Central)",
"York", "Downtown Core (Central)", "York"), occurrenceyear = c(2017L,
2018L, 2018L, 2018L, 2018L, 2018L, 2017L, 2018L, 2018L, 2018L
), occurrencemonth = structure(c(12L, 5L, 5L, 5L, 5L, 5L, 6L,
12L, 12L, 12L), .Label = c("", "April", "August", "December",
"February", "January", "July", "June", "March", "May", "November",
"October", "September"), class = "factor"), Long = c(-79.1886063,
-79.5458221, -79.3138199, -79.4392548, -79.4406738, -79.5390091,
-79.3820572, -79.4840012, -79.3930817, -79.4356079), Lat = c(43.7639694,
43.5895691, 43.6753197, 43.7586555, 43.727829, 43.6431503, 43.6683502,
43.6842308, 43.6707535, 43.6820869)), row.names = c(NA, 10L), class = "data.frame")
# Define UI ----
ui <- fluidPage(
titlePanel("Interactive Toronto Auto Theft Visualization"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("checkGroup", h3("Month"), choices = list("January", "February", "March", "April", "May", "June", "July", "August" ,"September", "October", "November", "December"), selected = "Janurary"),
actionLink("selectall", "Select All"),
checkboxGroupInput("checkGroup2", h3("Year"),
choices = list(2014, 2015,2016 , 2017, 2018 ), selected = 2018),
actionLink("Selectall2", "Select All"),
checkboxGroupInput("checkGroup3", "Toronto Neighbourhoods", choices = list("Downtown Core (Central)", "East End", "North End", "West End", "East York", "Etobicoke", "North York", "Scarborough", "York"), selected = "York"),
actionLink("Selectall3", "Select All")
),
mainPanel (leafletOutput("map", "100%", 500))
))
# Define server logic ----
server <- function(input, output, session){
observe({
if(input$selectall == 0) return(NULL)
else if(input$selectall%%2==0)
{
updateCheckboxGroupInput(session, "checkGroup", "Month", choices = list("January", "February", "March", "April", "May", "June", "July", "August" ,"September", "October", "November", "December"))
}
else
{
updateCheckboxGroupInput(session, "checkGroup", "Month", choices = list("January", "February", "March", "April", "May", "June", "July", "August" ,"September", "October", "November", "December"), selected = list("January", "February", "March", "April", "May", "June", "July", "August" ,"September", "October", "November", "December"))
}
if(input$Selectall2 == 0) return(NULL)
else if(input$Selectall2 %%2 == 0)
{
updateCheckboxGroupInput(session, "checkGroup2", "Year", choices = list(2014, 2015,2016 , 2017, 2018))
}
else
{
updateCheckboxGroupInput(session, "checkGroup2", "Year", choices = list(2014, 2015,2016 , 2017, 2018), selected = list(2014, 2015,2016 , 2017, 2018))
}
})
filtered <- reactive({
if (is.null(input$checkGroup) & is.null(input$checkGroup2) & is.null(input$checkGroup3)){
return (NULL)
}
data %>% filter(occurrencemonth %in% input$checkGroup & occurrenceyear %in% input$checkGroup2 & Area %in% input$checkGroup3)
})
output$map <- renderLeaflet({
leaflet()%>%
addProviderTiles("CartoDB") %>%
addCircleMarkers(data = filtered(), radius = 2)
})
}
I believe the problem is in the observe function because that is where the programming for the select all buttons are placed. I've only programmed 2 of the buttons before running into the problem and was trying to fix the issue before adding in the third button (selectall3).
I've tried creating two separate observe functions for the two separate buttons, but that did not fix the problem.

You have typo here:
if(input$selectall2 == 0) return(NULL)
Should be:
if(input$Selectall2 == 0) return(NULL)

Related

How to avoid error "must be size" when changing month names to numbers?

I am trying to convert a character column containing month names into a numerical column containing numbers (1=January, etc.).
Example 1. This works:
df2 <- structure(list(Month = c("January", "January", "March", "March", "April")), class = "data.frame", row.names = c(NA, -5L))
df2 %>%
group_by(Month) %>%
mutate(Month = which(Month == month.name)) -> df2
Example 2. This does not work (error: "Month must be size 31 or 1, not 2."):
df <- structure(list(Month = c("August", "August", "August", "August",
"August", "August", "August", "August", "August", "August", "August",
"August", "August", "August", "August", "August", "August", "August",
"August", "August", "August", "August", "August", "August", "August",
"August", "August", "August", "August", "August", "August", "December",
"December", "December", "December")), class = "data.frame", row.names = c(NA, -35L))
df %>%
group_by(Month) %>%
mutate(Month = which(Month == month.name)) -> df
The code for converting is the same in both cases. Why doesn't it work in the second example? I can't get my head around it.

Merging separate Month and Year columns to graph in ggplot2

I've been around the forums looking for a solution to my issue but can't seem to find anything. Derivatives of my question and their answer haven't really helped either. My data has four columns, one for Year and one for Month). I've been wanting to plot the data all in one graph without using any facets for years in ggplot. This is what I've been struggling with so far with:
df<-data.frame(Month = rep(c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October",
"November", "February", "March"),each = 20),
Year = rep(c("2018", "2019"), times = c(220, 40)),
Type = rep(c("C", "T"), 260),
Value = runif(260, min = 10, max = 55))
df$Month<-ordered(df$Month, month.name)
df$Year<-ordered(df$Year)
ggplot(df) +
geom_boxplot(aes(x = Month, y = Value, fill = Type)) +
facet_wrap(~Year)
I'd ideally like to manage this using dplyr and lubridate. Any help would be appreciated!
One option would be to make a true date value, then you can use the date axis formatter. Something like this is a rough start
ggplot(df) +
geom_boxplot(aes(x = lubridate::mdy(paste(Month, 1, Year)), y = Value, fill = Type, group=lubridate::mdy(paste(Month, 1, Year)))) +
scale_x_date(breaks="month", date_labels = "%m")
Do you mean this?
df<-data.frame(Month = rep(c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October",
"November", "February", "March"),each = 20),
Year = rep(c("2018", "2019"), times = c(220, 40)),
Type = rep(c("C", "T"), 260),
Value = runif(260, min = 10, max = 55))
df$Month <- factor(df$Month,levels=c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October",
"November", "Dicember"), ordered = T)
df$Month<-ordered(df$Month)
df$Year<-ordered(df$Year)
df$Year_Month <- paste0(df$Month, " ", df$Year)
df$Year_Month <- factor(df$Year_Month, levels = unique(df$Year_Month))
ggplot(df) +
geom_boxplot(aes(x = Year_Month, y = Value, fill = Type))

change graph according to slider position

Here is my code. I am trying to make an rshiny page to show the mean symptoms in the past 30 days vesus the state based off of slider position (1 to 12 - for month). I know it is a little sloppy but I almost have it. I can get a graph that changes the title based off of the month on the slider but the graph just lists all of the data and not by month. Any help would be great.
`asthma = read.csv("AsthmaChild.Ozone.2006_2007.Sample.csv")
state.month = asthma[,-3:-10]
state.month = state.month[,-4]
state.month = aggregate(state.month$Symptoms.Past30D ~ state.month$STATE +
state.month$Month, state.month, mean)
colnames(state.month) = c("STATE", "Month", "Symptoms.Past30D")
sd = asthma[,-3:-10]
sd = sd[,-4]
sd = aggregate(sd$Symptoms.Past30D ~ sd$STATE + sd$Month, sd, function(x)
sd = sd(x))
colnames(sd) = c("STATE", "Month", "sd")
merged = merge(state.month,sd, by=c("STATE", "Month"))
df = count(asthma, "STATE", "Month")
colnames(df) = c("STATE","Freq")
data = merge(df, merged,by=c("STATE"))
data$sem = (data$sd)/(sqrt(data$Freq))
merged = data
merged$ConfUp = (merged$Symptoms.Past30D) + (merged$sem)
merged$ConfDown = (merged$Symptoms.Past30D) - (merged$sem)
merged$Month = as.character(merged$Month)
merged$Month = gsub("12", "December", merged$Month)
merged$Month = gsub("11", "November", merged$Month)
merged$Month = gsub("10", "October", merged$Month)
merged$Month = gsub("9", "September", merged$Month)
merged$Month = gsub("8", "August", merged$Month)
merged$Month = gsub("7", "July", merged$Month)
merged$Month = gsub("6", "June", merged$Month)
merged$Month = gsub("5", "May", merged$Month)
merged$Month = gsub("4", "April", merged$Month)
merged$Month = gsub("3", "March", merged$Month)
merged$Month = gsub("2", "February", merged$Month)
merged$Month = gsub("1", "January", merged$Month)
index = c(1:12)
values = c("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December")
ui = fluidPage(
sidebarPanel(
sliderInput("Month", "Month: Jan=1, Dec=12",min = 1, max =
12,step=1,value=1)),
mainPanel(plotOutput("plot")))
server = function(input,output){
sliderInput(inputId="Month",
label="Month: Jan=1, Dec=12",
min = 1,
max = 12,
value=1,
step=1)
mainPanel(plotOutput("plot"))
dat = reactive({
test <- merged[merged$Month %in%
seq(from=min(input$Month),to=max(input$Month),by=1),]
})
output$plot = renderPlot({
ggplot(data=merged, aes(x=Symptoms.Past30D, y = STATE)) +
geom_errorbarh(aes(xmin=ConfUp,xmax=ConfDown), height=1, linetype = 1) +
xlab ("Mean Sympotms.Past30D (SEM)") + ylab ("STATE") +
labs(title=paste(values[match(input$Month, index)]))
})
}
shinyApp(ui, server)`

How to order by Month? [duplicate]

I have this data frame called yy:
structure(list(Time = structure(c(1209096000, 1238731200, 1272600000,
1301666400, 1335794400, 1364835600, 1218772800, 1250222400, 1280808000,
1314028800, 1346421600, 1377835200, 1229317200, 1262235600, 1291352400,
1324047600, 1355497200, 1385960400, 1204261200, 1235710800, 1265950800,
1298646000, 1328281200, 1360940400, 1199250000, 1232082000, 1263186000,
1295017200, 1326466800, 1357149600), class = c("POSIXct", "POSIXt"
), tzone = ""), Peak_Logons = c(472452L, 398061L, 655849L, 881689L,
873720L, 1278295L, 340464L, 520943L, 1995150L, 883184L, 931721L,
1098553L, 405193L, 638301L, 734635L, 1254951L, 962391L, 1126432L,
316200L, 477407L, 674884L, 812793L, 898550L, 1541478L, 291564L,
394967L, 902076L, 916832L, 878264L, 918102L), Year = c("2008",
"2009", "2010", "2011", "2012", "2013", "2008", "2009", "2010",
"2011", "2012", "2013", "2008", "2009", "2010", "2011", "2012",
"2013", "2008", "2009", "2010", "2011", "2012", "2013", "2008",
"2009", "2010", "2011", "2012", "2013"), Month = c("April", "April",
"April", "April", "April", "April", "August", "August", "August",
"August", "August", "August", "December", "December", "December",
"December", "December", "December", "February", "February", "February",
"February", "February", "February", "January", "January", "January",
"January", "January", "January")), .Names = c("Time", "Peak_Logons",
"Year", "Month"), row.names = c(35479L, 30535L, 23645L, 15248L,
49696L, 8077L, 24651L, 13098L, 20204L, 47450L, 41228L, 20740L,
28049L, 9739L, 2636L, 50230L, 43746L, 3435L, 38091L, 28351L,
7382L, 3343L, 47824L, 45481L, 23951L, 29664L, 10024L, 4545L,
38808L, 44205L), class = "data.frame")
What I would like to do is create a heat map, Year on the y-axis and Month on the x-axis.
I am doing this:
ggplot(yy ,aes(Month, Year, fill=Peak_Logons)) +
geom_tile() +
theme_bw() +
guides(fill = guide_legend(keywidth = 5, keyheight = 1)) +
theme(axis.text.x = element_text(size=10, angle=45, hjust=1))
This kinda works but Months on x-axis are not order from January, February, March, April ... December.
They are order aphabetically from April, August etc.
How would I order the x-axis from January to December?
Is there a way to change the default colors, it looks like it is using blue shades?
Can I isert text the the geom_tiles? I would like to insert Time and Peak_Logons inside the tiles.
I would really appreciate any insight.
Use reorder to arrange your axis labels. I create a new column with month index.
geom_text to add text. Maybe you should play with text size.
scale_fill_gradientn to change fill color. See also scale_fill_gradientn
dat.m <- data.frame(Month=months(seq(as.Date("2000/1/1"),
by = "month", length.out = 12)),month.id = 1:12)
yy <- merge(yy,dat.m)
library(ggplot2)
ggplot(yy ,aes(reorder(Month,month.id), Year, fill=Peak_Logons)) +
geom_tile() +
theme_bw() +
guides(fill = guide_legend(keywidth = 5, keyheight = 1)) +
theme(axis.text.x = element_text(size=10, angle=45, hjust=1)) +
geom_text(aes(label=paste(Peak_Logons,format(Time,"%H"),sep='-'))) +
scale_fill_gradient(low = "yellow", high = "red")
You seem to consider months an ordered factor. You should make it one in R:
Month = c("April", "April",
"April", "April", "April", "April", "August", "August", "August",
"August", "August", "August", "December", "December", "December",
"December", "December", "December", "February", "February", "February",
"February", "February", "February", "January", "January", "January",
"January", "January", "January")
Month.ordered <- ordered(Month, month.name)
#[1] April April April April April April August August August August August August December December December December
#[17] December December February February February February February February January January January January January January
#Levels: January < February < March < April < May < June < July < August < September < October < November < December

how do you order Months in ggplot

I have this data frame called yy:
structure(list(Time = structure(c(1209096000, 1238731200, 1272600000,
1301666400, 1335794400, 1364835600, 1218772800, 1250222400, 1280808000,
1314028800, 1346421600, 1377835200, 1229317200, 1262235600, 1291352400,
1324047600, 1355497200, 1385960400, 1204261200, 1235710800, 1265950800,
1298646000, 1328281200, 1360940400, 1199250000, 1232082000, 1263186000,
1295017200, 1326466800, 1357149600), class = c("POSIXct", "POSIXt"
), tzone = ""), Peak_Logons = c(472452L, 398061L, 655849L, 881689L,
873720L, 1278295L, 340464L, 520943L, 1995150L, 883184L, 931721L,
1098553L, 405193L, 638301L, 734635L, 1254951L, 962391L, 1126432L,
316200L, 477407L, 674884L, 812793L, 898550L, 1541478L, 291564L,
394967L, 902076L, 916832L, 878264L, 918102L), Year = c("2008",
"2009", "2010", "2011", "2012", "2013", "2008", "2009", "2010",
"2011", "2012", "2013", "2008", "2009", "2010", "2011", "2012",
"2013", "2008", "2009", "2010", "2011", "2012", "2013", "2008",
"2009", "2010", "2011", "2012", "2013"), Month = c("April", "April",
"April", "April", "April", "April", "August", "August", "August",
"August", "August", "August", "December", "December", "December",
"December", "December", "December", "February", "February", "February",
"February", "February", "February", "January", "January", "January",
"January", "January", "January")), .Names = c("Time", "Peak_Logons",
"Year", "Month"), row.names = c(35479L, 30535L, 23645L, 15248L,
49696L, 8077L, 24651L, 13098L, 20204L, 47450L, 41228L, 20740L,
28049L, 9739L, 2636L, 50230L, 43746L, 3435L, 38091L, 28351L,
7382L, 3343L, 47824L, 45481L, 23951L, 29664L, 10024L, 4545L,
38808L, 44205L), class = "data.frame")
What I would like to do is create a heat map, Year on the y-axis and Month on the x-axis.
I am doing this:
ggplot(yy ,aes(Month, Year, fill=Peak_Logons)) +
geom_tile() +
theme_bw() +
guides(fill = guide_legend(keywidth = 5, keyheight = 1)) +
theme(axis.text.x = element_text(size=10, angle=45, hjust=1))
This kinda works but Months on x-axis are not order from January, February, March, April ... December.
They are order aphabetically from April, August etc.
How would I order the x-axis from January to December?
Is there a way to change the default colors, it looks like it is using blue shades?
Can I isert text the the geom_tiles? I would like to insert Time and Peak_Logons inside the tiles.
I would really appreciate any insight.
Use reorder to arrange your axis labels. I create a new column with month index.
geom_text to add text. Maybe you should play with text size.
scale_fill_gradientn to change fill color. See also scale_fill_gradientn
dat.m <- data.frame(Month=months(seq(as.Date("2000/1/1"),
by = "month", length.out = 12)),month.id = 1:12)
yy <- merge(yy,dat.m)
library(ggplot2)
ggplot(yy ,aes(reorder(Month,month.id), Year, fill=Peak_Logons)) +
geom_tile() +
theme_bw() +
guides(fill = guide_legend(keywidth = 5, keyheight = 1)) +
theme(axis.text.x = element_text(size=10, angle=45, hjust=1)) +
geom_text(aes(label=paste(Peak_Logons,format(Time,"%H"),sep='-'))) +
scale_fill_gradient(low = "yellow", high = "red")
You seem to consider months an ordered factor. You should make it one in R:
Month = c("April", "April",
"April", "April", "April", "April", "August", "August", "August",
"August", "August", "August", "December", "December", "December",
"December", "December", "December", "February", "February", "February",
"February", "February", "February", "January", "January", "January",
"January", "January", "January")
Month.ordered <- ordered(Month, month.name)
#[1] April April April April April April August August August August August August December December December December
#[17] December December February February February February February February January January January January January January
#Levels: January < February < March < April < May < June < July < August < September < October < November < December

Resources