當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安裝