1. First Shiny App
1.2 構(gòu)造
1.2.1 UI
- 用ui創(chuàng)建功能***Page()創(chuàng)建ui, 例如
fluidPage()
,fixedPage()
,fillPage()
ordashboardPage
1.2.2 Tags
用tags可以創(chuàng)建掛件?
ui <- fluidPage(
titlePanel("Basic Demo"),
h2("My favourite things"),
tags$ul(tags$li("Coding"),
tags$li("Cycling"),
tags$li("Cooking")),
p("This is a very basic demo."),
tags$img(
src = "https://debruine.github.io/shinyintro/images/logos/shinyintro.png",
width = "100px",
height = "100px"
)
)
1.2.3 Page Layout
可以使用sidebarLayout()函數(shù)將元素排列成sidebarPanel()和mainPanel()兩個面板窄绒。如果瀏覽器寬度太窄跋核,則側(cè)邊欄將顯示在主面板的上方倦春。
ui <- fluidPage(titlePanel("Basic Demo"),
sidebarLayout(sidebarPanel(
h2("My favourite things"),
tags$ul(tags$li("Coding"),
tags$li("Cycling"),
tags$li("Cooking"))
),
mainPanel(
p("This is a very basic demo."),
tags$img(
src = "https://debruine.github.io/shinyintro/images/logos/shinyintro.png",
width = "100px",
height = "100px"
)
)))
1.3 動態(tài)構(gòu)成
1.3.1 輸入
section 3 會細(xì)講
checkboxGroupInput(
inputId = "fav_things",
label = "What are your favourite things?",
choices = c("Coding", "Cycling", "Cooking")
)
inputId, label 是二件套,一般都少不了
1.3.2 輸出
section 4 會細(xì)講
server會管理output, 包括表格誊薄,圖形,文字
1.3.3 動作按鈕
還可以添加執(zhí)行按鈕
ui <- fluidPage(
titlePanel("This is a basic demo"),
sidebarLayout(
sidebarPanel(
h2("Chapter 1"),
tags$ul(
tags$li("Car"),
tags$li("Cooking"),
tags$li("Coke")
),
checkboxGroupInput(
inputId = "fav_things",
label = "What are your favourite things?",
choices = c("Coding", "Cycling", "Cooking")
),
actionButton(
inputId = "count_fav_things",
label = "Count",
icon = icon("calculator")
)
),
mainPanel(
p("This is a simple UI"),
tags$img(
src="<https://debruine.github.io/shinyintro/images/logos/shinyintro.png>",
width="100px",
height="100px"
),
textOutput(outputId = "n_fav_things")
)
)
)
1.4 反應(yīng)式
反應(yīng)式函數(shù)是僅在特定類型的輸入更改時才運(yùn)行的函數(shù)拓挥。在server()函數(shù)內(nèi)撑瞧,輸入對象(input)是所有輸入值的命名列表碴萧。例如,如果想知道名為“fav_things”的選擇輸入中選擇了哪些項(xiàng)目末购,則應(yīng)使用input$fav_things破喻。
在這里,只想計算選中了多少個項(xiàng)目盟榴。希望每當(dāng)單擊“count_fav_things”按鈕時都執(zhí)行此操作曹质,因此我們可以使用反應(yīng)式函數(shù)observeEvent()來實(shí)現(xiàn)。每次inputcount_fav_things更改時運(yùn)行,而不是在函數(shù)內(nèi)的任何輸入更改時運(yùn)行迅办。
server <- function(input, output) {
# count favourite things
observeEvent(input$count_fav_things, {
n <- length(input$fav_things)
count_text <- sprintf("You have %d favourite things", n)
})
}
現(xiàn)在我們想要在輸出“n_fav_things”中顯示這個文本玩般。我們需要使用與輸出函數(shù)配對的渲染函數(shù)。由于“n_fav_things”是使用textOutput()創(chuàng)建的礼饱,因此我們需要使用renderText()填充它。
server <- function(input, output) {
# count favourite things
observeEvent(input$count_fav_things, {
n <- length(input$fav_things)
count_text <- sprintf("You have %d favourite things", n)
output$n_fav_things <- renderText(count_text)
})
}
像編程中常見的一樣究驴,有許多方法可以實(shí)現(xiàn)相同的功能镊绪。這些方法有不同的優(yōu)缺點(diǎn),我們將在第5節(jié)中學(xué)到更多相關(guān)知識洒忧。這里提供另一種實(shí)現(xiàn)上述功能的代碼模式蝴韭。
server <- function(input, output) {
# update count_text on fav_things
count_text <- reactive({
input$count_fav_things # just here to trigger the reactive
fav_things <-
isolate(input$fav_things) # don't trigger on checks
n <- length(fav_things)
sprintf("You have %d favourite things", n)
})
# display count_text when it updates
output$n_fav_things <- renderText(count_text())
}
這個模式使用reactive()函數(shù)創(chuàng)建了一個名為count_text()的新函數(shù),該函數(shù)會在反應(yīng)式函數(shù)內(nèi)的任何輸入更改時更新其返回值熙侍。我們使用isolate()函數(shù)防止在用戶單擊復(fù)選框時count_text()函數(shù)發(fā)生變化榄鉴。
每當(dāng)count_text()函數(shù)返回的值發(fā)生更改時,這會觸發(fā)“n_fav_things”輸出的更新蛉抓。
這是第三種方法庆尘,使用eventReactive()函數(shù)避免了需要隔離inputcount_fav_things發(fā)生更改時運(yùn)行巷送。
server <- function(input, output) {
# update count_text on fav_things
count_text <- eventReactive(input$count_fav_things, {
fav_things <-input$fav_things
n <- length(fav_things)
sprintf("You have %d favourite things", n)
})
# display count_text when it updates
output$n_fav_things <- renderText(count_text())
}
練習(xí)
用observeEvent, reactive, eventReactive寫反應(yīng)式
## ui
ui <- fluidPage(titlePanel("Addition Demo"),
sidebarLayout(
sidebarPanel(
numericInput("n1", "First number", 0),
numericInput("n2", "Second number", 0),
actionButton("add", "Add Numbers")
),
mainPanel(textOutput(outputId = "n1_plus_n2"))
))
## Use observeEvent
server <- function(input, output) {
observeEvent(input$add,{
sum<-input$n1+input$n2
add_text<-sprintf("%d+%d=%d",input$n1,input$n2,sum)
output$n1_plus_n2<-renderText(add_text)
})
}
## Use reactive
server <- function(input,output) {
add_text<-reactive({
input$add
n1<-isolate(input$n1)
n2<-isolate(input$n2)
sprintf("%d+%d=%d",n1,n2,n1+n2)
})
output$n1_plus_n2<-renderText(add_text())
}
## Use eventReactive
server<- function(input,output) {
add_text<-eventReactive(input$add,{
sprintf("%d+%d=%d",
input$n1,
input$n2,
input$n1 + input$n2)
})
output$n1_plus_n2<-renderText(add_text())
}