'tibble slice function error in a R Shiny app
I am trying to make a RShiny app to visualize a time series app. The app also allows the user to set/change a range of dates, this range will then update the Leaflet map points accordingly. In order to define the range on line 140 under sliderInput, I am using the slice function to get the slider range (initially rows 1:3) from a tibble. But I am getting an error while setting the range which as follows :
How can I slice an initial range (that the user can then change) from a tibble to create a double slider (range) sliderInput?
Warning: Error in : `slice()` expressions should return indices (positive or negative integers).
Code
# This is a Shiny time series map web application
library(shiny)
library(tidyverse)
library(tidyr)
library(leaflet)
library(xts)
# Create an xts to tibble function to get the maximum and
# minimum dates using slice_min and slice_max with pull
xts_to_tibble = function(xts_obj){
data.frame(index(xts_obj), coredata(xts_obj)) %>%
set_names(c("date", names(xts_obj))) %>%
as_tibble()
}
dft = xts_to_tibble(df)
# Create sample data
Date = c("2014-04-08", "2014-06-04", "2014-04-30",
"2014-05-30", "2014-05-01")
lat = as.numeric(c("45.53814", "45.51076", "45.43560", "45.54332",
"45.52234"))
lon = as.numeric(c("-73.63672", "-73.61029", "-73.60100",
"-73.56000 ", "-73.59022"))
id = as.numeric(c("1", "2", "3", "4", "5"))
# Create a df from the above columns
df = data.frame(id, lat, lon, Date)
df$Year = lubridate::year(df$Date)
df$Month = lubridate::month(df$Date, label = TRUE, abbr=FALSE)
df$Week = lubridate::week(df$Date)
df$Date = as.Date(df$Date)
ui = fluidPage(
htmltools::htmlDependencies(icon("", verify_fa = FALSE)),
tags$style(
'
.plot-zoom {
position: absolute;
border: none;
background-color: transparent;
bottom: 0;
left: 0;
z-index: 1;
}
.full-screen {
position: fixed;
height: 100vh !important;
width: 100vw !important;
left: 0;
top: 0;
z-index: 9999;
overflow: hidden;
}
.leaflet-full-screen {
position: relative;
}
'
),
# Title
titlePanel("Time Series Visiualization Map"),
sidebarLayout(
# Define the sidebar
sidebarPanel(
radioButtons(inputId = "Frequency",
label = " Select Time Series Frequency",
choices = c("weeks",
"months",
"years"),
selected = "weeks",
inline = T),
uiOutput("Time_Series_UI")
),
mainPanel(
div(
class = "leaflet-full-screen",
leafletOutput("Time_Series_Map"))),
),
tags$script(HTML(
"
function plotZoom(el){
el = $(el);
var parent = el.parent().parent();
if(el.attr('data-full_screen') === 'false') {
parent.addClass('full-screen')
.css('position', '')
.trigger('resize').fadeOut().fadeIn();
el.attr('data-full_screen', 'true');
} else {
parent.removeClass('full-screen')
.css('position', 'relative')
.trigger('resize').fadeOut().fadeIn();
el.attr('data-full_screen', 'false');
}
}
$(function(){
$('.leaflet-full-screen .leaflet.html-widget').append(
`
<div class='plot-zoom'>
<button onclick=plotZoom(this) data-full_screen='false' title='Full screen'>
<i class='fa fa-expand-arrows-alt'></i>
</button>
</div>
`);
})
"
))
)
# Define server logic required
server = function(input, output) {
# Render slider input depending on data frequency
observe({
# Create an xts object
df_xts = xts(df, order.by = as.Date(df$Date))
#All_Dates = unique(df$Start_Date)
Filtered_Dates = df_xts[xts::endpoints(
df_xts,
on = input$Frequency
)] %>% xts_to_tibble()
output$Time_Series_UI = renderUI({
sliderInput("Date", "Date:",
min = pull(slice_min(Filtered_Dates, date),date),
max = pull(slice_max(Filtered_Dates, date),date),
value = Filtered_Dates[1:3,][[1]],
step = 1,
#timeFormat = "%YYYY-%MM-%DD",
animate = T)
})
})
# Filter data for the date selected
Filtered_Data = reactive({
return(subset(df, (Week >= input$Date[1] && month <= input$Date[2])))
})
# Filtered_Data = reactive({
# req(input$Date)
# filter(df, Date == input$Date)
# })
# Create the leaflet map
output$Time_Series_Map = renderLeaflet({
leaflet(df) %>%
addTiles() %>%
setView(lat = 0, lng = 0, zoom = 2)
})
# Create data markers for selected date
observe({
leafletProxy("Time_Series_Map", data = Filtered_Data()) %>%
addCircleMarkers(lng = ~lon, lat = ~lat,
popup = ~id)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Update
I did try using this answer but I am still getting the same error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
