2016-09-03 2 views
1

나는 반짝이는 전단지를 작성하여 어디에서 왔는지 기록합니다. 좌표와 시간이 포함 된 데이터 집합이 있습니다. 내 반짝이는 애플 리케이션에는 2 개의 위젯이있다. 타임 라인을위한 슬라이더 바 (sliderbard), 내가 현재의 국가를 보여주는 드롭 다운 박스. 드롭 다운 상자의 국가 선택은 타임 라인 슬라이더 막대를 기반으로합니다. 예를 들어 : 2016 년 이전에지도의 모든 좌표가 A 나라에 있으면 드롭 다운 상자에는 드롭 다운 상자 (국가 A)에 하나의 옵션 만 표시됩니다. 2016-01-01 이후, 드롭 다운 상자에서 2로 증가한 나라의 수는 2 가지 옵션 (국가 A와 B)이 될 것이며 현재이 기능이 잘 작동합니다.R 리플릿 setview에서 selectinput의 동적 값 사용

지금 내 반짝이는 앱을 더 개발하고 싶습니다. 원하는 기능은 드롭 다운 상자에 여러 국가가있는 경우에 앱에서 국가 중 하나를 선택하고 나라를 선택하면 전단지가 허용됩니다. 내가 선택한 나라에 초점을 맞출 것이다. 나는 setview()에서 다른 문제를 해결해야한다고 생각합니다.

global.R 
df <-read.csv("https://dl.dropbox.com/s/5w09dayyeav7hzy/Coordinatestest.csv", 
      header = T, 
      stringsAsFactors = F) 
df$Time <- as.Date(df$Time, "%m/%d/%Y") 
countriesSP <- getMap(resolution='low') 

ui.R 
library(devtools) 
library(leaflet) 
library(htmlwidgets) 
library(shiny) 
library(shinydashboard) 
library(sp) 
library(rworldmap) 
library(RCurl) 

header <- dashboardHeader(
    title = 'Shiny Memery' 
) 
body <- dashboardBody(
fluidRow(
    tabBox(
    tabPanel("My Map", leafletOutput("mymap",height = 550)), 
    width = 700 
)) 

) 


dashboardPage(
header, 
dashboardSidebar(

sliderInput('Timeline Value','Time line',min = min(df$Time),max = max(df$Time), value = min(df$Time)), 

selectInput("select_country", label = "Select Country", 
      choices = NULL, 
      selected = NULL) 

), 
body 
) 

server.R 

shinyServer(function(input, output, session) { 



output$mymap <- renderLeaflet({ 

df <- subset(df, df$Time <= input$`Timeline Value`) 

observe({ 

    pointsSP <- SpatialPoints(df[,c("lon", "lat")], proj4string=CRS(proj4string(countriesSP))) 

    indices <- over(pointsSP, countriesSP) 

    part_choices <- as.list(c("All", na.omit(unique(as.character(indices$ADMIN))))) 

    updateSelectInput(session, "select_country", choices=part_choices) 
}) 

    lng <- ifelse(input$select_country == "All", mean(df$lon), 0) 
    lat <- ifelse(input$select_country == "All", mean(df$lat), 0) 


    m <- leaflet(df) %>% 
    addTiles(
    #urlTemplate = "http://otile4.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.png" 
) %>% # Add default OpenStreetMap map tiles 
    #setView(mean(df$lon), mean(df$lat), zoom = 5) %>% 
    setView(lng, lat, zoom = 5) %>% 
    addMarkers(~lon, ~lat, 
      clusterOptions = markerClusterOptions()) 

}) 

}) 

복사 Rstudio에 스크립트를 붙여 넣기를 실행하십시오 :

그럼 나는 (부분적으로) 가능한 반짝 아래 스크립트를 생성 . 끝까지 시간대를 드래그하면 국가 옵션이 증가하지만 기본값은 항상 모두입니다. 이상적으로 하나의 국가를 선택하고 선택이 이 아닌 한 간단한 논리를 기반으로 볼 때 좌표는 setview() 좌표가 (0,0)이어야합니다 (나중에 동적 일 수 있습니다. 현재는 setview()을 원합니다. 지도의 초점을 변경합니다. 이 기능은 현재 실제로 작동하지 않습니다. 즉, 다른 국가를 선택하면지도의 초점이 (0,0)으로 변경되지만 다시 기본 초점 (mean(df$lon), mean(df$lat))으로 다시 변경되며 선택 사항은 으로 다시 변경됩니다. 모두도 있습니다.

그래서이 코드를 변경하여 어떻게 작동하게 할 수 있습니까?

이 예제에서는 제 상황에 대해 명확히 알기를 바랍니다. 많은

내가이 작업을 수행해야한다고 생각하는 방법 server.R 부분을 변경 한 도움

답변

1

에 대해 감사드립니다. 이것이 도움이되는지 알려주십시오. 도움을

server.R

 shinyServer(function(input, output, session) { 

      dfs <- reactive({ 
        tmp <- subset(df, df$Time <= input$`Timeline Value`) 
        tmp 
      }) 

      part_choices <- reactive({ 
        tmp <- dfs() 
        pointsSP <- SpatialPoints(tmp[,c("lon", "lat")], proj4string=CRS(proj4string(countriesSP))) 
        indices <- over(pointsSP, countriesSP) 
        as.list(c("All", na.omit(unique(as.character(indices$ADMIN))))) 
      }) 

      observe({ 
        updateSelectInput(session, "select_country", choices=part_choices()) 
      }) 
      output$mymap <- renderLeaflet({ 
        lng <- ifelse(input$select_country == "All", mean(df$lon), 0) 
        lat <- ifelse(input$select_country == "All", mean(df$lat), 0) 
        m <- leaflet(dfs()) %>% 
          addTiles(
            #urlTemplate = "http://otile4.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.png" 
          ) %>% # Add default OpenStreetMap map tiles 
          #setView(mean(dfs()$lon), mean(dfs()$lat), zoom = 5) %>% 
          setView(lng, lat, zoom = 5) %>% 
          addMarkers(~lon, ~lat, 
             clusterOptions = markerClusterOptions()) 

      }) 

    }) 
+0

안녕 @Valter 감사합니다. 정말 잘 작동합니다. 이런 방식으로 일이 어떻게 진행될 수 있는지 물어봐 주시겠습니까? 'renderLeaflet' 밖에서 일부 파트를 이동시키고'reactive' 함수를주는 것처럼 보입니다. 유사한 규칙이 적용되는 규칙은 무엇이며 비슷한 상황에 대해서는 어떤주의를 기울여야합니까? 감사합니다. – Lambo

+0

시간이 있다면 @joecheng의 반응 형 프로그래밍에 대한 Shiny Developer Conference의 두 개의 비디오를 보는 것이 좋습니다. 그것은 나를 위해 많은 포인트를 지웠다. [이것은 링크입니다] (https://www.rstudio.com/resources/webinars/shiny-developer-conference/) –

+0

감사합니다. 좋은 물건입니다. 나는 한번 살펴 보겠다. – Lambo