在R中,传单不反映纬度或经度? [英] in R, leaflet don't reflect the latitude nor longitude?

查看:13
本文介绍了在R中,传单不反映纬度或经度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图用我的数据框和过滤器创建这样的地图 one 我发现传单不反映任何长期.或纬度.因此没有弹出窗口显示.css

in an attempt to create map like this one with my data frame and filters I found leaflet doesn't reflect any of the long. or lat. accordingly no pop up to show.dataset & css

代码


  library(shiny)
  library(dplyr)
  library(leaflet)
  library(readr)
  CBdata <- read_csv("/cloud/project/TESTLEAF/www/cblnglt.csv")
  df0<- data.frame("cho"=c("No schooling completed","vocational","Bachelor","Post Graduate ","Single","Married","Divorced","others","male","female","under_20","21_30","31_39","31_40","41_50","51_59","over_60","Retired","Public_Employee","Private_Employee","Self_employeed","Unemployed","Others"),
                   "cat"=c("educ.","educ.","educ.","educ.","Relationship.Status","Relationship.Status","Relationship.Status","Relationship.Status","by_gender","by_gender","By_age","By_age","By_age","By_age","By_age","By_age","By_age","profession","profession","profession","profession","profession","profession"))

  CBdata2<-as.data.frame(CBdata)
  ui<-
    navbarPage("Mapping Dashboard",
               theme = "bootstrap.css",
               tabPanel("tbd",
                        div(class="outer",
                            leafletOutput("mymap", width = "100%", height = "100%"), #
                            absolutePanel(id = "controls", class = "panel panel-default", 
                                          fixed = TRUE,draggable = TRUE, top = "10%", left = "auto", right = 20, bottom = "auto",
                                          width = 330, height = "auto", cursor = "move",br(),
                                          selectInput("region", "Which governorate?", unique(CBdata$by_gov), selected = "Mumbai"),
                                          selectInput("variable", "Filter by?", unique(df0$cat),         selected = "by_gender"),
                                          selectInput("sndvariable","sub filter",choices =NULL),
                                          selectInput("indicator","select Indicator",c("xyz","abc"),
                                                      selected = "abc"),
                                          plotOutput("bar", height = "250px"),
                            ))),
               tabPanel("By Product"))

  server<-function(input,output,session){
    observe({
      TX<-df0%>%filter(cat==input$variable)%>%select(cho)
      updateSelectInput(session,"sndvariable","sub filter",choices = unique(TX))
    })
    CBD1<-reactive({
      #browser
      CBdata2%>%
        filter(input$variable==input$sndvariable,by_gov==input$region)%>%
        group_by(lng,lat,input$variable,by_gov,input$indicator)%>%
        arrange(desc(input$indicator)) %>%
        select(lng,lat,input$variable,by_gov,input$indicator)
      })
      CBD2<-reactive({
        #browser
        summarize(CBD1(),summation=sum(as.integer(input$indicator)))
            })
      CBD3<-reactive({as.data.frame(CBD2())
      })
    output$mymap<-renderLeaflet({
      CBD3()%>%leaflet()%>%
        addProviderTiles(provider = "CartoDB.DarkMatter")%>%
        addCircleMarkers(lng=~lng,lat=~lat)
    })
      }
  shinyApp(ui,server)

附上我在我的应用程序中使用的 css,以及所有

attahced the css that i used in my app, and all data required. if i removed the lng=~lng and inserted the real value it works fine but the issue arise when we use the dataframe.

提前致谢

推荐答案

你的问题是你的输入例如.input$variable 返回文本.但是您希望 dplyr 函数(例如 filter)使用这些输入,就好像它们是数据集中的变量一样,而不是文本.

Your problem is that your inputs eg. input$variable return text. But you want the dplyr functions such as filter to use those inputs as if they were variables in your dataset, rather than text.

举个例子:

# this fails because input_filter is a character vector and not a variable
my_filt_wrong <- function(data, input_filter, cond){
  data %>% 
    filter(input_filter == cond)
}

my_filt_wrong(iris, 'Species', 'setosa')
#[1] Sepal.Length Sepal.Width  Petal.Length Petal.Width  Species     
#<0 rows> (or 0-length row.names)

# this works because we tell r to evaluate the input text as a variable
my_filt <- function(data, input_filter, cond){
  data %>% 
    filter(!!sym(input_filter) == cond)
}

my_filt(iris, 'Species', 'setosa')

## A tibble: 50 x 5
#   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
# 1          5.1         3.5          1.4         0.2 setosa 
# 2          4.9         3            1.4         0.2 setosa 
# 3          4.7         3.2          1.3         0.2 setosa 
# 4          4.6         3.1          1.5         0.2 setosa 
# 5          5           3.6          1.4         0.2 setosa 

如果我们将您的 input$... 替换为 !!sym(input$...) 我们要引用数据集中的变量,则地图现在应该可以工作了.

If we replace your input$... with !!sym(input$...) where we want to refer to a variable in our dataset, the map should now work.

library(shiny)
library(dplyr)
library(leaflet)
library(readr)
CBdata <- read_csv("~/downloads/cblnglt.csv")
df0<- data.frame("cho"=c("No schooling completed","vocational","Bachelor","Post Graduate ","Single","Married","Divorced","others","male","female","under_20","21_30","31_39","31_40","41_50","51_59","over_60","Retired","Public_Employee","Private_Employee","Self_employeed","Unemployed","Others"),
                 "cat"=c("educ.","educ.","educ.","educ.","Relationship.Status","Relationship.Status","Relationship.Status","Relationship.Status","by_gender","by_gender","By_age","By_age","By_age","By_age","By_age","By_age","By_age","profession","profession","profession","profession","profession","profession"))

CBdata2<-as.data.frame(CBdata)
ui<-
  navbarPage("Mapping Dashboard",
             theme = "bootstrap.css",


             tabPanel("tbd",
                      div( class= 'outer',
                          leafletOutput("mymap", width = "100%", height = "100%"), 
                          absolutePanel(id = "controls", class = "panel panel-default", 
                                        fixed = TRUE,draggable = TRUE, top = "10%", left = "auto", right = 20, bottom = "auto",
                                        width = 330, height = "auto", cursor = "move",br(),
                                        selectInput("region", "Which governorate?", unique(CBdata$by_gov), selected = "Mumbai"),
                                        selectInput("variable", "Filter by?", unique(df0$cat),  selected = "by_gender"),
                                        selectInput("sndvariable","sub filter",choices =NULL),
                                        selectInput("indicator","select Indicator",c("xyz","abc"),
                                                    selected = "abc"),
                                        plotOutput("bar", height = "250px")
                          )
                          )),
             tabPanel("By Product",
                      tableOutput('mytable'))
  )

server<-function(input,output,session){
  observe({
    TX<-df0 %>% filter(cat==input$variable)%>%select(cho)
    updateSelectInput(session,"sndvariable","sub filter",choices = unique(TX))
  })

  CBD1<-reactive({
    #browser
    CBdata2 %>%
      filter(!!sym(input$variable)==input$sndvariable,by_gov==input$region) %>%
      group_by(lng,lat,!!sym(input$variable),by_gov,!!sym(input$indicator))%>%
      arrange(desc(!!sym(input$indicator))) 



  })

    CBD2<-reactive({
      #browser
      summarize(CBD1(), summation = sum(as.integer(!!sym(input$indicator))))
    })

    output$mytable <- renderTable(
      CBD2()
    )
    output$mymap<-renderLeaflet({
      CBD2() %>%
      leaflet()%>%
        addProviderTiles(provider = "CartoDB.DarkMatter")%>%
        addCircleMarkers(lng=~lng, lat=~lat)
    })
}

shinyApp(ui,server)

这篇关于在R中,传单不反映纬度或经度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆