gpt4 book ai didi

R 传单 : conditional panel does not appear in map

转载 作者:行者123 更新时间:2023-12-05 04:11:54 26 4
gpt4 key购买 nike

我想在我的 map 上点击一个圆圈时显示一个条件面板,如果我在圆圈外点击这个条件面板必须消失,但它没有出现,我不知道为什么。

我认为这是关于 react 值(再一次)。

如果有什么想法,请告诉我。

非常感谢,这是一个可重现的例子(感谢 SymbolixAU):

用户界面:

library(shiny)
library(leaflet)

ui <- fluidPage(
leafletOutput("mymap",width="100%",height="750px"),

conditionalPanel(

condition = "output.COND == '2'",

fluidRow(

absolutePanel(id = "cond_panel",
class = "panel panel-default",
fixed = TRUE,
draggable = TRUE,
top = "auto",
left = 200,
right = "auto",
bottom = 0,
width = 400,
height = 400,
fluidRow(

) # e. of fluidRow(

) # # e. of absolutePanel

) # e. of fluidRow

) # e. of conditionalPanel

) # e. of fluidPage

和服务器:

server <- function(input, output){

rv <- reactiveValues()
rv$myDf <- NULL
rv$cond <- NULL

cities <- read.csv(textConnection("
City,Lat,Long,Pop
Boston,42.3601,-71.0589,645966
Hartford,41.7627,-72.6743,125017
New York City,40.7127,-74.0059,8406000
Philadelphia,39.9500,-75.1667,1553000
Pittsburgh,40.4397,-79.9764,305841
Providence,41.8236,-71.4222,177994
"))
cities$id <- 1:nrow(cities)

output$mymap <- renderLeaflet({
leaflet(cities) %>% addTiles() %>%
addCircles(lng = ~Long, lat = ~Lat, weight = 1,
radius = ~sqrt(Pop) * 30, popup = ~City, layerId = ~id)
})

observeEvent(input$mymap_click, {

print("map clicked")
rv$cond <- "1"
print(paste("Value rv$cond = ", rv$cond))
output$COND <- reactive({rv$cond})
leafletProxy("mymap")

}) # e. of observeEvent

observeEvent(input$mymap_shape_click, {

print("shape clicked")
rv$cond <- "2"
print(paste("Value rv$cond = ", rv$cond))
output$COND <- reactive({rv$cond})
leafletProxy("mymap")

}) # e. of observeEvent

} # e. of server

最佳答案

我将提出一种略有不同的方法,即使用 library(shinyjs)使用javascript来控制面板是否隐藏。

在这个例子中,我创建了一个hidden div 元素(即,当应用程序打开时,面板将开始隐藏)。然后单击圆圈时显示“div”,单击 map 时再次隐藏。

此答案的灵感来自 @Daattali's answer here (他是 library(shinyjs) 的作者。

library(shiny)
library(leaflet)
library(shinyjs)

ui <- fluidPage(
useShinyjs(), ## Call to use shinyJS
leafletOutput("mymap",width="100%",height="750px"),

#conditionalPanel(

#condition = "output.COND === '2'",
hidden(
div(id = "conditionalPanel",
fluidRow(

absolutePanel(id = "cond_panel",
class = "panel panel-default",
fixed = TRUE,
draggable = TRUE,
top = "auto",
left = 200,
right = "auto",
bottom = 0,
width = 400,
height = 400,
fluidRow(

) # e. of fluidRow(

) # # e. of absolutePanel

) # e. of fluidRow
)
)

# ) # e. of conditionalPanel

) # e. of fluidPage


server <- function(input, output){

rv <- reactiveValues()
rv$myDf <- NULL
rv$cond <- NULL

cities <- read.csv(textConnection("
City,Lat,Long,Pop
Boston,42.3601,-71.0589,645966
Hartford,41.7627,-72.6743,125017
New York City,40.7127,-74.0059,8406000
Philadelphia,39.9500,-75.1667,1553000
Pittsburgh,40.4397,-79.9764,305841
Providence,41.8236,-71.4222,177994
"))
cities$id <- 1:nrow(cities)

output$mymap <- renderLeaflet({
leaflet(cities) %>% addTiles() %>%
addCircles(lng = ~Long, lat = ~Lat, weight = 1,
radius = ~sqrt(Pop) * 30, popup = ~City, layerId = ~id)
})

observeEvent(input$mymap_click, {

shinyjs::hide(id = "conditionalPanel")
print("map clicked")
rv$cond <- "1"
print(paste("Value rv$cond = ", rv$cond))
output$COND <- reactive({rv$cond})
leafletProxy("mymap")

}) # e. of observeEvent

observeEvent(input$mymap_shape_click, {

shinyjs::show(id = "conditionalPanel")
print("shape clicked")
rv$cond <- "2"
print(paste("Value rv$cond = ", rv$cond))
output$COND <- reactive({rv$cond})
leafletProxy("mymap")

}) # e. of observeEvent

} # e. of server

shinyApp(ui, server)

关于R 传单 : conditional panel does not appear in map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41753439/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com