R包shiny的学习--完整 记录(上)

Shiny包是一个能够方便构建交互式网页应用的R包。

下载

还是很好下的

install.packages("shiny")

基本例子(lesson1)

runExample("01_hello")      # a histogram
runExample("02_text")       # tables and data frames
runExample("03_reactivity") # a reactive expression
runExample("04_mpg")        # global variables
runExample("05_sliders")    # slider bars
runExample("06_tabsets")    # tabbed panels
runExample("07_widgets")    # help text and submit buttons
runExample("08_html")       # Shiny app built from HTML
runExample("09_upload")     # file upload wizard
runExample("10_download")   # file download wizard
runExample("11_timer")      # an automated timer

# 在输出网页中显示完整代码
runApp("census-app", display.mode = "showcase")

以  runExample("01_hello")  为例,看一看其完整代码~

# runExample_01_hello.R
# Define UI for app that draws a histogram ----
ui <- fluidPage(

  # App title ----
  titlePanel("Hello Shiny!"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Slider for the number of bins ----
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Histogram ----
      plotOutput(outputId = "distPlot")

    )
  )
)

# Define server logic required to draw a histogram ----
server <- function(input, output) {

  # Histogram of the Old Faithful Geyser Data ----
  # with requested number of bins
  # This expression that generates a histogram is wrapped in a call
  # to renderPlot to indicate that:
  #
  # 1. It is "reactive" and therefore should be automatically
  #    re-executed when inputs (input$bins) change
  # 2. Its output type is a plot
  output$distPlot <- renderPlot({

    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = "#007bc2", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")

    })

}

shinyApp(ui = ui, server = server)

在当前工作目录下创建App-1文件夹,并将 上面的脚本储存 在该文件夹中,执行以下命令,可得到原结果。

 基本结构(lesson2)

library(shiny)

# Define UI ----
ui <- fluidPage(
  titlePanel('titlenames'),
  
  sidebarLayout(position = 'left',
    sidebarPanel('sidebarnames'),
    mainPanel('mainnames')
  )
  
)

# Define server logic ----
server <- function(input, output) {
  
}

# Run the app ----
shinyApp(ui = ui, server = server)

 文本调整方式

其中 img() 代表图像的参数中,图像存储位置必须位于与应用程序相同的目录中名为www的文件夹中

img(src = "my_image.png", height = 72, width = 72)
# height/width 指的是像素大小

例(格式应用)

结果显示(可根据上表自行复现)

相关代码 如下

ui <- fluidPage(
  titlePanel(h1('myapp_format_text',align = "center")),
  
  sidebarLayout(position = 'left',
    sidebarPanel(
      h2('Installation'),
      p('Shiny is available on CRAN, so you can install it in the usual way from your R console:'),
      code("install.packages('shiny')"),
      br(),
      br(),
      br(),
      img(src = "R_studio.png", height = 140, width = 400),
      p('Shiny is a product of ',
        span('Rstudio',style = 'color:blue')),
      ),
    mainPanel(
      h2('Introducing Shiny'),
      p('Shiny is a new package from Rstudio that makes it',
        em('incredibly'),
        'easy to build interactive web applications with R'),
      br(),
      p('For an introduction and live examples, visit the ',
        a('Shiny homepage.',
          href = 'http://shiny.rstudio.com')),
      br(),
      br(),
      h2('Features'),
      p('- Build useful web application with only a few lines of code-no JavaScript required.'),
      p("- Shiny application are automatically 'live' in the same way that",
        strong('spreadsheets'),
        'are live. Outputs change instantly as users modify inputs, without requiring a reload of the browser.'),
      )
  )

)

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

添加小组件(lesson3)

在sidebarPanel() 或者 mianPanel()中添加

参数
function 	        widget
actionButton 	    Action Button
submitButton 	    A submit button
checkboxInput     	A single check box
checkboxGroupInput 	A group of check boxes
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
textInput 	        A field to enter text

actionButton("action", label = "Action")

用法

ui <- fluidPage(
  titlePanel("Basic widgets"),
  sidebarLayout(
    sidebarPanel(
        fluidRow( 
           column( )# 用于行列中设置多个组件的情况下(如上图示例)
        )
    ),
  mainPanel('mainnames')
  )
)
  

例(格式应用)

结果显示(可根据上表自行复现)

 相关代码 如下

ui <- fluidPage(
    titlePanel(h1('censusVis')),
    
    sidebarLayout(
      sidebarPanel(p('Create demographic maps with information from the 2010 US Census.'),
                   selectInput('selectinput1',
                               h2('Choose a variable to display'),
                               choices = list('Percent White' = 1,'Percent Black' = 2, 
                                              'Percent Hispanic' = 3, 'Percent Asian' = 4),
                               selected = "Percent White"),
                   sliderInput('slider',h2('Ranger of interest'), 
                                min = 0, max = 100, value = c(1,100))),
      mainPanel('mainnames')
    )
    
  )

server <- function(input, output) {

}

shinyApp(ui = ui, server = server)

Shiny - Tutorialhttps://shiny.rstudio.com/tutorial/ 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值