当Shiny app用户修改内容之后,结果能够及时展示出来,这就是Shiny app的交互式响应。
想让Shiny app完成这样的工作需要两步:
- 在Shiny app界面添加一个R对象(ui.R中修改)
- 在server.R中编写R对象创建的代码
#Step 1: 添加一个R对象到UI
Shiny提供了一系列将R对象转换为用户界面输出的函数。每个函数创建一个特定类型的输出。
Output function | Creates |
---|---|
dataTableOutput |
DataTable |
htmlOutput |
raw HTML |
imageOutput |
image |
plotOutput |
plot |
tableOutput |
table |
textOutput |
text |
uiOutput |
raw HTML |
verbatimTextOutput |
text |
添加这些输出与添加HTML 元素类似;只需要将这些函数放置到ui中的sidebarPanel
或mainPanel
。
例子:使用textOutput将文本输出到主页面
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(
textOutput("selected_var")
)
)
)
注意,textOutput接受一个参数,字符串“selected_var”。每个 *Output
函数都需要一个参数:一个字符串,这个字符串将用作响应元素的名称。用户将不会看到这个名称。
#Step 2: 编写创建R对象的代码
在ui中放置一个函数会告诉Shiny在哪里显示对象。接下来,需要告诉Shiny如何构建这个对象。这部分工作在server.R中完成。
server中创建类似列表的output;每个R对象在列表中都有自己的名称。但是这个名称必须与ui.R中一致。
例如,server中output$selected_var匹配ui中 textOutput("selected_var")。
server <- function(input, output) {
output$selected_var <- renderText({
"You have selected this"
})
}
注:server中output中这些对象应该是render*函数的结果。这样才会使响应式展示结果。对于不同类型R对象的render函数:
render function | creates |
---|---|
renderDataTable |
DataTable |
renderImage |
images (saved as a link to a source file) |
renderPlot |
plots |
renderPrint |
any printed output |
renderTable |
data frame, matrix, other table like structures |
renderText |
character strings |
renderUI |
a Shiny tag object or HTML |
每个render*函数都有一个参数:一个由大括号{}包围的R表达式。表达式可以是一行简单的文本,也可以包含许多行代码。
要实现这个功能,表达式应该返回希望的对象(一段文本、一个图形、一个数据框架等等)。如果表达式没有返回对象,或者返回了错误类型的对象,则会报错。
#使用ui.R中小工具选择的值
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(
textOutput("selected_var")
)
)
)
server <- function(input, output) {
output$selected_var <- renderText({
"You have selected this"
})
}
shinyApp(ui=ui, server = server)
运行这段代码,主页面的"You have selected this"还不是响应式的。
但是在ui.R中定义了两个小工具,“var" 和"range",但是在server.R中,没有任何代码对他们进行操作。如果,在server.R中要对他们进行操作,他们对应的对象是input$var
和input$range
, input和output类似,都是server的参数,都类似列表,调用其中的对象使用$就可以。
新的代码:
server <- function(input, output) {
output$selected_var <- renderText({
paste("You have selected: ", input$var)
})
}
现在每次改变Shiny app上的variable,主页面显示的文字都会发生变化。这种实时变化是由renderText产生,在renderText调用了input$var,这是来自于ui.R的对象。
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(
textOutput("selected_var")
)
)
)
server <- function(input, output) {
output$selected_var <- renderText({
paste("You have selected: ", input$var)
})
}
shinyApp(ui=ui, server = server)
#总结
- 在ui中使用*Output函数来安排响应对象的位置
- 使用服务器中的render*函数告诉Shiny如何构建你的对象
- 在每个render*函数中用大括号{}包围产生R对象的表达式
- 在output列表中保存render*表达式,每个响应对象都有一个名字
- 通过在render*表达式中包含input值来创建反应性。
原文:
系列文章:
R shiny教程-1:一个 Shiny app的基本组成部分
R shiny教程-2:布局用户界面
R shiny教程-3:添加小部件到Shiny App
Shiny Server安装