在上一节「R shiny基础」交互式入门, 我们实现了简单的用户交互。这一篇将会在之前的基础上,以对美国的人口普查结果进行可视化为例讲解如何通过从外部加载数据和脚本,实现更加复杂的操作。 加载数据和脚本在app.R的同级路径下创建'census-app'文件夹。之后进入该文件夹创建data文件夹,之后下载counties.rds数据到此文件夹中 用 readRDS 读取rds类型数据,数据内容为州名、人口数,不同种群人的比例。 conties <> readRDS('census-app/data/counties.rds')
下载helpers.R和app.R同级,该脚本用于从之前的数据中进行绘图。鉴于该脚本依赖于 maps 和 mapproj 这两个R包,你需要在使用之前进行安装。 用 source 加载R脚本,以及用library依赖包 library(map)
library(mapproj)
source('census-app/helpers.R')
脚本里面就一个 percent_map 函数,支持五个参数,分别为可视化列,颜色,标题,阴影大小下限,阴影大小上限。 percent_map(counties$white, 'darkgreen', '% White')
效果图如下: 
代码执行在shiny应用中,不同部分的代码的执行次数不同,规律如下: 因此,对于数据加载和代码加载的代码应该放在 server 函数外,仅仅将那些用户交互时才需要发生变更的代码放在render函数的表达式中。 结果现在,我们就可以构建出一个能够根据用户选择,然后展示结果的Shiny应用了。 版本一: library(shiny)
library(maps)
library(mapproj)
source('census-app/helpers.R')
counties <> readRDS('census-app/data/counties.rds')
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' = 'white',
'Percent Black' = 'black',
'Percent Hispanic' = 'hispanic',
'Percent Asian' = 'asian'),
selected = 'Percent White'),
sliderInput('range',
label = 'Range of interest:',
min = 0, max = 100, value = c(0, 100))
),
mainPanel(
plotOutput(outputId = 'selectVar')
)
)
)
# Define server logic required to draw a histogram
server <> function(input, output) {
output$selectVar <> renderPlot({
percent_map(counties[,input$var],
'darkgreen',
paste0('% ', input$var),
input$range[1],
input$range[2])
})
}
}
# Run the application
shinyApp(ui = ui, server = server)
版本二:在表达式中用switch函数,而不是在selectInput定义键值关系 ...
selectInput('var',
label = 'Choose a variable to display',
choices = c('Percent White', 'Percent Black',
'Percent Hispanic', 'Percent Asian'),
selected = 'Percent White')
...
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)
legend <> switch(input$var,
'Percent White' = '% White',
'Percent Black' = '% Black',
'Percent Hispanic' = '% Hispanic',
'Percent Asian' = '% Asian')
percent_map(data, 'darkgreen', legend, input$range[1], input$range[2])
})
}
版本三: 用 do.call 简化版本二的代码。 server <> function(input, output) {
output$map <> renderPlot({
args <> switch(input$var,
'Percent White' = list(counties$white, 'darkgreen', '% White'),
'Percent Black' = list(counties$black, 'black', '% Black'),
'Percent Hispanic' = list(counties$hispanic, 'darkorange', '% Hispanic'),
'Percent Asian' = list(counties$asian, 'darkviolet', '% Asian'))
args$min <> input$range[1]
args$max <> input$range[2]
do.call(percent_map, args)
})
}
总结这一节学习了 下一节主要学习构建模块化,快速运行的Shiny应用的一些技巧。 传送门Shiny基础教程:
|