Creating Map from coordinates in data frame

Hello,

I am creating a shiny app where the data is being retrieved from an xlsx sheet.

In the excel worksheet, I have the name of the countries to map. Each country is divided into 50*50 grids.

Each grid has 2 coordinates (lat and long) which point to the centroid of the cubic grid.

I would like to create a shiny app where I can filter by country, month, and year and obtain the cubic grids for each country mapped over the base map.

I haven't succeeded in that yet.

I have tried using leaflet, ggplot2, and highcharter but I haven't been successful at finding a solution yet

Any help is highly appreciated

Thanks

What have you done so far?

Would it be better to join to the polygons of the country rather than the centroid?

if so? how would i be able to visualize the grids as cubes of certain dimensions?

so far i haven't been able to visualize anything, i only added the buttons for filtering etc...


I am hoping to be able to do something like this

What works? What doesn't? Share some code.

  dashboardHeader(title="Title"),
  dashboardSidebar(sidebarMenu(
    menuItem(" Data",tabName=" Data",icon =icon("dashboard"))
  )),
  dashboardBody(
    tabItem(tabName="Data",
            fluidRow(
              box(title="Please choose a country,month and year (Selecting Multiple countries is possible)", width=12,status="warning",solidHeader=TRUE,
                  selectInput("Country","Country",choices=countries_list, multiple=FALSE),
                  selectInput("Month","Month",choices=month_list,multiple=FALSE),
                  selectInput("Year","Year",choices=year_list,multiple=FALSE),
                  downloadButton("DownloadData","Download Data as csv"))
                    ),
            
            fluidRow(
              ##box(title = "Grids",width=12,leafletOutput("Leafplot", height = 250),status = "primary",solidHeader = TRUE)

              box(title="Grids",width=12,highchartOutput("Map",height=450),status="primary",solidHeader=TRUE)
            )
            )
  )
)```

```server<-function(input,output){

#Interactive filters & download button tab 
  data1<-reactive(
    {
      req(input$Country)
      req(input$Month)
      req(input$Year)
      data_filtered<-df_Main%>%filter(country %in% input$County)%>%filter(month_name %in% input$Month)%>%filter(year %in% input$Year)
    }
   
  )
  
  #head(df_Main)
  #browser()
  output$Map <- renderHighchart({
    req(input$Country)
    req(input$Month)
    req(input$Year)

    data_filtered<-df_Main%>%filter(country %in% input$County)%>%filter(month_name %in% input$Month)%>%filter(year %in% input$Year)
  
    highchart(type="map")%>%hc_add_series_map(worldgeojson,df=data_filtered,value="Value",joinBy=c("name","country"))
                                          
    }
  )

df_Main is the data frame that reads from excel where I have the coordinates for the grids
So far I have been able to visualize the select input buttons with a static image of the worldmap

It is not reproducible because we don't have your data.

What is wrong with it? What error messages do you get?

I do not get any error, I just get the world map blank empty and it does not change if I choose a certain country

Change this to Country (missing 'r').

map
what I am aiming to do but still don't know how to do it, is to have at any time i choose a certain country, to have the country map only with the cubic grids as an overlay maybe....

Ow! okay now what i am getting is the country selected in blue

This should be a reactive value. See:

https://shiny.rstudio.com/tutorial/written-tutorial/lesson6/

Edit: Actually maybe not. It isn't in the example here, but it normally would be. highcharter-shiny/app.R at master · jbkunst/highcharter-shiny · GitHub

I will give it a try

Big thanks, will keep u posted

1 Like