前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shiny学习-2

shiny学习-2

作者头像
火星娃统计
发布2020-09-15 15:27:25
1.8K0
发布2020-09-15 15:27:25
举报

shiny学习-2

概述

填补上次的更新

正文

添加控件

类似的控件如下

shiny提供了一系列的预置的控件,已经打包好,作为函数

函数名

控件

actionButton

Action Button

checkboxGroupInput

A group of check boxes

checkboxInput

A single check box

dateInput

A calendar to aid date selection

dateRangeInput

A pair of calendars for selecting a date range

fileInput

A file upload control wizard

helpText

Help text that can be added to an input form

numericInput

A field to enter numbers

radioButtons

A set of radio buttons

selectInput

A box with choices to select from

sliderInput

A slider bar

submitButton

A submit button

textInput

A field to enter text

使用这些控件需要两个参数,一个参数用来命名,一个参数是label,前一个被用来在程序内传递参数,后一个参数用来显示在用户界面 例子

library(shiny)

# 定义 UI ----
ui <- fluidPage(
  titlePanel("Basic widgets"), # app的名字
  fluidRow(   #构建网格化的控件,与之前的sidebarLayout不同
    column(3,#3为各个控件的间隔
           h3("Buttons"),#3号字体
           actionButton("action", "Action"),# 添加动作按钮
           br(),
           br(), 
           submitButton("Submit")),   
    column(3,
           h3("Single checkbox"),
           # 单选框
           checkboxInput("checkbox", "Choice A", value = TRUE)),    
    column(3, 
    # 多选框
           checkboxGroupInput("checkGroup", 
                              h3("Checkbox group"), 
                              choices = list("Choice 1" = 1, 
                                             "Choice 2" = 2, 
                                             "Choice 3" = 3),
                              selected = 1)),    
    column(3, 
    # 日期输入
           dateInput("date", 
                     h3("Date input"), 
                     value = "2014-01-01"))   
  ), 
  # 第二个行控件网格
  fluidRow(    
    column(3,# 日期范围
           dateRangeInput("dates", h3("Date range"))),    
    column(3,#输入文件
           fileInput("file", h3("File input"))),    
    column(3, #文本控件
           h3("Help text"),
           helpText("Note: help text isn't a true widget,", 
                    "but it provides an easy way to add text to",
                    "accompany other widgets.")),    
    column(3, #数字输入
           numericInput("num", 
                        h3("Numeric input"), 
                        value = 1))   
  ),  
#   第三行控件
  fluidRow(   
    column(3,#单选按钮
           radioButtons("radio", h3("Radio buttons"),
                        choices = list("Choice 1" = 1, "Choice 2" = 2,
                                       "Choice 3" = 3),selected = 1)),    
    column(3,#选择窗口
           selectInput("select", h3("Select box"), 
                       choices = list("Choice 1" = 1, "Choice 2" = 2,
                                      "Choice 3" = 3), selected = 1)),    
    column(3, #滑动条
           sliderInput("slider1", h3("Sliders"),
                       min = 0, max = 100, value = 50),
           sliderInput("slider2", "",
                       min = 0, max = 100, value = c(25, 75))
    ),    
    column(3, #文本输入
           textInput("text", h3("Text input"), 
                     value = "Enter text..."))   
  )  
)

# 定义server 代码 ----
# 这里为空
server <- function(input, output) {
  
}

# 运行app ----
shinyApp(ui = ui, server = server)

结果

显示输出

在UI中添加r对象的输出 具体函数如下

Output function

Creates

dataTableOutput

DataTable

htmlOutput

raw HTML

imageOutput

image

plotOutput

plot

tableOutput

table

textOutput

text

uiOutput

raw HTML

verbatimTextOutput

text

输出函数需要放在ui的sidebarPanel或mainPanel中 例子

# 构建输出面板,编写UI
ui <- fluidPage(
  titlePanel("censusVis"), 
  sidebarLayout(
    sidebarPanel(# 侧面板
      helpText("Create demographic maps with
               information from the 2010 US Census."),
      
      selectInput("var", 
                  label = "Choose a variable to display",
                  choices = c("Percent White", 
                              "Percent Black",
                              "Percent Hispanic", 
                              "Percent Asian"),
                  selected = "Percent White"),
      
      sliderInput("range", 
                  label = "Range of interest:",
                  min = 0, max = 100, value = c(0, 100))
    ),
    
    mainPanel(# 主面板
      textOutput("selected_var")#文本输出
    )
  )
)

在面板中定义了输出,接下来需要在server中定义输出内容 在shiny中提供了函数自动引用面板中的数据,render函数自动引用面板中的变量

render function

creates

renderDataTable

DataTable

renderImage

images (saved as a link to a source file)

renderPlot

plots

renderPrint

any printed output

renderTable

data frame, matrix, other table like structures

renderText

character strings

renderUI

a Shiny tag object or HTML

# 编写server代码
server <- function(input, output) {
  #在ui中,我们定义了selected_var,但是并没有定义它的具体赋值
  output$selected_var <- renderText({ 
    paste("You have selected", input$var)
  })
  
}

结果,控件框中输入不同的值,右边显示不同的结果

结束语

love & peace

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 火星娃统计 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • shiny学习-2
    • 概述
      • 正文
        • 添加控件
        • 显示输出
      • 结束语
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档