其他
shiny 之 其它控件
零落成泥碾作尘
1引言
介绍其它控件。
2App
界面:
左侧为两个控件,选择控件 和数字控件:
选择控件:
数字输入控件:
动作按钮 点击刷新控件变化后的值:
功能:
可以选择不同的数据集,左侧则会出现统计信息,和表格信息,数字控制表格显示的行数。
3代码解析
ui 部分
library(shiny)
# Define UI for dataset viewer app ----
ui <- fluidPage(
# App title ----
titlePanel("More Widgets"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a dataset ----
selectInput("dataset", "Choose a dataset:",
choices = c("rock", "pressure", "cars")),
# Input: Specify the number of observations to view ----
numericInput("obs", "Number of observations to view:", 10),
# Include clarifying text ----
helpText("Note: while the data view will show only the specified",
"number of observations, the summary will still be based",
"on the full dataset."),
# Input: actionButton() to defer the rendering of output ----
# until the user explicitly clicks the button (rather than
# doing it immediately when inputs change). This is useful if
# the computations required to render output are inordinately
# time-consuming.
actionButton("update", "Update View")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Header + summary of distribution ----
h4("Summary"),
verbatimTextOutput("summary"),
# Output: Header + table of distribution ----
h4("Observations"),
tableOutput("view")
)
)
)
选择控件:
choices 参数跟上向量:
selectInput("dataset", "Choose a dataset:",
choices = c("rock", "pressure", "cars"))
数字控件:
直接给个数值即可:
numericInput("obs", "Number of observations to view:", 10)
文本控件:
输入提示文本内容:
helpText("Note: while the data view will show only the specified",
"number of observations, the summary will still be based",
"on the full dataset.")
动作按钮:
给 ID 和标签即可:
actionButton("update", "Update View")
mainPanel 的统计输出和表格输出:
verbatimTextOutput("summary")
tableOutput("view")
server 部分
# Define server logic to summarize and view selected dataset ----
server <- function(input, output) {
# Return the requested dataset ----
# Note that we use eventReactive() here, which depends on
# input$update (the action button), so that the output is only
# updated when the user clicks the button
datasetInput <- eventReactive(input$update, {
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
}, ignoreNULL = FALSE)
# Generate a summary of the dataset ----
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
# Show the first "n" observations ----
# The use of isolate() is necessary because we don't want the table
# to update whenever input$obs changes (only when the user clicks
# the action button)
output$view <- renderTable({
head(datasetInput(), n = isolate(input$obs))
})
}
shinyApp(ui, server)
结合动作按钮延迟输入响应:
datasetInput <- eventReactive(input$update, {})
里面使用 swith 函数来传入选择控件的参数,实现不同切换:
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
使用 datasetInput() 储存数据的格式给 summary 函数进行汇总统计, renderPrint 输出结果:
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
renderTable 输出表格,其中 使用 isolate 来隔离 obs 显示的数量,防止时时更新:
output$view <- renderTable({
head(datasetInput(), n = isolate(input$obs))
})
大家可以去试试吧!
在线网址:
https://shiny.rstudio.com/gallery/widgets.html
欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群
哦,数据代码已上传至QQ群,欢迎加入下载。
群二维码:
老俊俊微信:
知识星球:
所以今天你学习了吗?
欢迎小伙伴留言评论!
今天的分享就到这里了,敬请期待下一篇!
最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!
如果觉得对您帮助很大,赏杯快乐水喝喝吧!
往期回顾
◀跟着 NC (Nature Comm) 学画图: 箱线图嵌套并分面添加文本注释
◀跟着 NC (Nature Communications) 学画图: 火山图进阶
◀...