[英]Subset dataframe with daterangeInput before further subsetting without Shiny
這是我第一次使用stackoverflow提問,所以我希望以下內容足夠清楚:
我正在制作一個Shiny應用程序,我使用Leaflet繪制數據。 該圖目前由三個層組成(類型= a,類型= b,類型= a + b),可以使用復選框選擇,並且我還添加了切片器以選擇持續時間。
現在,一切正常,但我想添加一個DaterangeInput。
我的數據框由五個變量(最簡單的形式)組成,名稱/位置/類型/持續時間/日期。 同名可以有很多觀察,因為它們是事件,同一個名稱的不同觀察可以有type = a或type = b。
對於我目前正在運行的Shiny應用程序,我將group_by(name)的原始數據用於type = a,type = b和type = a + b。 這樣我得到三層,我可以通過我的復選框組選擇。 當滑塊用於更改我的Leaflet地圖上的標記數量時,子集化的數據框隨后在反應函數中使用。
我現在要做的是通過在其他所有內容之前使用daterangeInput來對我的原始數據幀進行子集化。 這樣做的原因是日期是唯一的唯一變量,這就是為什么我希望它作為第一個過濾器,但是直到我制作了一個功能正常的Shiny應用程序,我才想添加一個小東西:P
這是我的代碼的簡化版本
ui <- fluidPage(
titlePanel(Title),
sidebarLayout(
sidebarPanel(
#slider for number of events
sliderInput(inputId = "events",
label = "number:",
min = 1, max = 100,
value = c(1,100),
step = 1),
#type a and/or b
checkboxInput(inputId = "a",
label = "a",
value = TRUE),
checkboxInput(inputId = "b",
label = "b",
value = TRUE),
#Daterange for events to plot
dateRangeInput(inputId = "date",
label = "from - until:",
start = 1-1-2018,
end = 31-12-2019,
min = 1-1-2018,
max = 31-12-2019,
format = "dd/mm/yyyy",
separator = " - "),
),
#printing map
mainPanel(
leafletOutput(outputId = "map", width = "100%", height = 900)
)
)
)
server <- function(input, output) {
output$map <- renderLeaflet({
#plot empty map
empty_map <- leaflet() %>%
addTiles() %>%
setView(lng = 5.583541, lat = 52.577159, zoom = 8)
})
raw_date <- reactive({
raw[raw$date >= input$date[1] & raw$date <= input$date[2],]
raw
})
#other filters
#removing duplicate adresses
reactive({
rawdate <- raw_date()
Name_unq <- rawdate[!duplicated(rawdate$Adres),]
Name_unq <- Adr_tot_unq[order(Adr_tot_unq$Adres),]
#determining information per event
type_ab <- rawdate %>%
group_by(Adres) %>%
summarise(Total = sum(duration), mean = mean(duration)) %>%
ungroup()
#link adresses and location
type_ab <- data.frame(type_ab,Name_unq$Longitude,Name_unq$Latitude)
names(type_ab)[7:8] <- c("Longitude", "Latitude")
#determining which layer to plot
observeEvent({input$a
input$b},
{if(input$a == TRUE & input$b == TRUE) {
lpRemoveAll()
lpAddTotal()
} else if(input$a == FALSE & input$b == FALSE) {
lpRemoveAll()
}
}
)
#define functions and type_ab-layer
lpAddTotal <- function() {
observe(
leafletProxy(mapId = "map", data = type_ab_slider()) %>%
clearMarkerClusters() %>%
addMarkers(clusterOptions = markerClusterOptions(),group = "Total")
)
}
#define function lpRemove
lpRemoveAll <- function() {
leafletProxy(mapId = "map") %>%
clearGroup("Total")
}
#functions to link sliders to layers
type_ab_slider <- reactive({
type_ab[(type_ab$Aantal >= input$events[1] & type_ab$Aantal <= input$events[2]),]
})
shinyApp(ui = ui, server = server)
結束語:我想通過使用daterangeInput對原始數據進行子集化,之后我想對該子集進行進一步的過濾。
嘗試:
raw_date <- reactive({
raw <- subset(raw, date >= input$date[1] & date <= input$date[2])
raw
})
和:
reactive({
rawdate<-raw_date()
type_a <- rawdate[type == "a"]
type_a
})
你不需要第二部分的觀察者,被動做你正在尋找的東西。 希望這可以幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.