Shiny 应用程序在显示传单地图后与服务器断开连接

问题描述 投票:0回答:1

我创建了一个简单的应用程序,选举官员可以在其中输入来到投票站的选民的姓名

voters
以及每个候选人拥有的选票。此外,它还会渲染一张
leaflet
地图,显示
ward
以及该选区所在的子县或县。
submit
按钮是一个
eventReactive
,单击它后,用户可以在三个选项卡中的任意一个中看到地图、表格结果及其名称。该应用程序在 rstudio 中运行良好,但在shinyapps.io 服务器中除外。在后者中,显示地图后,它会与服务器断开连接。整个 r 脚本可从 here 获得。

Disconnected from the server.
Reload

这是该应用程序的完整脚本。

# the web map should show the total no. of voters in each constituency.

library(shiny)
library(htmltools)
library(dplyr)
library(tools)
library(leaflet)
library(rgeos)
library(rgdal)
library(sp)
library(sf)
library(DT)
library(shinythemes)

load('wards.RData')
attach(wards)

wards_shapefile <- readOGR(dsn='wards_shapefile.shp')

# create ui that show the name of presiding officer
ui <- fluidPage(
  theme = shinytheme('flatly'),
  sidebarLayout(
    sidebarPanel(
      textInput(
        inputId = 'presiding_officer',
        label = 'Name of presiding officer',
        placeholder = 'ex. Tom Omondi'
      ),
      
      # option to select the ward
      selectizeInput(
        inputId = 'selected_wards',
        label = 'Choose the ward you are presiding over',
        choices = c(wards$ward)
      ),
      
      # show the total number of voters who came to vote
      numericInput(
        inputId = 'total_voters',
        label = 'Total voters who came to vote',
        value = 0,
        min = 0,
        max = NA
      ),
      
      # show the results of the five different presidential candidates
      
      numericInput(
        inputId = 'votes_david',
        label = 'Votes for David Waihiga Mwaure',
        value = 0,
        min = 0,
        max = NA
      ),
      
    
      numericInput(
        inputId = 'votes_george',
        label = 'Votes for George Wajackoyah',
        value = 0,
        min = 0,
        max = NA
      ),
      
      
      numericInput(
        inputId = 'votes_raila',
        label = 'Votes for Raila Odinga',
        value = 0,
        min = 0,
        max = NA
      ),
      
      
      numericInput(
        inputId = 'votes_reuben',
        label = 'Votes for Reuben Kigame',
        value = 0,
        min = 0,
        max = NA
      ),
      
     
      numericInput(
        inputId = 'votes_william',
        label = 'Votes for William Ruto',
        value = 0,
        min = 0,
        max = NA
      ),
      
      # put the submit button
      actionButton(inputId = "submit",
                   label = "Submit")
    ),
    
    ## mainPanel with tabls
    mainPanel(
      tabsetPanel(
        tabPanel("Map", leafletOutput(outputId = 'wards_map', width = '100%', height = 400)),
        tabPanel("Reactive table", DTOutput(outputId = 'table_react')),
        tabPanel("Officer's name", uiOutput(outputId = 'officer_name'))
      )
    )
  )
)

# define the server function 

server <- function(input, output, session){
  
  # in server
  server <- function(input, output, session) {
    
    updateSelectizeInput(session, 'selected_wards', choices =  c(wards$ward), server = TRUE)
  }
  
  table_results <- eventReactive(input$submit, {
    req(input$selected_wards)
    input$total_voters
    input$votes_david
    input$votes_george
    input$votes_raila
    input$votes_reuben
    input$votes_william
    
    data.frame(ward = input$selected_wards, voters = input$total_voters, david_waih = input$votes_david, 
               george_waj = input$votes_george, raila_odin = input$votes_raila, reuben_kig = input$votes_reuben,
               william_ru = input$votes_william)
  })
  
  
  presiding_officer_name <- eventReactive(input$submit, {
    input$presiding_officer
    
  })
  
  factpal <- colorFactor(palette = rainbow(47), unique(wards_shapefile@data$county))
  
  map <- leaflet() %>%
    addTiles() %>%
    addPolygons(data = wards_shapefile, stroke = T, weight = 0.5,
                fillColor = ~factpal(wards_shapefile@data$county), 
                fillOpacity = 0.2, popup = paste0("County: ", wards_shapefile$county, "<br>",
                                                  "Sub_county: ", wards_shapefile$subcounty, "<br>",
                                                  "Wards: ", wards_shapefile$ward))
  
  map_new <- reactive({
    
    if(input$submit) return(map)
  })
  
  output$wards_map <- renderLeaflet(map_new())
  
  br()
  br()
  
  # create a reactive table showing total no. of voters for each ward, and results for each of the five 
  # presidential candidates
  output$table_react <- renderDT(table_results())
  
  br()
  
  output$officer_name <- renderUI({HTML(paste0("Signed by Presiding officer name: ", "<br>",
                                               presiding_officer_name()))})
}


# create the shiny app object
shinyApp(ui, server)

shapefile

wards_shapefile
很重,包含表格的
wards.RData
也很重,但我认为如果一切都正确完成,这不会影响应用程序。如果有任何帮助使应用程序在部署时成功,它将有所帮助。

The app disconnected from server, bottom right

Rstudio日志------------------------

runApp('election_app_14')
Warning: package ‘shinythemes’ was built under R version 4.1.3
The following objects are masked from new_wards (pos = 4):

    county, cuid, david_waih, george_waj, gid, pop2009, raila_odin, reuben_kig, scuid,
    subcounty, uid, voters, ward, william_ru

The following objects are masked from new_wards (pos = 5):

    county, cuid, david_waih, george_waj, gid, pop2009, raila_odin, reuben_kig, scuid,
    subcounty, uid, voters, ward, william_ru

The following objects are masked from new_wards (pos = 6):

    county, cuid, david_waih, george_waj, gid, pop2009, raila_odin, reuben_kig, scuid,
    subcounty, uid, voters, ward, william_ru

The following objects are masked from new_wards (pos = 7):

    county, cuid, david_waih, george_waj, gid, pop2009, raila_odin, reuben_kig, scuid,
    subcounty, uid, voters, ward, william_ru

The following objects are masked from new_wards (pos = 9):

    county, cuid, david_waih, george_waj, gid, pop2009, raila_odin, reuben_kig, scuid,
    subcounty, uid, voters, ward, william_ru

The following objects are masked from new_wards (pos = 10):

    county, cuid, david_waih, george_waj, gid, pop2009, raila_odin, reuben_kig, scuid,
    subcounty, uid, voters, ward, william_ru

OGR data source with driver: ESRI Shapefile 
Source: "E:\Documents\R Studio is here\R Studio files projects\all_new_census\election_app_14\wards_shapefile.shp", layer: "wards_shapefile"
with 1450 features
It has 14 fields
Integer64 fields read as strings:  gid Voters david_waih george_waj raila_odin reuben_kig william_ru 
Warning: The select input "selected_wards" contains a large number of options; consider using server-side selectize for massively improved performance. See the Details section of the ?selectizeInput help topic.

Listening on http://127.0.0.1:4882
r shiny r-leaflet shinyapps
1个回答
1
投票

我找到了!!!秘密在于将超过 40MB 的庞大

ward_shapefile
减小到更小的大小。这是使用
rmapshaper
包完成的。 安装后,使用
ms_simplify
并在
keep
参数中输入一个非常低的数字。就我而言,我输入了 0.03。为了不让线条过于简化(假设可能会导致更多问题),请将
True
参数设置为
keep_shapes
以保留拓扑。
new_shapefile2 <- ms_simplify(input = new_shapefile, keep = 0.03, keep_shapes = T)

© www.soinside.com 2019 - 2024. All rights reserved.