【发布时间】:2026-01-23 08:25:01
【问题描述】:
在下面的闪亮示例中,我有 3 个变量(Project、Stand 和 ID_Unique)。我希望当我选择Project 时,变量Stand 和ID_Unique 将仅包含在输入中Project 中所做的选择中。这是我的详细示例:
# 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(
uiOutput("selectedvariable0"),
uiOutput("selectedvariable1"),
uiOutput("selectedvariable2"),
),
mainPanel(
textOutput("idSaida"),
fluidRow(
splitLayout(plotOutput("myplot"))),
dateInput(inputId = "Dates selection", label = "Time"),
leafletOutput("map")
)
)
)
server <- function(input, output, session){
output$selectedvariable0 <- renderUI({
selectInput("selectedvariable0",
label = "PROJECT",
choices = unique(stands_ds$PROJECT),
selected = TRUE )
})
data2 <- reactive({
req(input$selectedvariable0)
data2 <- subset(stands_ds, PROJECT %in% input$selectedvariable0)
})
output$selectedvariable1 <- renderUI({
req(data2())
selectInput("selectedvariable1",
label = "STAND",
choices = unique(data2()$CD_TALHAO),
selected = TRUE )
})
data3 <- reactive({
req(input$selectedvariable2,data2())
data3 <- subset(data2(), CD_TALHAO %in% input$selectedvariable1)
})
output$selectedvariable2 <- renderUI({
req(data3())
selectInput("selectedvariable2",
label = "ID UNIQUE",
choices = unique(data2()$ID_UNIQUE), ## use data3() instead of data2(), if you wish to subset from data3()
selected = TRUE )
})
currentvariable0 <- reactive({input$selectedvariable0})
currentvariable1 <- reactive({input$selectedvariable1})
currentvariable2 <- reactive({input$selectedvariable2})
output$myplot <- renderPlot({
#Subset stand
stands_sel <- subset(stands_extent, stands_extent@data$ID_UNIQUE==currentvariable4())
#Subset for input$var and assign this subset to new object, "fbar"
ds_sel<- stands_ds[stands_ds$ID_UNIQUE==currentvariable4(),]
#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==currentvariable4(),]
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)
##
请帮忙,因为只有两个反应变量(selectedvariable0 和 selectedvariable1)工作得很好,而我的情节也不起作用。
【问题讨论】:
-
变量 Stand 和 ID_Unique :在地图中?在输入中?
-
请在输入中@HubertL。我认为我需要在
selectInput参数之间创建一些规则。 -
请让我尝试重新表述您的需求:当我在 selectInput 中选择一个项目时,我希望其他 selectInputs 适应并仅显示与该项目相关的值。是这样吗?
-
如果有,请查看*.com/questions/34080629/…
-
也许回答here 可能会有所帮助。
标签: r shiny shiny-reactivity