【发布时间】:2015-01-20 16:54:22
【问题描述】:
组织大型 Shiny 应用程序的最佳做法是什么?
我认为最佳 R 实践也适用于 Shiny。
此处讨论了最佳 R 实践:How to organize large R programs
链接到 Google 的 R 风格指南:Style Guide
但是在 Shiny 上下文中,我可以采用哪些独特的技巧和窍门来使我的 Shiny 代码看起来更好(并且更具可读性)? 我在想这样的事情:
- 在 Shiny 中利用面向对象编程
- 在
server.R中应该采购哪些部件? - 项目的文件层次结构包含 Markdown 文档、图片、 xml 和源文件
例如,如果我在每个tabPanel 中都使用navbarPage 和tabsetPanel,那么在添加几个UI 元素后,我的代码开始看起来很混乱。
示例代码:
server <- function(input, output) {
#Here functions and outputs..
}
ui <- shinyUI(navbarPage("My Application",
tabPanel("Component 1",
sidebarLayout(
sidebarPanel(
# UI elements..
),
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("plot")
# More UI elements..
),
tabPanel("Summary", verbatimTextOutput("summary")
# And some more...
),
tabPanel("Table", tableOutput("table")
# And...
)
)
)
)
),
tabPanel("Component 2"),
tabPanel("Component 3")
))
shinyApp(ui = ui, server = server)
为了组织 ui.R 代码,我从 GitHub 找到了非常好的解决方案:radiant code
解决方案是使用renderUI 来渲染每个tabPanel,并且在server.R 中,标签来自不同的文件。
server <- function(input, output) {
# This part can be in different source file for example component1.R
###################################
output$component1 <- renderUI({
sidebarLayout(
sidebarPanel(
),
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)
)
)
})
#####################################
}
ui <- shinyUI(navbarPage("My Application",
tabPanel("Component 1", uiOutput("component1")),
tabPanel("Component 2"),
tabPanel("Component 3")
))
shinyApp(ui = ui, server = server)
【问题讨论】: