0
votes

I want to plot map using library(rworldmap), the problem is i can't use the reactive function for selecting the data. for example the user is allowed to select the data( for example:countryRegions or countryExData). Here's the code

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

Also, is there any chance to allow the users to select some data's columns ?

1
Can you be a bit more specific? What parts of the dataset are you not able to subset dynamically (using reactive environments)? - Roman Luštrik
i want to allow the user to choose the data , for that i put selectInput("dataset", "Data", c('countryRegions', 'countryExData')) and then depending on the selected data, i use it in a map plot, for that i use reactive fonction datasetInput <- reactive({ switch(input$dataset,... but it is obvious i am not using it well, i can't find the mistake. - Zina Jg
The last question is probably answered in other posts here on SO. Feel free to browse. If there's nothing, create a small example (such as the one I posted below) and how you would use the information on a certain variable in your workflow. - Roman Luštrik

1 Answers

0
votes

How I approach these things is start simple and then build upon the (working) skeleton. The approach below is made so that you have two if statements and each corresponds to the appropriate dataset. You have to make sure that the if statement is checking the input value. You assigned it dataset, but checking the identity whole dataset is not that simple and at least in this case, not necessary since you have control over the entire workflow.

For example, let's first create proper output

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

This convinces you that whatever option is selected it is plotted to the device. Then start adding your own functions like so:

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

    })
  }
))

This will display your worldmap for input$dataset == "countryRegion" and the generic plot for input$dataset == "countryExData". Fiddle with it from here on.

enter image description here