使用子集和/或dplyr打印:过滤器不适用于动态反应对象

使用子集和/或dplyr打印:过滤器不适用于动态反应对象,r,shiny,shiny-reactivity,R,Shiny,Shiny Reactivity,我试图在Shiny中创建一个应用程序,所有selectInput都是动态反应对象,但当它们通过变量select组合生成一些绘图输出$myplot时,我的绘图不起作用charToDate中的错误:字符串不是标准的明确格式。 问题在于最终选择对象selectedvariable4,尝试使用selectedvariable4、selectedvariable4、selectedvariable4$ID\u UNIQUE和uniqueselectedvariable4$ID\u UNIQUE,但未成功。

我试图在Shiny中创建一个应用程序,所有selectInput都是动态反应对象,但当它们通过变量select组合生成一些绘图输出$myplot时,我的绘图不起作用charToDate中的错误:字符串不是标准的明确格式。 问题在于最终选择对象selectedvariable4,尝试使用selectedvariable4、selectedvariable4、selectedvariable4$ID\u UNIQUE和uniqueselectedvariable4$ID\u 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帖子中提到的错误是由于直接应用as.Date而没有任何格式说明。默认情况下,它使用的格式是%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的帖子中,不清楚格式。自动检测格式的一个选项是使用anydate from anytime


您能否将choices=NULL更改为默认日期值?如果我选择choices=NULL by choices=cuniquestands\u ds$DATA\u S2,我将丢失响应性更改:好的,我认为根据显示的错误,是格式未正确检测到。我在下面发布了一个解决方案
...
library(anytime)
filter(selectedvariable0(), DATA_S2 == anydate(input$selectedvariable1))
...