R shiny 开发web交互应用数据-R语言-图表-决策-Linux-Pythonshiny

R包shiny开发网页--3.认识shinydashboard

2018-09-14  本文已影响21人  小洁忘了怎么分身

据说这是一个shiny必学框架。上一节中提到,常用的布局方式是侧边栏+主题,shinydashboard的UI界面就是根据这个布局设计的。
本文是根据这个导航页练习的。
https://rstudio.github.io/shinydashboard/get_started.html

#首先要安装这个包
install.packages("shinydashboard")

1.空白页面模板,仅分侧边栏(Slidebar)和主体(Body)两部分


页面是空的,只能看出分了两部分

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

标准风格代码

header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody()
ui <- dashboardPage(header, sidebar, body)
server= function(input, output) { }
shinyApp(ui = ui, server=server )

2.给主体body加上内容

library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(),
  dashboardBody(
    #把两个box放在同一行,fluidRow布局
    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)

3.分两页(tab)并在侧边栏放菜单(sidebarMenu)

分两页的

需要实现:
(1)侧边栏
添加菜单板块:silebarMenu,菜单由条目组成
添加菜单单个条目menuItem:包括变量名,标签名tabName和图标icon
(2)主体
标签页:tabItems
分别定义两页内容:tabItem,以tabName关联菜单栏。

library(shinydashboard)
ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Widgets", tabName = "widgets", icon = icon("th"))
    )
  ),
  dashboardBody(
    
    tabItems(
      # 第一页的内容
      tabItem(tabName = "dashboard",
              fluidRow(
                box(plotOutput("plot1", height = 250)),
                
                box(
                  title = "Controls",
                  sliderInput("slider", "Number of observations:", 1, 100, 50)
                )
              )
      ),
      
      # 第二页的内容
      tabItem(tabName = "widgets",
              h2("Widgets tab content")
      )
    )
  )
)

server <- function(input, output) {
  set.seed(122)
  histdata <- rnorm(500)
  
  output$plot1 <- renderPlot({
    data <- histdata[seq_len(input$slider)]
    hist(data)
  })
}

shinyApp(ui, server)

层层嵌套,好臃肿的结构!粘贴了好几遍才弄对了。

标准风格的UI模板是:

header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody()
dashboardPage(header, sidebar, body)

刚才的一大段代码可以根据这个优化为::

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

以下属于强迫症患者的尝试:
body部分还是很肿大,再试试。嗯结果失败了。。
睡了一觉起来再试又成功了

library(shinydashboard)
header=dashboardHeader(title = "Basic dashboard")
sidebar=dashboardSidebar(
  sidebarMenu(
    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
    menuItem("Widgets", tabName = "widgets", icon = icon("th"))
  )
)
x=fluidRow(
  box(plotOutput("plot1", height = 250)),
  box(
    title = "Controls",
    sliderInput("slider", "Number of observations:", 1, 100, 50)
  )
)
body=dashboardBody(
  tabItems(
    tabItem(tabName = "dashboard",x),
    tabItem(tabName = "widgets",h2("Widgets tab content"))
  )
)
ui <- dashboardPage(header,sidebar,body,title = 'doudou')
server <- function(input, output) {
  set.seed(122)
  histdata <- rnorm(500)  
  output$plot1 <- renderPlot({
    data <- histdata[seq_len(input$slider)]
    hist(data)
  })
}

shinyApp(ui, server)

盘点一下,每段代码在网页上的体现



微信公众号生信星球同步更新我的文章。


上一篇下一篇

猜你喜欢

热点阅读