更新应用程序内部传单地图时出现闪亮(R)问题

更新应用程序内部传单地图时出现闪亮(R)问题,r,leaflet,shiny,R,Leaflet,Shiny,我使用的是Shiny,我对传单有意见 我想做什么: 我的项目的最终目标是选择特定的法国县。 当选择这些县时,我会在移动中(通过合并)创建形状文件(这是在传单上绘制地图所必需的)和包含所有类型的公共数据(如人口数量等)的数据库,这些数据只对应于这些特定县 我的问题:我知道在闪亮的应用程序中,“合并”做得很好。但是,我的Outputmap不想工作 详细信息:事实上,我在另一个.R脚本上启动了我的“合并和绘图”,它工作得非常好(在启动脚本之前,我只定义了“手动”要绘图的县)。在我的ShinyApp上,

我使用的是Shiny,我对传单有意见

我想做什么: 我的项目的最终目标是选择特定的法国县。 当选择这些县时,我会在移动中(通过合并)创建形状文件(这是在传单上绘制地图所必需的)和包含所有类型的公共数据(如人口数量等)的数据库,这些数据只对应于这些特定县

我的问题:我知道在闪亮的应用程序中,“合并”做得很好。但是,我的Outputmap不想工作

详细信息:事实上,我在另一个.R脚本上启动了我的“合并和绘图”,它工作得非常好(在启动脚本之前,我只定义了“手动”要绘图的县)。在我的ShinyApp上,这个选择由
输入$choix_depa
变量执行

现在是一些代码。我有三个脚本可以同时做出反应:global.rui.R和server.R(正如您所知,这就是Shiny的工作原理)。 对于global.R(我这里只显示“interestsant”部分),我加载包含所有县数据的数据库文件(不是形状文件!只有数据)

在ui.R(用户界面)上,我有我的“选择”县:

shinydashboard::tabItem(tabName= "Departements", class = 'active',
           shiny::fluidPage(
           shiny::absolutePanel(
           draggable = FALSE,
           fixed = TRUE, 
           top = 60, left = "auto", right = 20, bottom = "auto",
           width = 330, height = "auto",
           wellPanel(
           shiny::h4("Départements"),
           selectizeInput(inputId = "choix_depa", label = "",multiple=TRUE,
           choices = seq(1,95))
            )
            ), textOutput("answ")
            ),
selectizeInput
按钮允许用户在
序列(1,95)
中选择一个或多个县

在server.R(最重要的)上,我有:

用户要在地图上绘制的变量选择在
input$choix\u var\u pop
中。 在此之后,我将在地图上创建所需的特定变量:

label_reac_pop = reactive({as.character(input$choix_var_pop)})
var_reac_pop = reactive({dico$Variable[dico$Label == label_reac_pop()]})
col_reac_pop = reactive({as.character(dico$Couleur[dico$Label == label_reac_pop()])})
type_reac_pop = reactive({as.character(dico$Type[dico$Label == label_reac_pop()])})
unite_reac_pop = reactive({ifelse(as.character(type_reac_pop()) == "Pct", " %", "")})
最后,我绘制了传单图:(为了清晰起见,我大大减少了下面的代码)

输出$Carte\u Pop%
addProviderTiles(“CartoDB.Positronolabels”)%>%
addProviderTiles(“CartoDB.PositronOnlyLabels”)%>%
setView(lng=2.468738900000062,纬度=49.19316,缩放=7)%>%
clearShapes()%>%
clearPopups()%>%
})
当然,我在ui.R文件中将这个输出称为$Carte_Pop,以绘制它。 好吧,那么,这一切的结果是什么? 正如我之前提到的,这个脚本在“单独”时工作,在没有输入$choix_depa时工作(我手动输入要合并到数组中的县,它们合并良好,地图绘制良好)。 但是当我在ShinyApp上使用我的3个脚本(global.R、ui.R和server.R)时,“map”的“new”值不会被“保存”

例如:如果我选择(在我的单独脚本中)合并并绘制第4和第87个县,效果很好(合并部分和绘制部分效果很好)

但是当我启动ShinyApp时,当我选择我想要的县(例如13和91),即使
等高线_IRIS
数据_BPC
与对应于13和91的数据很好地合并,所以我假设
观测事件中创建的
地图_WGS84
(输入$choix_depa…)
与13和91对应良好
,当我要求绘制特定变量时(在观察事件之后(输入$choix_var_pop)绘制的地图不是之前创建的地图,而是带有4和87的“旧地图”(在“单独脚本”上创建的地图)…在启动ShinyApp之前!)。但我100%确信在
观测事件(输入$choix_depa…)
中创建的地图是好的。但是,ShinyApp未“保存”此地图的“值”(他们使用地图的旧值…)

所以我的问题是:我应该怎么做才能绘制好的新地图(在应用程序内部创建),而不是旧的坏地图(在应用程序之前和外部创建…)

这个问题有点“复杂”,如果您有任何问题,请随时提问

谢谢!:)

摘要:当您想使某些输出依赖于其他表达式时,应该使用eventReactive,或者将对象存储在reactiveValues中,然后从observeEvent更新该对象

好的,我已经阅读了你的问题,以下是我的想法:


map_WGS84是什么类型的对象?这仅在它是一个反应值时才起作用。否则您不会覆盖全局变量,而只是覆盖函数observeEvent中的局部变量。一旦observeEvent完成,全局map_WGS84就不会更改。我想这就是问题所在



最好的选择可能是将其设置为事件反应式,而不是observeEvent,因为您希望它生成可在其他地方使用的输出。另一个选择是将map_WGS84存储在反应值表达式中,并从您的observeEvent覆盖它。

谢谢Florian的帮助,我将精确描述我是如何成功的:

 output$answ<- renderText({
      paste("Choix départements:", input$choix_depa)
  })

 observeEvent(input$choix_depa, {
      choice=input$choix_depa
  })
  map_reactive <- eventReactive(input$choix_depa,{
     ... merging and creating contours_IRIS (shape file) 
     and data_BPC given input$choix_depa ...

     map_WGS84 = spTransform(
        merge(contours_IRIS, data_BPC, by.x = 'CODE_IRIS', by.y = 'IRIS'),
        CRS("+init=epsg:4326")
        )

     list(map = map_WGS84) 
   })

observeEvent(input$choix_var_pop, {XXXXXXXXX})

... defining variables... 

output$Carte_Pop <- renderLeaflet({
      compulsive = map_reactive()$map
      label = label_reac_pop() 
      var = var_reac_pop() 
      col = col_reac_pop() 
      type = type_reac_pop() 
      unite = unite_reac_pop() 
      values_var = compulsive@data[,var] 

      leaflet(compulsive) %>%

  })
输出$answ
observeEvent(input$choix_var_pop, {XXXXXXXXX})
label_reac_pop = reactive({as.character(input$choix_var_pop)})
var_reac_pop = reactive({dico$Variable[dico$Label == label_reac_pop()]})
col_reac_pop = reactive({as.character(dico$Couleur[dico$Label == label_reac_pop()])})
type_reac_pop = reactive({as.character(dico$Type[dico$Label == label_reac_pop()])})
unite_reac_pop = reactive({ifelse(as.character(type_reac_pop()) == "Pct", " %", "")})
output$Carte_Pop <- renderLeaflet({

      label = label_reac_pop() 
      var = var_reac_pop() 
      col = col_reac_pop() 
      type = type_reac_pop() 
      unite = unite_reac_pop() 
      values_var = map_WGS84@data[,var] 

      leaflet(map_WGS84) %>%
      addProviderTiles("CartoDB.PositronNoLabels") %>% 
      addProviderTiles("CartoDB.PositronOnlyLabels") %>% 
      setView(lng = 2.468738900000062, lat = 49.19316, zoom = 7)  %>% 
      clearShapes() %>%                       
      clearPopups() %>% 
  })
ObserveEvent(input$choix_depa, {
   output$answ<- renderText({
      paste("You choose", input$choix_depa)
    })
   output$answ<- renderText({
      paste("You choose", input$choix_depa)
    })

ObserveEvent(input$choix_depa, {
....
map_WGS84 = spTransform(
  merge(contours_IRIS, data_BPC, by.x = 'CODE_IRIS', by.y = 'IRIS'),
  CRS("+init=epsg:4326")
) 
 output$answ<- renderText({
      paste("Choix départements:", input$choix_depa)
  })

 observeEvent(input$choix_depa, {
      choice=input$choix_depa
  })
  map_reactive <- eventReactive(input$choix_depa,{
     ... merging and creating contours_IRIS (shape file) 
     and data_BPC given input$choix_depa ...

     map_WGS84 = spTransform(
        merge(contours_IRIS, data_BPC, by.x = 'CODE_IRIS', by.y = 'IRIS'),
        CRS("+init=epsg:4326")
        )

     list(map = map_WGS84) 
   })

observeEvent(input$choix_var_pop, {XXXXXXXXX})

... defining variables... 

output$Carte_Pop <- renderLeaflet({
      compulsive = map_reactive()$map
      label = label_reac_pop() 
      var = var_reac_pop() 
      col = col_reac_pop() 
      type = type_reac_pop() 
      unite = unite_reac_pop() 
      values_var = compulsive@data[,var] 

      leaflet(compulsive) %>%

  })