我一直在探索(并热爱)golem软件包,用于使用R Shiny开发模块化仪表板。但是我一直在努力学习如何测试模块化仪表板。
例如,在下面的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)
我建议将应用程序的核心与用户界面分开。
{golem}框架允许在R包中构建应用程序,这意味着您可以使用从包构建到文档编制和测试代码的所有工具。
如果您按照engineering-shiny.org/中的指南进行操作,则会看到我们建议您从“服务器”部分中提取所有R代码以在小插图中对其进行测试,并将其转换为常规函数,以便可以对其进行测试。通常与R包一起使用。
因此,您的ShinyApp仅调用已记录和测试的内部函数。使用这种方法,您可以测试应用程序中可能发生的不同方案的输出。在开发的后续步骤中,无论您在应用程序中进行了什么更改,都可以在静态脚本中尝试不同的输入参数并验证输出。
这本书提供了很多建议。如果我必须将它们总结为一个工作流程,则为:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句