I'm putting together a flexdashboard
with spatial data collected over time. I want to have a user input for selection, and an animated map of the selected subset, showing coordinates by date.
The animation works great without the user selection. But when I add the user selection bit, the animation bar disappears. Any help with running the animation post-selection would be appreciated!
toy example (to be saved as Rmd file):
---
title: "Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: columns
---
```{r setup, include=FALSE}
library(crosstalk) # Link interactive plots
library(flexdashboard) # Create the dashboard
library(dplyr) # Powerful data-wrangling library
library(plotly) # Interactive plots
```
```{r}
# Import data
data <- expand.grid(Lat = c(46.5, 46.7, 46.8), Lon = seq(-110, -115, -0.5), Group = c("A", "B"),
Date = as.Date(c("24/02/2020", "25/02/2020", "26/02/2020", "27/02/2020", "28/02/2020", "29/02/2020"),
format = "%d/%m/%Y")) %>%
mutate(Date = as.factor(Date))
data <- data[sample(1:nrow(data), 50), ]
# Shared data
S_data <- SharedData$new(data)
```
Date animation
=====================================
Column 1 {data-width=80}
-----------------------------------------------------------------------
### Data selection - uncomment this to break the animation
```{r}
# filter_select(
# id = "group",
# label = "group",
# sharedData = S_data,
# group = ~Group,
# multiple = FALSE)
```
```{r, warning = FALSE, echo = FALSE, message = FALSE}
fig <- S_data %>%
plot_ly(
lat = ~Lat,
lon = ~Lon,
type = "scattermapbox",
marker = list(color = "fuchsia"),
frame = ~Date)
fig <- fig %>%
layout(
mapbox= list(
style = "white-bg",
sourcetype = 'raster',
zoom = 4,
center = list(lon = -112 ,lat= 46.7),
layers = list(list(
below = 'traces',
sourcetype = "raster",
source = list("https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}")))))
fig
```
Apparently, the issue was not that the animation bar disappeared, but that it was pushed so far down, that you couldn't see it. I'm only posting this answer in case someone else gets stumped by this. Separating the user input from the map/animation bar in different columns fixed the issue.
---
title: "Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: columns
---
```{r setup, include=FALSE}
library(crosstalk) # Link interactive plots
library(flexdashboard) # Create the dashboard
library(dplyr) # Powerful data-wrangling library
library(plotly) # Interactive plots
```
```{r}
# Import data
data <- expand.grid(Lat = c(46.5, 46.7, 46.8), Lon = seq(-110, -115, -0.5), Group = c("A", "B"),
Date = as.Date(c("24/02/2020", "25/02/2020", "26/02/2020", "27/02/2020", "28/02/2020", "29/02/2020"),
format = "%d/%m/%Y")) %>%
mutate(Date = as.factor(Date))
data <- data[sample(1:nrow(data), 50), ]
# Shared data
S_data <- SharedData$new(data)
```
Date animation
=====================================
Column 1 {data-width=80}
-----------------------------------------------------------------------
### Data selection
```{r}
filter_select(
id = "group",
label = "group",
sharedData = S_data,
group = ~Group,
multiple = FALSE)
```
Column 2 {data-width=300}
-----------------------------------------------------------------------
```{r, warning = FALSE, echo = FALSE, message = FALSE}
fig <- S_data %>%
plot_ly(
lat = ~Lat,
lon = ~Lon,
type = "scattermapbox",
marker = list(color = "fuchsia"),
frame = ~Date)
fig <- fig %>%
layout(
mapbox= list(
style = "white-bg",
sourcetype = 'raster',
zoom = 4,
center = list(lon = -112 ,lat= 46.7),
layers = list(list(
below = 'traces',
sourcetype = "raster",
source = list("https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}")))))
fig
```