如何简化第一个面板中生成的代码?

时间:2015-11-24 17:19:43

标签: r

library(shiny)
library(shinydashboard)
ui <- dashboardPage(  skin="blue",title = "asdfasf",
dashboardHeader(title = "Fund Analysis Status Tool",titleWidth=255, .list=NULL),            
dashboardSidebar(  width = 255, sidebarMenu(menuItem("Analysis",  icon = icon("th"),menuSubItem("ME-1", tabName = "Analysis-ME-1"),
menuSubItem("Current", tabName = "Analysis-Current")),menuItem("Post-Analysis1",icon = icon("th"),menuSubItem("ME-1", tabName = "Post_Analysis-ME-1"),menuSubItem("Current", tabName = "Post_Analysis-Current")))), # it gives heading in the left panel

dashboardBody(
tabItems(tabItem(tabName = "Analysis-ME-1",fluidRow(box(dataTableOutput('table')),box(title = "Controls",
sliderInput("slider", "Number of observations:", 1, 100, 50)))),
tabItem(tabName = "Analysis-Current",h2("To be discussed1")),       

                       # Second tab content
tabItem(tabName = "Post_Analysis-ME-1", h2("To be discussed1")),
tabItem(tabName = "Post_Analysis-Current",h2("To be discussed2")))))



server <- function(input, output) { 
histdata <- rnorm(500)
output$table <- renderDataTable(iris)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
}
shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

这主要是关于你如何做到这一点的意见问题。我已经重新组织了您的代码,以便我可以在下面进行处理。

在我构建的少数应用程序中,我选择的策略因应用程序的大小而异。在较小的应用程序中,我会在函数中提供源代码或在global.R中定义它们,然后在整个应用程序中使用这些函数。最近,我开始将每个应用程序编写为独立包。

无论哪种方式,一般过程都是将您的应用程序分解为小步骤,将每个步骤写入具有描述性名称的函数(简洁并不一定是这里的优点),然后调用函数生成你的申请。

library(shiny) 
library(shinydashboard)

#* Put these in your global.R file, perhaps.
dashboard_header <- function(){
  dashboardHeader(
      title = "Fund Analysis Status Tool",
      titleWidth=255, 
      .list=NULL
    )
}

dashboard_sidebar <- function(){
  dashboardSidebar(
      width = 255,
      sidebarMenu(
        menuItem("Analysis",  icon = icon("th"),
          menuSubItem(
            "ME-1", 
            tabName = "Analysis-ME-1"
          ),
          menuSubItem(
            "Current", 
            tabName = "Analysis-Current"
          )
        ),
        menuItem(
          "Post-Analysis1",
          icon = icon("th"),
          menuSubItem(
            "ME-1", 
            tabName = "Post_Analysis-ME-1"
          ),
          menuSubItem(
            "Current", 
            tabName = "Post_Analysis-Current"
          )
        )
      )
    )
}

tab_items <- function(){
  tabItems(
        # First tab content
        tabItem(
          tabName = "Analysis-ME-1",
          fluidRow(
            box( dataTableOutput('table') )
          ),
          box(
            title = "Controls",
            sliderInput(
              "slider", 
              "Number of observations:", 
              1, 
              100, 
              50
            )
          )
        ),
        tabItem(
          tabName = "Analysis-Current",
          h2("To be discussed1")
        ),
        # Second tab content
        tabItem(
          tabName = "Post_Analysis-ME-1",
          h2("To be discussed1")
        ),
        tabItem(
          tabName = "Post_Analysis-Current",
          h2("To be discussed2")
        )
      )
}

#***************************
#***************************
#* And now the ui and server

ui <- 
  dashboardPage( 
    skin="blue",
    title = "asdfasf",
    dashboard_header(),
    dashboard_sidebar(),# it gives heading in the left panel
    dashboardBody(
      tab_items()
    )
  )

server <- shinyServer(function(input, output) {
  histdata <- rnorm(500)
  output$table <- renderDataTable(iris)
  output$plot1 <- renderPlot({ data <- histdata[seq_len(input$slider)]
                               hist(data) })
})

shinyApp(ui, server)