使用子集 and/or dplyr:filter 的绘图不适用于动态反应对象
Plot using subset and/or dplyr:filter doesn't work with dynamic reactive objects
我尝试在 Shiny 中创建一个应用程序,所有 selectInput
都是动态反应对象,但目前它们通过变量 select 的组合制作了一些情节 (output$myplot
) ,我的情节不起作用 (Error in charToDate: character string is not in a standard unambiguous format
)。
问题是最终的 selection 对象 selectedvariable4
并尝试使用 selectedvariable4
、selectedvariable4()
、selectedvariable4()$ID_UNIQUE
和 unique(selectedvariable4()$ID_UNIQUE)
但没有成功。在我的例子中:
# Packages
library(rgdal)
library(shiny)
library(leaflet)
library(leaflet.providers)
library(ggplot2)
library(shinythemes)
library(sf)
library(lubridate)
library(dplyr)
# get AOI
download.file(
"https://github.com/Leprechault/trash/raw/main/stands_example.zip",
zip_path <- tempfile(fileext = ".zip")
)
unzip(zip_path, exdir = tempdir())
# Open the files
setwd(tempdir())
stands_extent <- readOGR(".", "stands_target") # Border
stands_ds <- read.csv("pred_target_stands.csv", sep=";") # Data set
stands_ds <- stands_ds %>%
mutate(DATA_S2 = ymd(DATA_S2))
# Create the shiny dash
ui <- fluidPage(
theme = shinytheme("cosmo"),
titlePanel(title="My Map Dashboard"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "selectedvariable0", "Type", choices = c(unique(stands_ds$PEST))),
selectInput(inputId = "selectedvariable1", "Date", choices = NULL),
selectInput(inputId = "selectedvariable2", "Project",choices = NULL),
selectInput(inputId = "selectedvariable3", "Stand",choices = NULL),
selectInput(inputId = "selectedvariable4", "ID-Unique",choices = NULL)
),
mainPanel(
textOutput("idSaida"),
fluidRow(
splitLayout(plotOutput("myplot"))),
dateInput(inputId = "Dates selection", label = "Time"),
leafletOutput("map")
)
)
)
server <- function(input, output, session){
#stands_ds<- reactive({stands_ds})
selectedvariable0 <- reactive({
filter(stands_ds, PEST == unique(stands_ds$PEST))
})
observeEvent(selectedvariable0(), {
choices <- unique(selectedvariable0()$DATA_S2)
updateSelectInput(inputId = "selectedvariable1", choices = choices)
})
selectedvariable1 <- reactive({
req(input$selectedvariable1)
filter(selectedvariable0(), DATA_S2 == as.Date(input$selectedvariable1))
})
observeEvent(selectedvariable1(), {
choices <- unique(selectedvariable1()$PROJETO)
updateSelectInput(inputId = "selectedvariable2", choices = choices)
})
selectedvariable2 <- reactive({
req(input$selectedvariable2)
filter(selectedvariable0(), PROJETO == input$selectedvariable2)
})
observeEvent(selectedvariable2(), {
choices <- unique(selectedvariable2()$CD_TALHAO)
updateSelectInput(inputId = "selectedvariable3", choices = choices)
})
selectedvariable3 <- reactive({
req(input$selectedvariable3)
filter(selectedvariable0(), CD_TALHAO == input$selectedvariable3)
})
observeEvent(selectedvariable3(), {
choices <- unique(selectedvariable3()$ID_UNIQUE)
updateSelectInput(inputId = "selectedvariable4", choices = choices)
})
selectedvariable4 <- reactive({
req(input$selectedvariable4)
filter(selectedvariable0(), ID_UNIQUE == input$selectedvariable4)
})
output$myplot <- renderPlot({
#Subset stand
stands_sel <- subset(stands_extent, stands_extent@data$ID_UNIQUE==unique(selectedvariable4()))
#Subset for input$var and assign this subset to new object, "fbar"
ds_sel<- stands_ds[stands_ds$ID_UNIQUE==unique(selectedvariable4()),]
#Create a map
polys <- st_as_sf(stands_sel)
ggplot() +
geom_sf(data=polys) +
geom_point(data=ds_sel,
aes(x=X, y=Y), color="red") +
xlab("Longitude") + ylab("Latitude") +
coord_sf() +
theme_bw() +
theme(text = element_text(size=10))
})
output$map <- renderLeaflet({
stands_actual<-stands_ds[stands_ds$ID_UNIQUE==unique(selectedvariable4()),]
lng <- mean(stands_actual$X)
lat <- mean(stands_actual$Y)
leaflet() %>%
setView(lng = lng, lat = lat, zoom=17) %>%
addProviderTiles(providers$Esri.WorldImagery) %>%
addMarkers(lng=stands_actual$X, lat=stands_actual$Y, popup="Location")
})
}
shinyApp(ui, server)
##
请问有什么解决办法吗?
OP post 中提到的错误是由于直接应用 as.Date
而没有对 format
进行任何说明。默认情况下,它使用的格式是 %Y-%m-%d
即 YYYY-MM-DD
。如果输入格式是其他任何格式,它会抛出如下错误
as.Date("12/24/2023")
#Error in charToDate(x) :
# character string is not in a standard unambiguous format
这里需要
as.Date("12/24/2023", format = '%m/%d/%Y')
#[1] "2023-12-24"
在 OP 的 post 中,format
并不清楚。自动检测 format
的一种选择是使用 anytime
中的 anydate
...
library(anytime)
filter(selectedvariable0(), DATA_S2 == anydate(input$selectedvariable1))
...
我尝试在 Shiny 中创建一个应用程序,所有 selectInput
都是动态反应对象,但目前它们通过变量 select 的组合制作了一些情节 (output$myplot
) ,我的情节不起作用 (Error in charToDate: character string is not in a standard unambiguous format
)。
问题是最终的 selection 对象 selectedvariable4
并尝试使用 selectedvariable4
、selectedvariable4()
、selectedvariable4()$ID_UNIQUE
和 unique(selectedvariable4()$ID_UNIQUE)
但没有成功。在我的例子中:
# Packages
library(rgdal)
library(shiny)
library(leaflet)
library(leaflet.providers)
library(ggplot2)
library(shinythemes)
library(sf)
library(lubridate)
library(dplyr)
# get AOI
download.file(
"https://github.com/Leprechault/trash/raw/main/stands_example.zip",
zip_path <- tempfile(fileext = ".zip")
)
unzip(zip_path, exdir = tempdir())
# Open the files
setwd(tempdir())
stands_extent <- readOGR(".", "stands_target") # Border
stands_ds <- read.csv("pred_target_stands.csv", sep=";") # Data set
stands_ds <- stands_ds %>%
mutate(DATA_S2 = ymd(DATA_S2))
# Create the shiny dash
ui <- fluidPage(
theme = shinytheme("cosmo"),
titlePanel(title="My Map Dashboard"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "selectedvariable0", "Type", choices = c(unique(stands_ds$PEST))),
selectInput(inputId = "selectedvariable1", "Date", choices = NULL),
selectInput(inputId = "selectedvariable2", "Project",choices = NULL),
selectInput(inputId = "selectedvariable3", "Stand",choices = NULL),
selectInput(inputId = "selectedvariable4", "ID-Unique",choices = NULL)
),
mainPanel(
textOutput("idSaida"),
fluidRow(
splitLayout(plotOutput("myplot"))),
dateInput(inputId = "Dates selection", label = "Time"),
leafletOutput("map")
)
)
)
server <- function(input, output, session){
#stands_ds<- reactive({stands_ds})
selectedvariable0 <- reactive({
filter(stands_ds, PEST == unique(stands_ds$PEST))
})
observeEvent(selectedvariable0(), {
choices <- unique(selectedvariable0()$DATA_S2)
updateSelectInput(inputId = "selectedvariable1", choices = choices)
})
selectedvariable1 <- reactive({
req(input$selectedvariable1)
filter(selectedvariable0(), DATA_S2 == as.Date(input$selectedvariable1))
})
observeEvent(selectedvariable1(), {
choices <- unique(selectedvariable1()$PROJETO)
updateSelectInput(inputId = "selectedvariable2", choices = choices)
})
selectedvariable2 <- reactive({
req(input$selectedvariable2)
filter(selectedvariable0(), PROJETO == input$selectedvariable2)
})
observeEvent(selectedvariable2(), {
choices <- unique(selectedvariable2()$CD_TALHAO)
updateSelectInput(inputId = "selectedvariable3", choices = choices)
})
selectedvariable3 <- reactive({
req(input$selectedvariable3)
filter(selectedvariable0(), CD_TALHAO == input$selectedvariable3)
})
observeEvent(selectedvariable3(), {
choices <- unique(selectedvariable3()$ID_UNIQUE)
updateSelectInput(inputId = "selectedvariable4", choices = choices)
})
selectedvariable4 <- reactive({
req(input$selectedvariable4)
filter(selectedvariable0(), ID_UNIQUE == input$selectedvariable4)
})
output$myplot <- renderPlot({
#Subset stand
stands_sel <- subset(stands_extent, stands_extent@data$ID_UNIQUE==unique(selectedvariable4()))
#Subset for input$var and assign this subset to new object, "fbar"
ds_sel<- stands_ds[stands_ds$ID_UNIQUE==unique(selectedvariable4()),]
#Create a map
polys <- st_as_sf(stands_sel)
ggplot() +
geom_sf(data=polys) +
geom_point(data=ds_sel,
aes(x=X, y=Y), color="red") +
xlab("Longitude") + ylab("Latitude") +
coord_sf() +
theme_bw() +
theme(text = element_text(size=10))
})
output$map <- renderLeaflet({
stands_actual<-stands_ds[stands_ds$ID_UNIQUE==unique(selectedvariable4()),]
lng <- mean(stands_actual$X)
lat <- mean(stands_actual$Y)
leaflet() %>%
setView(lng = lng, lat = lat, zoom=17) %>%
addProviderTiles(providers$Esri.WorldImagery) %>%
addMarkers(lng=stands_actual$X, lat=stands_actual$Y, popup="Location")
})
}
shinyApp(ui, server)
##
请问有什么解决办法吗?
OP post 中提到的错误是由于直接应用 as.Date
而没有对 format
进行任何说明。默认情况下,它使用的格式是 %Y-%m-%d
即 YYYY-MM-DD
。如果输入格式是其他任何格式,它会抛出如下错误
as.Date("12/24/2023")
#Error in charToDate(x) :
# character string is not in a standard unambiguous format
这里需要
as.Date("12/24/2023", format = '%m/%d/%Y')
#[1] "2023-12-24"
在 OP 的 post 中,format
并不清楚。自动检测 format
的一种选择是使用 anytime
anydate
...
library(anytime)
filter(selectedvariable0(), DATA_S2 == anydate(input$selectedvariable1))
...