3

我一直在探索(**并且喜欢)使用 R Shiny 开发模块化仪表板的 golem 包。但是我正在努力思考如何测试模块化仪表板。

例如,在下面的 repex 中,如果导入模块中的 input$n_rows 设置为 15,那么我将如何测试显示模块中的输出包含 15 行?

我将非常感谢对此的任何支持!


library(shiny)
library(reactable)
library(dplyr)

# Import module UI
mod_import_ui <- function(id){
  
  ns <- NS(id)
  
  fluidRow(
    # Allow the user to select the number of rows to view
    numericInput(ns("n_rows"), 
                 "Select number of observations",
                 value = 10)
    
  )
}

# Import module Server
mod_import_server <- function(id){
  
  moduleServer(
    id,
    function(input, output, session){
      
      data <- reactive({
        
        # Sample the requested number of rows from mtcars and return this to the application server
        mtcars %>%
          slice_sample(n = input$n_rows)
        # [....] # Some complex formatting and transformations
        
      })
      
      return(data)
      
      
      
    }
  )}

# Display module UI
mod_display_ui <- function(id){
  
  ns <- NS(id)
  
  fluidRow(
    
    reactableOutput(ns("table"))
    
  )
}

# Display module Server
mod_display_server <- function(id, data_in){
  
  moduleServer(
    id,
    function(input, output, session){
      
      # [....] # Some more transformations and merging with data from other modules
      
      output$table <- renderReactable(reactable(data_in()))
      
    }
  )}


app_ui <- function(request) { 
  
  tagList(
  
    mod_import_ui("import_1"),
    mod_display_ui("display_1")

  )
  
  }


app_server <- function(input, output, session) { 
  
  data_in <- mod_import_server("import_1")
  mod_display_server("display_1", data_in)
  
}

shinyApp(ui = app_ui, server = app_server)


4

2 回答 2

4

我建议将应用程序的核心与用户界面分开。

{golem} 框架允许在 R 包中构建您的应用程序,这意味着您可以使用从包构建到记录和测试代码的所有工具。
如果您按照engineering-shiny.org/中的指南进行操作,您将看到我们建议从“服务器”部分提取所有R 代码以在小插图中对其进行测试,将其转换为常规函数,以便您可以将其测试为通常与 R 包一起使用。
因此,您 ShinyApp 只调用已经记录和测试的内部函数。使用这种方法,您可以测试应用程序中可能发生的不同场景的输出。在静态脚本中尝试不同的输入参数并验证输出,无论您在后续开发步骤中对应用程序进行什么更改。

书中给出了很多建议。如果我必须将它们总结为一个工作流程,这将是:

  1. 直接在 Rmd 中构建必要的代码。这使您无需进行所有必要的点击即可测试操作。我们称之为“Rmd first”方法:https ://rtask.thinkr.fr/when-development-starts-with-documentation/
  2. 将此代码分解为 R 函数,以尽可能少地放入 Shiny 应用程序本身。
  3. 创建没有服务器(或不要太多)的 UI 部分,只是为了看看一般外观是什么样的
  4. 在应用程序的适当位置包含您的功能。
  5. 加强代码。可重现的示例、单元测试、文档、代码版本控制……(与代码并行执行此步骤会更好)
于 2021-02-10T16:25:51.473 回答
3

作为对塞巴斯蒂安回答的补充,我想指出从{shiny}v 1.5.0 开始,您可以直接使用该功能测试服务器功能testServer,这可能是您正在寻找的。

这是您如何实现这一目标的代表:

library(shiny)
library(magrittr)
library(dplyr)

mod_import_server <- function(id){
  moduleServer( id, function(input, output, session){
    data <- reactive({
      mtcars %>%
        slice(n = 1:input$n_rows)
    })
    return(data)
  })
}

shiny::testServer(mod_import_server, {
  
  for (i in 1:10){
    
    session$setInputs(n_rows = i)
    testthat::expect_equal(
      data(), 
      slice(mtcars, n = 1:i)
    )
    
  }
})

在这里,您可以测试您的reactive()行为是否符合您的预期。这并不完美,但是一个好的开始:) 虽然很难找到一种方法来测试第二个模块的行为,因为它依赖于作为值传递的 reactive()。

科林

于 2021-02-10T20:54:56.597 回答