R手册(Communicate)--shiny

Shiny

Shiny是来自RStudio的软件包,它使得用R构建交互式Web应用程序变得非常简单。

Build an App

A Shiny app is a web page (UI) connected to a
computer running a live R session (Server)

library(shiny)
ui <- fluidPage(
 numericInput(inputId = "n",  
 "Sample size", value = 25),  
 plotOutput(outputId = "hist") 
 )
server <- function(input, output) {
 output$hist <- renderPlot({   
 hist(rnorm(input$n)) })    
 }
shinyApp(ui = ui, server = server)
  1. Define UI ----(支持HTML标签语法)
  • ui <- fluidPage()定义HTML用户界面
  • 添加input到ui ,用<type>Input系列函数
  • 添加output到ui,用<type>Output系列函数
  1. Define server logic
  • server <- function(input, output){}定义R对象和UI组件的连接方式
  • input$<inputId>output$<outputId>提供数据给UI组件
  • render<type>赋值给output$<id>输出给UI output 组件
  1. Run the app
  • combines ui and server
  • Run shinyApp(ui = ui, server = server)
  1. Save
    保存成一个app.R 或者拆分成 ui.Rserve.R(shinyApp不需要保存)

  2. Share your APP

  • 注册免费或者专业账户 http://shinyapps.io
  • 在RStudio IDE点击Publish 按钮或者运行代码:
    rsconnect::deployApp("<path to directory>")

Inputs

使用input$<inputId>获取input当前value

input说明
actionButton(inputId, label, icon,…)按钮
submitButton(text, icon)提交按钮
radioButtons(inputId, label,choices, selected, inline)单选按钮
checkboxGroupInput(inputId, label,choices, selected, inline)复选框组合
checkboxInput(inputId, label,value)单个复选框
dateInput(inputId, label, value, min,max, format,
startview, weekstart,language)
日期单选
dateRangeInput(inputId, label,start, end, min, max, format,
startview, weekstart, language,separator)
日期复选
numericInput(inputId, label, value,min, max, step)数字输入文本框
passwordInput(inputId, label,value)密码输入文本框
textInput(inputId, label, value)文本输入框
selectInput(inputId, label, choices,selected, multiple,
selectize, width,size)
(also selectizeInput())下拉列表
sliderInput(inputId, label, min, max,value, step,
round, format, locale,ticks, animate, width, sep, pre,post)
滚动条
actionLink(inputId, label, icon, …)链接
fileInput(inputId, label, multiple,accept)文件选择

Outputs

render<type>() and <type>Output() functions work together to add R output to the UI

render<type>()<type>Output()说明
DT::renderDataTable(expr, options,callback, escape, env, quoted)dataTableOutput(outputId, icon, …)dataTable
renderImage(expr, env, quoted,deleteFile)imageOutput(outputId, width, height,
click, dblclick, hover, hoverDelay, inline,
hoverDelayType, brush, clickId, hoverId)
图片
renderPlot(expr, width, height, res, …,env, quoted, func)plotOutput(outputId, width, height, click,
dblclick, hover, hoverDelay, inline,
hoverDelayType, brush, clickId, hoverId)
作图
renderPrint(expr, env, quoted, func,width)verbatimTextOutput(outputId)打印
renderTable(expr,…, env, quoted, func)tableOutput(outputId)表格输出
renderText(expr, env, quoted, func)textOutput(outputId, container, inline)文本标签输出
renderUI(expr, env, quoted, func)uiOutput(outputId, inline, container, …)
htmlOutput(outputId, inline, container, …)

Layouts(布局)

合并多个元素为单一元素

panel说明
absolutePanel()
conditionalPanel()
fixedPanel()
headerPanel()
inputPanel()输入面板
mainPanel()主面板
navlistPanel()
sidebarPanel()工具栏面板
tabPanel()
tabsetPanel()
titlePanel()标题面板
wellPanel()
wellPanel(
  dateInput("a", ""),
  submitButton()
)

UI布局

# fluidRow()
ui <- fluidPage(
 fluidRow(column(width = 4),column(width = 2, offset = 3)),
 fluidRow(column(width = 12))
)

2

# flowLayout
ui <- fluidPage(
 flowLayout( # object 1,
 # object 2, # object 3 )
)

3

# sidebarLayout
ui <- fluidPage(
 sidebarLayout(
 sidebarPanel(),  # 侧边栏
 mainPanel()      # 主面板
 )
)

4

# splitLayout
ui <- fluidPage(
 splitLayout( # object 1,
 # object 2
 )
)

5

# verticalLayout
ui <- fluidPage(
 verticalLayout( # object 1,
 # object 2,
 # object 3
 )
)

6

# tabPanel
ui <- fluidPage( tabsetPanel(
 tabPanel("tab 1", "contents"),
 tabPanel("tab 2", "contents"),
 tabPanel("tab 3", "contents")))
ui <- fluidPage( navlistPanel(
 tabPanel("tab 1", "contents"),
 tabPanel("tab 2", "contents"),
 tabPanel("tab 3", "contents")))
ui <- navbarPage(title = "Page",
 tabPanel("tab 1", "contents"),
 tabPanel("tab 2", "contents"),
 tabPanel("tab 3", "contents"))

7

Reactivity(反应)

Reactivity说明
input$<inputId> functions
reactiveValues(…)
input接收的值被储存在input$<inputId>
render<type>() functions
output$<outputId>
将结果保存到output$<outputId>
isolate(expr)抑制反应
observeEvent(eventExpr, handlerExpr,…)触发主观代码
reactive(x, env, quoted,label, domain)模块化反应机制
eventReactive(eventExpr,valueExpr,…)延迟反应
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值