zoukankan      html  css  js  c++  java
  • R shinydashboard ——1. 基本用法

    shiny和shinydashboard使用虽然简单,但控件众多,需及时总结归纳。
    install.packages("shinydashboard")

    shinydashboard的UI包括三部分结构:头header,侧边栏siderbar和主体body:

    ## ui.R ##
    library(shinydashboard)
    
    dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody()
    )
    

    结合server看下基础结构的效果:

    ## app.R ##
    library(shiny)
    library(shinydashboard)
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody()
    )
    
    server <- function(input, output) { }
    
    shinyApp(ui, server)
    

    image.png
    添加内容,如在body中添加box:

    ## app.R ##
    library(shinydashboard)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Basic dashboard"),
      dashboardSidebar(),
      dashboardBody(
        # Boxes need to be put in a row (or column)
        fluidRow(
          box(plotOutput("plot1", height = 250)),
    
          box(
            title = "Controls",
            sliderInput("slider", "Number of observations:", 1, 100, 50)
          )
        )
      )
    )
    
    server <- function(input, output) {
      set.seed(122)
      histdata <- rnorm(500)
    
      output$plot1 <- renderPlot({
        data <- histdata[seq_len(input$slider)]
        hist(data)
      })
    }
    
    shinyApp(ui, server)
    

    image.png

    添加侧边栏内容,如添加菜单项menuItem(和shiny中tabPanel类似):

    library(shiny)
    library(shinydashboard)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Basic dashboard"),
      
      ## Sidebar content
      dashboardSidebar(
        sidebarMenu(
          menuItem("Dashboard",tabName = "dashboard",icon = icon("dashboard")),  #菜单1
          menuItem("Widgets",tabName = "widgets",icon = icon("th"))
        ) #菜单2
      ),
    
      ## Body content
      dashboardBody(
        tabItems(
          # First tab content 菜单1
          tabItem(tabName = "dashboard",
                  fluidRow(
                    box(plotOutput("plot1", height = 250)),
                    
                    box(
                      title = "Controls",
                      sliderInput("slider", "Number of observations:", 1, 100, 50)
                    )
                  )
          ),
          
          # Second tab content 菜单2
          tabItem(tabName = "widgets",
                  h2("Widgets tab content")
          )
        )
      )
    )
    
    
    server <- function(input, output){
      set.seed(1)
      histdata <- rnorm(500)
      output$plot1 <- renderPlot({
        data <- histdata[seq_len(input$slider)]
        hist(data)
      })
    }
    
    shinyApp(ui, server)
    

    image.png

    Ref:
    https://rstudio.github.io/shinydashboard/get_started.html

  • 相关阅读:
    nasm astrstr函数 x86
    nasm astrspn函数 x86
    nasm astrset_s函数 x86
    nasm astrrev函数 x86
    模板:最长公共子序列(LCS)
    模板:最小生成树(MST)
    模板:并查集
    模板:单源最短路径
    模板:最近公共祖先(LCA)
    模板:线段树(1)——加法,求和
  • 原文地址:https://www.cnblogs.com/jessepeng/p/13049070.html
Copyright © 2011-2022 走看看