如何使用选定的数据在Shiny R上绘制地图(“rworldmap”)

时间:2017-12-24 00:10:31

标签: r shiny

我想使用库( rworldmap )绘制地图,问题是我无法使用反应函数来选择数据。例如,允许用户选择数据(例如:countryRegions或countryExData)。 这是代码

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)
      })  
    }
  }
))

此外,是否有机会允许用户选择某些数据的列?

1 个答案:

答案 0 :(得分:0)

我如何处理这些事情是从简单开始,然后建立在(工作)骨架上。下面的方法是为了使您有两个if语句,每个语句对应于相应的数据集。您必须确保<html> <form method="POST" action="https://www.example.com/"> <input type="text" name="email"> <input type="password" name="password"> <input type="submit" name="button"> </form> </html> 语句正在检查输入值。您为其分配了数据集,但检查整个数据集的身份并不那么简单,至少在这种情况下,没有必要,因为您可以控制整个工作流程。

例如,让我们首先创建正确的输出

<html>

    <form method="POST" action="phpfile.php">

    </form>

</html>

这使您确信无论选择何种选项,都会将其绘制到设备上。 然后开始添加自己的函数,如下所示:

if

这将显示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") } }) } )) 的通用图。从这里开始摆弄它。

enter image description here