组织更大的应用程序的最佳实践是什么

但是,在shiny 的环境中,我可以采用哪些独特的技巧和技巧来让shiny 的代码看起来更好(更可读)?

  • 利用面向对象编程技术
  • server.R中,哪些零件应该采购?
  • 包含markdown 文档、图片、,

例如,如果我在每tabPanel个元素中使用navbarPagetabsetPanel,那么在添加了几个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

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)

推荐答案

添加modules至R后发亮.在shiny 的应用程序中管理复杂的 struct 变得容易多了.

发光模块的详细说明:Here

使用模块的优点:

  • 一旦创建,它们很容易重用
  • ID冲突更容易避免
  • 基于模块输入和输出的代码组织

在基于标签的shiny 应用程序中,一个标签可以被视为一个模块,其中有inputsoutputs.然后,选项卡的输出可以作为输入传递给其他选项卡.

基于标签 struct 的单文件应用程序,利用模块化思维.应用程序可以使用cars个数据集进行测试.从Joe Cheng(第一个链接)复制的部分代码.欢迎大家发表意见.

# Tab module
# This module creates new tab which renders dataTable

dataTabUI <- function(id, input, output) {
  # Create a namespace function using the provided id
  ns <- NS(id)

  tagList(sidebarLayout(sidebarPanel(input),

                        mainPanel(dataTableOutput(output))))

}

# Tab module
# This module creates new tab which renders plot
plotTabUI <- function(id, input, output) {
  # Create a namespace function using the provided id
  ns <- NS(id)

  tagList(sidebarLayout(sidebarPanel(input),

                        mainPanel(plotOutput(output))))

}

dataTab <- function(input, output, session) {
  # do nothing...
  # Should there be some logic?


}

# File input module
# This module takes as input csv file and outputs dataframe
# Module UI function
csvFileInput <- function(id, label = "CSV file") {
  # Create a namespace function using the provided id
  ns <- NS(id)

  tagList(
    fileInput(ns("file"), label),
    checkboxInput(ns("heading"), "Has heading"),
    selectInput(
      ns("quote"),
      "Quote",
      c(
        "None" = "",
        "Double quote" = "\"",
        "Single quote" = "'"
      )
    )
  )
}

# Module server function
csvFile <- function(input, output, session, stringsAsFactors) {
  # The selected file, if any
  userFile <- reactive({
    # If no file is selected, don't do anything
    validate(need(input$file, message = FALSE))
    input$file
  })

  # The user's data, parsed into a data frame
  dataframe <- reactive({
    read.csv(
      userFile()$datapath,
      header = input$heading,
      quote = input$quote,
      stringsAsFactors = stringsAsFactors
    )
  })

  # We can run observers in here if we want to
  observe({
    msg <- sprintf("File %s was uploaded", userFile()$name)
    cat(msg, "\n")
  })

  # Return the reactive that yields the data frame
  return(dataframe)
}
basicPlotUI <- function(id) {
  ns <- NS(id)
  uiOutput(ns("controls"))

}
# Functionality for dataselection for plot
# SelectInput is rendered dynamically based on data

basicPlot <- function(input, output, session, data) {
  output$controls <- renderUI({
    ns <- session$ns
    selectInput(ns("col"), "Columns", names(data), multiple = TRUE)
  })
  return(reactive({
    validate(need(input$col, FALSE))
    data[, input$col]
  }))
}

##################################################################################
# Here starts main program. Lines above can be sourced: source("path-to-module.R")
##################################################################################

library(shiny)


ui <- shinyUI(navbarPage(
  "My Application",
  tabPanel("File upload", dataTabUI(
    "tab1",
    csvFileInput("datafile", "User data (.csv format)"),
    "table"
  )),
  tabPanel("Plot", plotTabUI(
    "tab2", basicPlotUI("plot1"), "plotOutput"
  ))

))


server <- function(input, output, session) {
  datafile <- callModule(csvFile, "datafile",
                         stringsAsFactors = FALSE)

  output$table <- renderDataTable({
    datafile()
  })

  plotData <- callModule(basicPlot, "plot1", datafile())

  output$plotOutput <- renderPlot({
    plot(plotData())
  })
}


shinyApp(ui, server)

R相关问答推荐

为什么以及如何修复Mapview不显示所有点并且st_buffer合并一些区域R?

R Lubridate:舍入/快照日期时间到一天中最近的任意时间?

寻找图片边缘

如何计算前一行的值,直到达到标准?

过滤器数据.基于两列的帧行和R中的外部向量

如何改变x轴比例的列在面

DEN扩展包中的RECT树形图出现异常行为

具有重复元素的维恩图

根据另一列中的值和条件查找新列的值

观察器中的inaliateLater的位置

如何计算每12行的平均数?

自定义交互作用图的标签

如何显示准确的p值而不是<;0.001*?

计算来自单独分组的分幅的值的百分位数

如何在AER::ivreg中指定仪器?

从字符串01JAN2021创建日期

我怎么才能把一盘棋变成一盘棋呢?

真实世界坐标的逆st_变换

如何修复geom_rect中的层错误?

在shiny /bslb中,当卡片是从json生成时,如何水平排列卡片?