拆分Shiny Leaflet App时遇到麻烦

时间:2017-01-31 06:49:12

标签: r leaflet shiny

我正在尝试将我的Shiny App拆分为shinyapps.io主机,但是当我拆分应用程序时,我丢失了数据。

当ui和服务器在一起时,它可以工作;但是在将它们分成两个单独的文件时,我正在丢失边栏。

ui.R

library(shiny)
library(leaflet)
library(RColorBrewer)
library(shiny)

shinyUI(
  bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(uiOutput("sorted.data"), top = 10, right = 10
                )
    )
  )

server.r

library(shiny)
library(leaflet)
library(RColorBrewer)
library(maps)

capwords <- function(s, strict = FALSE) {
  cap <- function(s) paste(toupper(substring(s, 1, 1)),
                           {s <- substring(s, 2); if(strict) tolower(s) else s},
                           sep = "", collapse = " " )
  sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s)))
}

# mapStates = map("state", fill = TRUE)
# data("us.cities")
map.cities(us.cities)
# leaflet(mapStates) %>% addTiles() %>% setView(lng = -97.00, lat = 38.00, zoom = 4)

# import and clean data
alumni <- read.csv("../Residency Match List 2010-2015 - Sheet1.csv", stringsAsFactors = F, header = T)
# convert city to lowercase then capitalize it, then paste it together with the state
# we're doing this to match the alumni data to the us.cities data
alumni$city.state <- paste(capwords(tolower(alumni$City)), alumni$State)

# merge the data by the city.state column we just created and the name column in the us.cities data
alumni.map.data <- merge(alumni, us.cities, by.x = "city.state", by.y = "name")

shinyServer(function(input, output, session) {

    # Reactive expression for the data subsetted to what the user selected
    filteredData <- reactive({
      alumni.map.data[alumni.map.data$Primary.Specialty == input$Primary.Specialty,]
    })

    output$sorted.data <- renderUI({sort(unique(alumni.map.data$Primary.Specialty))})

    output$map <- renderLeaflet({
      # Use leaflet() here, and only include aspects of the map that
      # won't need to change dynamically (at least, not unless the
      # entire map is being torn down and recreated).
      leaflet(alumni.map.data) %>% addTiles() %>%
        fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
    })

    # Incremental changes to the map (in this case, replacing the
    # circles when a new color is chosen) should be performed in
    # an observer. Each independent set of things that can change
    # should be managed in its own observer.
    observe({

      leafletProxy("map", data = filteredData()) %>%
        clearMarkers() %>% clearMarkerClusters() %>%
        addCircleMarkers(
          popup = ~as.character(paste(sep = "<br/>",
                                      paste0('<a href = ', #begin link format to search google
                                             "https://www.google.com/#q=",
                                             paste(First.Name,
                                                   Last.Name, "Tulane", sep = "+"), #search elements
                                             ">",
                                             paste(First.Name, Last.Name, sep = " "),
                                             '</a>'),  # end link format to search google
                                      paste("Class of", Class.Year, sep = " "),Program, 
                                      Primary.Specialty)),
          clusterOptions = markerClusterOptions()
        )
    })
  })

当我运行此代码时,地图会被渲染,但没有任何聚类/标记,我会收到以下错误:

Warning in charToRaw(enc2utf8(text)) :
  argument should be a character vector of length 1
all but the first element will be ignored
Assuming 'long' and 'lat' are longitude and latitude, respectively

我觉得我错过了一些简单的事情。谢谢!

1 个答案:

答案 0 :(得分:0)

我不是百分之百,因为我无法重新创建,所以我猜,但我会在

之前取出你server.r文件中的所有内容。
shinyServer(function(input, output, session){

将其放入同一目录中的global.r文件中。

同样适用于所有库调用,只需将它们全部放在global.r

相关问题