2017-12-24 10 views
0

라이브러리 (rworldmap)를 사용하여지도를 플롯하려는 경우 문제는 데이터를 선택하는 데 반응 기능을 사용할 수 없다는 것입니다. 사용자는 데이터를 선택 할 수 있습니다.? (예 : countryRegions 또는 countryExData) 여기Shiny R의지도 ("rworldmap")를 선택한 데이터로 채우기하는 방법

library(rworldmap) 
library(shiny) 
data(countryRegions) 
data(countryExData) 

runApp(list( 
    ui= fluidPage(
    selectInput("dataset", "Data", c('countryRegions', 'countryExData')), 
    plotOutput("Cart", height="560px", width="950px") 
), 

    server = function(input, output) { 
    datasetInput <- reactive({ 
     switch(input$dataset, 
      'countryRegions' = countryRegions, 
      'countryExData' = countryExData) }) 
    if (datasetInput()==countryRegions) { 
     sPDF <- joinCountryData2Map(datasetInput() 
            , joinCode = "ISO3" 
            , nameJoinColumn = "ISO3") 

     output$Cart <- renderPlot({ 
     mapParams <- mapPolys(sPDF, nameColumnToPlot="AVOIDnumeric", 
           mapRegion='world', 
           missingCountryCol='dark grey', numCats=10, 
           colourPalette=c('yellow','green','blue'), 
           addLegend=TRUE, 
           oceanCol='light blue') 
     mtext("[Grey Color: No Data Available]",side=1,line=-1) 
     })} 
    if (datasetInput()==countryExData){ 
     ##maping 
     sPDF <- joinCountryData2Map(datasetInput() 
            , joinCode = "ISO3" 
            , nameJoinColumn = "ISO3V10") 
     output$Cart <- renderPlot({ 
     mapParams <- mapPolys(sPDF, nameColumnToPlot="Population2005", 
           mapRegion='world', 
           missingCountryCol='dark grey', numCats=10, 
           colourPalette=c('yellow','green','blue'), 
           addLegend=TRUE, 
           oceanCol='light blue') 
     mtext("[Grey Color: No Data Available]",side=1,line=-1) 
     }) 
    } 
    } 
)) 

또한 코드의, 사용자가 일부 데이터의 열을 선택할 수 있도록 기회가

+0

좀 더 구체적으로 표현할 수 있습니까? 데이터 세트의 어떤 부분을 (반응 환경을 사용하여) 동적으로 서브 세트 할 수 없습니까? –

+0

나는 사용자가'selectInput ("dataset", "Data", c ('countryRegions', 'countryExData'))'를 넣은 다음 선택한 데이터에 따라 데이터를 선택할 수 있도록하고 싶다. 지도 플롯에서'reactive fonction datasetInput <- reactive ({ switch (input $ dataset, ...)'을 사용하지만 잘 사용하지 않고 있음을 분명히 알았습니다. 실수를 찾을 수 없습니다.) –

+0

마지막 질문은 아마도 여기에있는 다른 게시물에서 아마도 대답 할 수 있습니다. 찾아보기 바랍니다. 아무것도 없다면 작은 예제 (예 : 아래에 게시 한 것과 같은)를 만들고 워크 플로의 특정 변수에 대한 정보를 사용하는 방법. –

답변

0

어떻게 이러한 것들에 접근하는 것은 간단하게 시작한 다음 (작동하는) 뼈대를 기초로합니다. 두 개의 if 문이 있고 각각이 적절한 데이터 집합에 해당한다고 가정합니다. if 문이 입력 값을 확인하고 있는지 확인해야합니다. 데이터 세트를 할당했지만 ID 전체 데이터 세트를 확인하는 것은 간단하지 않으며 적어도이 경우 전체 워크 플로를 제어 할 수 있으므로 필요하지 않습니다.

예를 들어, 먼저 어떤 옵션을하면이 장치에 플롯을 선택했는지 적절한 출력이 당신을 확신

runApp(list( 
    ui= fluidPage(
    selectInput("dataset", "Data", c('countryRegions', 'countryExData'), selected = NULL), 
    plotOutput("Cart", height="560px", width="950px") 
), 

    server = function(input, output) { 
    output$Cart <- renderPlot({ 
     if (input$dataset == "countryRegions") { 
     message("Selected countryRegions") 
     plot(x = 1:10, y = rnorm(10), main = "Selected countryRegions") 

     } 
     if (input$dataset == "countryExData") { 
     message("selected countryExData") 
     plot(x = 1:10, y = rnorm(10), main = "Selected countryExData") 
     } 
    }) 
    } 
)) 

을 만들 수 있습니다. 는 다음과 같이 자신의 기능을 추가하기 시작 :

runApp(list( 
    ui = fluidPage(
    selectInput("dataset", "Data", c('countryRegions', 'countryExData'), selected = NULL), 
    plotOutput("Cart", height="560px", width="950px") 
), 

    server = function(input, output) { 
    output$Cart <- renderPlot({ 
     if (input$dataset == "countryRegions") { 
     sPDF <- joinCountryData2Map(countryRegions 
            , joinCode = "ISO3" 
            , nameJoinColumn = "ISO3") 

      mapParams <- mapPolys(sPDF, nameColumnToPlot = "AVOIDnumeric", 
           mapRegion = 'world', 
           missingCountryCol = 'dark grey', numCats = 10, 
           colourPalette = c('yellow', 'green', 'blue'), 
           addLegend = TRUE, 
           oceanCol = 'light blue') 
      mtext("[Grey Color: No Data Available]", side = 1, line = -1) 

     } 
     if (input$dataset == "countryExData") { 
     message("selected countryExData") 
     plot(x = 1:10, y = rnorm(10), main = "Selected countryExData") 
     } 

    }) 
    } 
)) 

이것은 당신의 input$dataset == "countryRegion"에 대한 맵에와 input$dataset == "countryExData"에 대한 일반 플롯을 표시합니다. 여기에서 그것으로 바이올린.

enter image description here