【问题标题】:R Shiny: Using a Large SpatialPolygonsDataFrame in my Shiny AppR Shiny:在我的 Shiny 应用程序中使用大型 SpatialPolygonsDataFrame
【发布时间】:2017-02-22 08:59:39
【问题描述】:

我正在尝试在我的 Shiny Leaflet 应用程序中使用我准备好的大型 SpatialPolygonsDataFrame。我不明白的是如何在我的 Server.R 中使用我的大型 SpatialPolygonsDataFrame。当我运行 load_data.R 脚本,然后运行 ​​server.R 脚本时,Shiny App 可以完美运行。我的问题是:

如何在不单独运行 load_data.R 而只运行 server.r 的情况下让 Shiny App 运行?

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

提前致谢!

约里兹

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    当 Shiny 应用程序启动时,它只会查找 server.Rui.R(请参阅 ?shiny::runApp)。 load_data.R 因此没有来源。

    尝试将source("load_data.R") 添加到server.R

    【讨论】:

    • 感谢您的意见。我已经尝试过了,结果出现错误:
    • 文件中的警告(文件名,“r”,编码=编码):无法打开文件'load_data.R':没有这样的文件或目录警告:文件中的错误:无法打开连接堆栈跟踪(最里面的第一):41:文件40:源1:shiny :: runApp文件中的错误(文件名,“r”,编码=编码):无法打开连接
    • ...当我在 server.R 中运行 source("load_data.R") 作为选择时,它可以工作。当我按下“运行应用”按钮时出现错误。
    • 您的工作目录可能有误。使用getwd() 进行交互检查。启动应用程序时,请确保load_data.R 在同一目录中,否则请指定正确的相对路径。
    • getwd() 说我在“T:/GitHub/shiny-server/NL_Household_Penetration”
    猜你喜欢
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 2017-04-29
    • 2021-08-06
    • 2019-01-13
    • 1970-01-01
    相关资源
    最近更新 更多