2

I'm trying to use my prepared Large SpatialPolygonsDataFrame in my Shiny Leaflet App. What I don't understand is how to use my Large SpatialPolygonsDataFrame in my Server.R. When I run the load_data.R script and after that the server.R script, then the Shiny App works perfectly. My question to you is:

How can I get the Shiny App working without running seperately load_data.R, but just with running the server.r?

load_data.R:

    library(RSQLite)
    library(rgdal)
    library(dplyr)

    # Use the SQLite database
    my_sqdb = src_sqlite("NL_Household_Penetration/Data/dataset.sqlite")

    # Extract the main dataset out of the SQLite database
    df = data.frame(tbl(my_sqdb, sql("SELECT * FROM df")))

    # Extract the stores with their locations out of the SQLite database
    Winkels = data.frame(tbl(my_sqdb, sql("SELECT * FROM Winkels")))

    # Read the shape-data(polygons) into R
    shape <-readOGR("NL_Household_Penetration/PMA_Shape/Polygonen NL Postcodes 4PP.kml", "Polygonen NL Postcodes 4PP")

    # Combine the main dataset with the shape data to plot data into zipcode areas
    SalesMap <- merge(shape, df, by.x='Description', by.y='POSTCODE')

server.R:

#shiny
library(shiny)
library(shinydashboard)

#define color
library(RColorBrewer)
library(colorspace)

# leaflet map
library(leaflet)
library(htmlwidgets)
library(htmltools)


## Creating leaflet map
pal <- colorNumeric("Reds",SalesMap@data$SALES)

polygon_popup <- paste0("<strong>Postcode: </strong>", SalesMap$Description, "<br>",
                        "<strong>Waarden: </strong>", SalesMap$SALES)

pop = as.character(Winkels$WINKEL)

Icon <- makeIcon(
  iconUrl = "NL_Household_Penetration/Images/image.png",
  iconWidth = 100, iconHeight = 78
)

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

  output$mymap <- renderLeaflet({

    leaflet() %>% 
      addTiles(
        urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
        attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>'
      )  %>%


      addPolygons(data = SalesMap,
                  fillColor = ~pal(SalesMap@data$SALES),         
                  fillOpacity = 0.6,  ## how transparent do you want the polygon to be? 
                  popup = polygon_popup,
                  color = "black",       ## color of borders between districts
                  weight = 2.0) %>%

      addMarkers(Winkels$Lon, Winkels$Lat, popup=pop, icon=IKEAIcon)

  })
}

ui.R

 library(shiny)
 library(shinydashboard)
 library(leaflet)


    ui <- bootstrapPage(
      tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
      leafletOutput("mymap", width = "100%", height = "100%")
    )

Thanks in advance!

Yoorizz

Yoorizz
  • 217
  • 2
  • 12

1 Answers1

0

When the Shiny app is launched, it looks only for server.R and ui.R (see ?shiny::runApp). load_data.R is thus not sourced.

Try adding source("load_data.R") to server.R.

Aurèle
  • 12,545
  • 1
  • 31
  • 49
  • Thank you for your input. I already tried that with an error as result: – Yoorizz Oct 13 '16 at 17:40
  • Warning in file(filename, "r", encoding = encoding) : cannot open file 'load_data.R': No such file or directory Warning: Error in file: cannot open the connection Stack trace (innermost first): 41: file 40: source 1: shiny::runApp Error in file(filename, "r", encoding = encoding) : cannot open the connection – Yoorizz Oct 13 '16 at 17:41
  • ...And when I run source("load_data.R") as a selection in the server.R it works. I'm getting the error when I press the Run App button. – Yoorizz Oct 13 '16 at 17:46
  • Your working directory may be wrong. Check it interactively with `getwd()`. When launching the app, make sure `load_data.R` is in the same directory, or else specify the correct relative path to it. – Aurèle Oct 13 '16 at 17:54
  • getwd() is saying that I'm in "T:/GitHub/shiny-server/NL_Household_Penetration" – Yoorizz Oct 13 '16 at 17:58
  • Still an error, but a little differen: Warning: Error in : Path does not exist and create = FALSE Stack trace (innermost first): 44: src_sqlite 43: eval [load_data.R#6] 42: eval 41: withVisible 40: source 1: shiny::runApp Error : Path does not exist and create = FALSE – Yoorizz Oct 13 '16 at 17:59
  • Well, I see. The path to `dataset.sqlite` must be relative to `server.R` directory. If you want it to be relative to `load_data.R` directory, use `chdir = TRUE` in `source()`. See http://stackoverflow.com/questions/12048436/r-sourcing-files-using-a-relative-path – Aurèle Oct 13 '16 at 18:09
  • It worked! There is one last error that has to do with the merge in load_data.R: Warning: Error in serverFuncSource: server.R returned an object of unexpected type: character Stack trace (innermost first): 1: runApp Error in serverFuncSource() : server.R returned an object of unexpected type: character – Yoorizz Oct 13 '16 at 18:51
  • Also fixed this issue. Thanks for your help! – Yoorizz Oct 13 '16 at 18:57