gpt4 book ai didi

r - 单元测试 Shiny 的应用程序

转载 作者:行者123 更新时间:2023-11-28 19:40:23 24 4
gpt4 key购买 nike

所以我一直在编写一个相当详细的 Shiny 应用程序,并且在未来需要更新,因为运行的内容背后的功能不断变化。

我需要做的是进行单元测试(使用 testthat 或其他对 Shiny 应用更有用的库),使我能够以更自动化的方式运行这些测试。

我编写了一个简单的 Shiny 应用程序。为了进行测试,我想知道如果我在数字输入中选择数字 20,那么我会得到 400 作为 output$out 文本。但是希望能够在不亲自实际运行应用程序的情况下执行此操作。

library(shiny)

ui <- fluidPage(title = 'Test App',
numericInput('num', 'Number', 50, 1, 100, 0.5),
'Numeric output',
textOutput('out')
)

server <- function(input, output, session) {
aux <- reactive(input$num ^ 2)

output$out <- renderText(aux())
}

shinyApp(ui = ui, server = server)

最佳答案

正如许多人已经提到的,您可以将包 shinytest 与 testthat 结合使用。

这里有一个简单的例子:

library(shinytest)
library(testthat)

context("Test shiny app")

#open shiny app
app <- ShinyDriver$new('path_to_shiny_app')

test_that("app gets expected output", {
#set numeric input
app$setInputs(num = 20)
#get output
output <- app$getValue(name = "out")
#test
expect_equal(output, "400")
})

#stop shiny app
app$stop()

关于r - 单元测试 Shiny 的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338779/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com