2018-10-25
閱讀量:
920
R里的shiny包學(xué)習(xí)--shiny text
Shiny Text這個應(yīng)用程序展示的是直接打印R對象,以及用HTML表格展示數(shù)據(jù)框。要運行例子程序,只需鍵入:
> library(shiny)
> runExample("02_text")
前面那個例子里用一個滑動條來輸入數(shù)值,并且輸出圖形。而這個例子更進(jìn)了一步:有兩個輸入,以及兩種類型的文本輸出。
如果你改變觀測個數(shù), 將會發(fā)現(xiàn)Shiny應(yīng)用程序的一大特性:輸入和輸出是結(jié)合在一起的,并且“實時”更新運算結(jié)果(就像Excel一樣)。 在這個例子中,當(dāng)觀測個數(shù)發(fā)生變化時,只有表格更新,而不需要重新加載整個頁面。
下面是用戶界面定義的代碼。請注意,sidebarPanel
和mainPanel
的函數(shù)調(diào)用中有兩個參數(shù)(對應(yīng)于兩個輸入和兩個輸出)
library(shiny)
# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Shiny Text"),
# Sidebar with controls to select a dataset and specify the number
# of observations to view
sidebarPanel(
selectInput("dataset",
"Choose a dataset:",
choices
=
c("rock",
"pressure",
"cars")),
numericInput("obs",
"Number of observations to view:",
10)
),
# Show a summary of the dataset and an HTML table with the requested
# number of observations
mainPanel(
verbatimTextOutput("summary"),
tableOutput("view")
)
))
服務(wù)端的程序要稍微復(fù)雜一點?,F(xiàn)在,我們創(chuàng)建:
- 一個反應(yīng)性表達(dá)式來返回用戶選擇的相應(yīng)數(shù)據(jù)集。
- 還有兩個渲染表達(dá)式(rendering expressions,分別是
renderPrint
和renderTable
),以返回output$summary
的output$view
的值。
這些表達(dá)式和第一個例子中的 renderPlot
運作方式類似:通過聲明渲染表達(dá)式,你也就告訴了shiny,一旦渲染表達(dá)式所依賴的值(在這里例子中是兩個用戶輸入值的任意一個:input$dataset
或 input$n
)發(fā)生改變,表達(dá)式就會執(zhí)行。。
library(shiny)
library(datasets)
# Define server logic required to summarize and view the selected dataset
shinyServer(function(input,
output)
{
# Return the requested dataset
datasetInput
<-
reactive({
switch(input$dataset,
"rock"
=
rock,
"pressure"
=
pressure,
"cars"
=
cars)
})
# Generate a summary of the dataset
output$summary
<-
renderPrint({
dataset
<-
datasetInput()
summary(dataset)
})
# Show the first "n" observations
output$view
<-
renderTable({
head(datasetInput(),
n
=
input$obs)
})
})






評論(0)


暫無數(shù)據(jù)
推薦帖子
0條評論
0條評論
0條評論