在Shiny App中需要加載數(shù)據(jù)末盔、R腳本和包筑舅。
# #數(shù)據(jù)
counties.rds
是美國(guó)每個(gè)州的人口數(shù)據(jù)集,收集在R 包 UScensus2010
陨舱,也可以直接下載:here.
counties.rds數(shù)據(jù):
- 每個(gè)州的名字
- 每個(gè)州的總?cè)丝?/li>
- 每個(gè)州居民中白人翠拣、黑人、西班牙裔或亞裔的百分比
如果當(dāng)前要?jiǎng)?chuàng)建一個(gè)census-app Shiny app
-
在census-app下創(chuàng)建一個(gè)data文件夾游盲,放置數(shù)據(jù)
App folder with data subfolder
數(shù)據(jù)導(dǎo)入:
counties <- readRDS("census-app/data/counties.rds")
head(counties)
name total.pop white black hispanic asian
1 alabama,autauga 54571 77.2 19.3 2.4 0.9
2 alabama,baldwin 182265 83.5 10.9 4.4 0.7
3 alabama,barbour 27457 46.8 47.8 5.1 0.4
4 alabama,bibb 22915 75.0 22.9 1.8 0.1
5 alabama,blount 57322 88.9 2.5 8.1 0.2
6 alabama,bullock 10914 21.9 71.0 7.1 0.2
#helpers.R
helpers.R可以繪制一個(gè)人口分布地圖误墓,使用顏色展示人口的變化。
# Note: percent map is designed to work with the counties data set
# It may not work correctly with other data sets if their row order does
# not exactly match the order in which the maps package plots counties
percent_map <- function(var, color, legend.title, min = 0, max = 100) {
# generate vector of fill colors for map
shades <- colorRampPalette(c("white", color))(100)
# constrain gradient to percents that occur between min and max
var <- pmax(var, min)
var <- pmin(var, max)
percents <- as.integer(cut(var, 100,
include.lowest = TRUE, ordered = TRUE))
fills <- shades[percents]
# plot choropleth map
map("county", fill = TRUE, col = fills,
resolution = 0, lty = 0, projection = "polyconic",
myborder = 0, mar = c(0,0,0,0))
# overlay state borders
map("state", col = "white", fill = FALSE, add = TRUE,
lty = 1, lwd = 1, projection = "polyconic",
myborder = 0, mar = c(0,0,0,0))
# add a legend
inc <- (max - min) / 4
legend.text <- c(paste0(min, " % or less"),
paste0(min + inc, " %"),
paste0(min + 2 * inc, " %"),
paste0(min + 3 * inc, " %"),
paste0(max, " % or more"))
legend("bottomleft",
legend = legend.text,
fill = shades[c(1, 25, 50, 75, 100)],
title = legend.title)
}
helpers.R下載:here
helpers.R中需要調(diào)用 maps
和mapproj
R 包益缎。
install.packages(c("maps", "mapproj"))
helpers.R中有一個(gè)percent_map函數(shù)谜慌,參數(shù)如下:
Argument | Input |
---|---|
var |
a column vector from the counties.rds dataset |
color |
any character string you see in the output of colors()
|
legend.title |
A character string to use as the title of the plot’s legend |
max |
A parameter for controlling shade range (defaults to 100) |
min |
A parameter for controlling shade range (defaults to 0) |
library(maps)
library(mapproj)
source("census-app/helpers.R")
counties <- readRDS("census-app/data/counties.rds")
percent_map(counties$white, "darkgreen", "% White")
#加載文件和文件路徑
- 在上面的代碼中,首先導(dǎo)入了需要的r包:
library(maps)
library(mapproj)
調(diào)用helpers.R
source("census-app/helpers.R")
- 導(dǎo)入數(shù)據(jù):
counties <- readRDS("census-app/data/counties.rds")
percent_map(counties$white, "darkgreen", "% White")
注:在運(yùn)行server.R莺奔,默認(rèn)的工作路徑是server.R保存的位置欣范,所以上面運(yùn)行source("helpers.R")
也可以
#執(zhí)行
第一次調(diào)用runApp時(shí),Shiny會(huì)運(yùn)行整個(gè)腳本令哟。
每當(dāng)新的用戶(hù)訪(fǎng)問(wèn)應(yīng)用程序,Shiny 就會(huì)運(yùn)行server一次恼琼,為每個(gè)用戶(hù)構(gòu)建一組不同的響應(yīng)對(duì)象。
當(dāng)用戶(hù)使用工具交互并更改值時(shí)屏富,Shiny將會(huì)重新運(yùn)行分配給每個(gè)反應(yīng)對(duì)象的R表達(dá)式晴竞,這些反應(yīng)對(duì)象依賴(lài)于值被改變的工具。如果用戶(hù)非澈莅耄活躍噩死,這些表達(dá)式可能會(huì)在一秒鐘內(nèi)多次重新運(yùn)行。
程序運(yùn)行的額規(guī)律:
- 當(dāng)啟動(dòng)應(yīng)用時(shí)神年,shinyApp函數(shù)會(huì)運(yùn)行一次
- 每當(dāng)用戶(hù)訪(fǎng)問(wèn)應(yīng)用程序時(shí)已维,server就會(huì)運(yùn)行一次
- render*函數(shù)內(nèi)的R表達(dá)式會(huì)運(yùn)行很多次。每當(dāng)用戶(hù)更改小部件的值時(shí)已日,Shiny就會(huì)運(yùn)行它們一次衣摩。
這些信息對(duì)構(gòu)建程序有很大的幫助:
- 運(yùn)行R腳本、加載庫(kù)和讀取數(shù)據(jù)集在app.R中的位置應(yīng)該是在server外捂敌,Shiny將只運(yùn)行這段代碼一次艾扮。
- 定義與用戶(hù)特定的對(duì)象在server中,應(yīng)該在render*外占婉。
- render中代碼運(yùn)行次數(shù)最多泡嘴,shiny app每一次改變都會(huì)運(yùn)行一次。通常應(yīng)該避免在render函數(shù)中放置不需要的代碼逆济。這樣做會(huì)減慢整個(gè)應(yīng)用程序的速度酌予。
#census-app展示
# Load packages ----
library(shiny)
library(maps)
library(mapproj)
# Load data ----
counties <- readRDS("data/counties.rds")
# Source helper functions -----
source("helpers.R")
# User interface ----
ui <- fluidPage(
titlePanel("censusVis"),
sidebarLayout(
sidebarPanel(
helpText("Create demographic maps with
information from the 2010 US Census."),
selectInput("var",
label = "Choose a variable to display",
choices = c("Percent White", "Percent Black",
"Percent Hispanic", "Percent Asian"),
selected = "Percent White"),
sliderInput("range",
label = "Range of interest:",
min = 0, max = 100, value = c(0, 100))
),
mainPanel(plotOutput("map"))
)
)
# Server logic ----
server <- function(input, output) {
output$map <- renderPlot({
data <- switch(input$var,
"Percent White" = counties$white,
"Percent Black" = counties$black,
"Percent Hispanic" = counties$hispanic,
"Percent Asian" = counties$asian)
color <- switch(input$var,
"Percent White" = "darkgreen",
"Percent Black" = "black",
"Percent Hispanic" = "darkorange",
"Percent Asian" = "darkviolet")
legend <- switch(input$var,
"Percent White" = "% White",
"Percent Black" = "% Black",
"Percent Hispanic" = "% Hispanic",
"Percent Asian" = "% Asian")
percent_map(data, color, legend, input$range[1], input$range[2])
})
}
# Run app ----
shinyApp(ui, server)
#總結(jié):
- 放置app.R的位置將會(huì)是Shiny app的工作目錄
- server前的代碼只會(huì)在Shiny 中運(yùn)行一次
- server中的代碼會(huì)多次運(yùn)行磺箕,代碼太多會(huì)使程序運(yùn)行速度減慢
系列文章:
R shiny教程-1:一個(gè) Shiny app的基本組成部分
R shiny教程-2:布局用戶(hù)界面
R shiny教程-3:添加小部件到Shiny App
R shiny教程-4:Shiny app響應(yīng)式結(jié)果展示
Shiny Server安裝