rshinyshinydashboardr-leafletgoogleway

Is there a reason the output data can't be read by leaflet for creating a map in shiny


could someone tell me why leaflet doesn't read the data.frame properly created inside the server function of shiny? It doesn't show any map when executed, as soon as I try to addCirkleMarkers by the data I gathered before? It works perfectly, when I do it outside of shiny without input$type and just "Coworking" as search_string and keyword.

library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)
library(dplyr)
library(data.table)
library(leaflet.extras)
library(googleway)
library(tidyverse)


dashboard <- dashboardPage(
  skin = "blue",
  dashboardHeader(title = "Cluster-Dashboard"),
  dashboardSidebar(
    textInput("type", "Nutzung")),
  dashboardBody(
    fluidRow(box(width = 12, leaflet::leafletOutput(outputId = "mymap"))),
    fluidRow(box(width = 12, dataTableOutput(outputId = "Coworking")))
  ))

db_server <- function(input, output) {
  
  # gathering data and cleaning it
  output$Coworking <- renderDataTable({
    
    Coworking_2 <- google_places(
      search_string = input$type,
      location = c(52.52639577069,13.342801700749),
      radius = 0.5,
      keyword = input$type,
      simplify = TRUE,
      key = "Hidden")
    
    keeps <- c("name", "formatted_address", "geometry", "place_id")
    CoWorking_Clean = Coworking_2$results[keeps]
    CoWorking_Clean_2 <- CoWorking_Clean %>% 
      unnest(geometry) %>% 
      unnest(location) %>%
      subset(select = c("name", "formatted_address", "lat", "lng", "place_id")) %>%
      rename(address = "formatted_address")
    
    
  })
  
  output$mymap <- reactive({leaflet::renderLeaflet({Karte})})
  
  Karte <- reactive({
    leaflet (output$Coworking) %>% 
      addTiles() %>% 
      addProviderTiles(providers$CartoDB.PositronNoLabels) %>% 
      setView(13.412, 52.505, zoom = 11) %>% 
      addCircleMarkers(popup = ~as.character(name), label = ~as.character(name),fill =TRUE, fillColor ="#225287", stroke =FALSE, opacity = 0.5, group = "Companies",
                       labelOptions = labelOptions(noHide = T, textOnly = TRUE, direction = "bottom", style = list("color" = "black", "font-size" = "11px", "font-family" = "AkkuratStd", "font-style" = "Thin")))
  }
  )}



shinyApp (ui = dashboard , server = db_server)

the dataTableOutput "Coworking" clarifies that the dataTable was created and the information should be able to read by leaflet. there are long and lat inside. Im freaking out....

there is no error, the shiny app loads instant and everything works perfect except the leaflet map. There is even no error...


Solution

  • I spoted some errors in your server function, but i wasn't able to test if the code below solves the problem because of the api token.

    db_server <- function(input, output) {
      
      cowork <- reactive({
        
        Coworking_2 <- google_places(
          search_string = input$type,
          location = c(52.52639577069,13.342801700749),
          radius = 0.5,
          keyword = input$type,
          simplify = TRUE,
          key = "Hidden")
        
        keeps <- c("name", "formatted_address", "geometry", "place_id")
        CoWorking_Clean = Coworking_2$results[keeps]
        CoWorking_Clean_2 <- CoWorking_Clean %>% 
          unnest(geometry) %>% 
          unnest(location) %>%
          subset(select = c("name", "formatted_address", "lat", "lng", "place_id")) %>%
          rename(address = "formatted_address")
        return(CoWorking_Clean_2)
      })
      
      output$Coworking <- renderDataTable({
        cowork()
      })
      
      output$mymap <- renderLeaflet({
        leaflet (cowork()) %>% 
          addTiles() %>% 
          addProviderTiles(providers$CartoDB.PositronNoLabels) %>% 
          setView(13.412, 52.505, zoom = 11) %>% 
          addCircleMarkers(popup = ~as.character(name), label = ~as.character(name),fill =TRUE, fillColor ="#225287", stroke =FALSE, opacity = 0.5, group = "Companies",
                           labelOptions = labelOptions(noHide = T, textOnly = TRUE, direction = "bottom", style = list("color" = "black", "font-size" = "11px", "font-family" = "AkkuratStd", "font-style" = "Thin")))
      })
    }