R Shiny动画滑块的地图

时间:2017-06-27 03:31:44

标签: r dictionary animation shiny

我是Shiny和编码的新手。我找到了一个使用等值线图(ichorophlet函数)来显示多年和美国各州的犯罪率的例子。我想使用美国的年度贫困率在Shiny中复制这张地图。我的问题是:1)如何在Shiny上加载地图? 2)如何让动画按钮工作?以下是我使用的R代码。任何想法如何解决这个问题?

ui.R

shinyUI(fluidPage(
  titlePanel("U.S. Poverty Rates"),

  # Sidebar with slider that demonstrates various years
  sidebarLayout(
    sidebarPanel(
      helpText("Create a poverty map."),    

      # Animation with custom interval (in ms) to control speed, plus looping
      sliderInput("animation", "Press Play:", 1980, 2015, 1, step=1, 
                  animate=animationOptions(interval=800, loop=TRUE))),

   # Show map summarizing the values entered
   mainPanel(
      plotOutput("map")
    )   
  )
))

server.R

# Load libraries
library(lattice)
library(plyr)
library(dplyr) 
library(readxl)
library(RColorBrewer)
library(rMaps) 
library(rjson)
library(rCharts)
library(shiny)

# Load data and helper files
data <- read_excel("data/hstpov21.xls", sheet = "Sheet1")
source("toJASON.R")
source("ichoropleth.R")

# Remove DC
datm <- subset(na.omit(data), 
               !(State %in% c("D.C.", "District of Columbia")))

# Discreticize poverty rates
datm2 <- transform(datm,
                   State = state.abb[match(as.character(State), state.name)],
                   fillKey = cut(Poverty, 
                                 quantile(Poverty, seq(0, 1, 1/5)), 
                                 labels = LETTERS[1:5]),
                   Year = as.numeric(substr(Year, 1, 4))
                   )

# Fill colors
fills = setNames(
        c(RColorBrewer::brewer.pal(5, 'YlOrRd'), 'white'),
        c(LETTERS[1:5], 'defaultFill')
        )

# Create Payload for DataMaps
dat2 <- dlply(na.omit(datm2), "Year", function(x){
        y = toJSONArray2(x, json = F)
        names(y) = lapply(y, '[[', 'State')
        return(y)
        })

# Define server logic for slider
shinyServer(
  function(input, output) {

    # Reactive expression to compose a data frame containing all of the values
      sliderValues <- reactive({

       # Compose data frame
         data.frame(
         Name = c("Animation"),
         Value = as.character(c(input$animation)), 
         stringsAsFactors=FALSE)
     })  

    # Show the values using a chorophlet map
      output$map <- renderPlot({
         sliderValues()

         ichoropleth(Poverty ~ State,
         data = datm2[,1:3],
         pal = 'PuRd',
         ncuts = 5,
         animate = 'Year',
         play = TRUE)

        }) 
 })

0 个答案:

没有答案