輸入和輸出
在Sidebar上添加輸入
我們要使用R內(nèi)置的datasets包中的mtcars數(shù)據(jù)構(gòu)建程序, 允許用戶查看箱線圖來(lái)研究英里每加侖(miles-per-gallon,簡(jiǎn)稱MPG) 和其他三個(gè)變量(氣缸,變速器,齒輪)之間的關(guān)系。
我們想提供一種方式來(lái)選擇繪制MPG與哪個(gè)變量的圖形,也提供了個(gè)選項(xiàng),可選擇繪圖時(shí)包含或剔除異常值。為了完成這個(gè)目標(biāo),我們要往sidebar上加兩個(gè)元素,一個(gè)是selectInput
,用來(lái)指定變量,另一個(gè)是checkboxInput
,用來(lái)控制是否顯示異常值。添加這些元素后的用戶接口定義如下所示:
ui.R
library(shiny)
# Define UI for miles per gallon application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Miles Per Gallon"),
# Sidebar with controls to select the variable to plot against mpg
# and to specify whether outliers should be included
sidebarPanel(
selectInput("variable",
"Variable:",
list("Cylinders"
=
"cyl",
"Transmission"
=
"am",
"Gears"
=
"gear")),
checkboxInput("outliers",
"Show outliers",
FALSE)
),
mainPanel()
))
如果在做了這些修改之后你再運(yùn)行該程序,你會(huì)在sidebar看到兩個(gè)用戶輸入:

創(chuàng)建服務(wù)端腳本
我們需要定義程序的服務(wù)端腳本,用來(lái)接收輸入,并計(jì)算輸出。文件server.R如下所示,它說(shuō)明了下面幾個(gè)重要的概念:
- 使用
input
對(duì)象的組件來(lái)訪問(wèn)輸入,并通過(guò)向output
對(duì)象的組件賦值來(lái)生成輸出。 - 在啟動(dòng)的時(shí)候初始化的數(shù)據(jù)可以在應(yīng)用程序的整個(gè)生命周期中被訪問(wèn)
- 使用反應(yīng)表達(dá)式來(lái)計(jì)算被多個(gè)輸出共享的值
shiny服務(wù)端的腳本的基本任務(wù)是定義輸入和輸出之間的關(guān)系。腳本訪問(wèn)輸入值,然后計(jì)算,接著向輸出的組件賦以反應(yīng)表達(dá)式。
下面是全部服務(wù)端腳本的代碼(注釋更詳盡地講解了實(shí)現(xiàn)技巧):
server.R
library(shiny)
library(datasets)
# We tweak the "am" field to have nicer factor labels. Since this doesn't
# rely on any user inputs we can do this once at startup and then use the
# value throughout the lifetime of the application
mpgData
<-
mtcars
mpgData$am
<-
factor(mpgData$am,
labels
=
c("Automatic",
"Manual"))
# Define server logic required to plot various variables against mpg
shinyServer(function(input,
output)
{
# Compute the forumla text in a reactive expression since it is
# shared by the output$caption and output$mpgPlot expressions
formulaText
<-
reactive({
paste("mpg ~",
input$variable)
})
# Return the formula text for printing as a caption
output$caption
<-
renderText({
formulaText()
})
# Generate a plot of the requested variable against mpg and only
# include outliers if requested
output$mpgPlot
<-
renderPlot({
boxplot(as.formula(formulaText()),
data
=
mpgData,
outline
=
input$outliers)
})
})
shiny用renderText
和renderPlot
生成輸出(而不是直接賦值),這樣做讓程序成為反應(yīng)式的。這一層封裝返回特殊的表達(dá)式,只有當(dāng)其所依賴的值改變的時(shí)候才會(huì)重新執(zhí)行。這就使shiny在輸入值發(fā)生改變時(shí)自動(dòng)更新輸出。
展示輸出
服務(wù)端腳本給兩個(gè)輸出賦值:output$caption
和output$mpgPlot
。為了讓用戶接口能顯示輸出,我們需要在主UI面板上添加一些元素。
在下面修改后的用戶接口定義中,你可以看到,我們用h3元素添加了說(shuō)明文字,并用textOutput
函數(shù)添加了其中的文字,還調(diào)用了plotOutput
函數(shù)渲染了圖形。
ui.R
library(shiny)
# Define UI for miles per gallon application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Miles Per Gallon"),
# Sidebar with controls to select the variable to plot against mpg
# and to specify whether outliers should be included
sidebarPanel(
selectInput("variable",
"Variable:",
list("Cylinders"
=
"cyl",
"Transmission"
=
"am",
"Gears"
=
"gear")),
checkboxInput("outliers",
"Show outliers",
FALSE)
),
# Show the caption and plot of the requested variable against mpg
mainPanel(
h3(textOutput("caption")),
plotOutput("mpgPlot")
)
))
運(yùn)行應(yīng)用程序,就可以顯示它的最終形式,包括輸入和動(dòng)態(tài)更新的輸出:

現(xiàn)在我們完成了一個(gè)簡(jiǎn)單的程序,接下來(lái)可能想做一些修改








暫無(wú)數(shù)據(jù)