英文:
test output or observe elements in shiny using testthat
问题
我有一个Shiny应用程序,我在其中使用testthat
和testServer
来测试服务器组件。应用程序的服务器部分具有一些响应式值,但还在observe()
元素中渲染一些元素(出于性能原因,我将多个渲染组合在一起)。
一个简单的服务器+testServer示例函数如下:
library(shiny)
library(dplyr)
library(testthat)
server <- function(input, output, session) {
data <- reactive(mtcars)
observe({
print("Observe is active")
# 在这里对数据进行进一步的分析
data2 <- data() %>%
filter(mpg > 20)
output$nrows <- renderUI(nrow(data2)) # 14
output$unique_mpgs <- renderUI(length(unique(data2$mpg))) # 10
})
}
testServer(server, {
expect_equal(data(), mtcars) # 可以工作,因为data()是reactive()而不是observe()...
# 在这里如何测试nrows == 14和unique_mpgs == 10?
})
如前所述,如果可能的话,我不想有多个响应式值(例如一个用于nrows,然后另一个用于unique_mpgs)。
是否有一种方法可以在testServer中检查observe元素的值?
英文:
I have a shiny app, where I test the server component using testthat
and testServer
. The server part of the app has a couple of reactive values but also renders some elements in an observe()
element (I group multiple renders together for performance reasons).
A simple server + testServer example function looks like this
library(shiny)
library(dplyr)
library(testthat)
server <- function(input, output, session) {
data <- reactive(mtcars)
observe({
print("Observe is active")
# do some further analysis on the data here
data2 <- data() |> filter(mpg > 20)
output$nrows <- renderUI(nrow(data2)) # 14
output$unique_mpgs <- renderUI(length(unique(data2$mpg))) # 10
})
}
testServer(server, {
expect_equal(data(), mtcars) # works because data() is a reactive() not observe()...
# how can I test here that nrows == 14 and unique_mpgs == 10
})
As mentioned earlier, I don't want to have multiple reactive values (eg one for nrows, then another for unique_mpgs) if possible.
Is there a way to check the value of an observe element in testServer?
答案1
得分: 1
session$flushReact()
运行后似乎触发了 observe
部分(print
部分对于识别这一点非常有帮助)。这强制手动刷新所有的响应式组件。之后 output$nrows
和 output$unique_mpgs
变得可用。
testServer(server, {
expect_equal(data(), mtcars) # works because data() is a reactive() not observe()...
# manually force a flush of reactives
session$flushReact()
# check output is available
expect_error(output$nrows, NA)
expect_error(output$unique_mpgs, NA)
# or just
output$nrows
output$unique_mpgs
# since these are rendered as UI, so the output is HTML
expect_equal(
as.character(output$nrows$html),
'14'
)
expect_equal(
as.character(output$unique_mpgs$html),
'10'
)
})
英文:
Running session$flushReact()
seems to trigger the observe
part (the print part was helpful in identifying this). This forces manual refresh of all reactives. After that output$nrows
and output$unique_mpgs
become available.
testServer(server, {
expect_equal(data(), mtcars) # works because data() is a reactive() not observe()...
# manually force a flush of reactives
session$flushReact()
# check output is available
expect_error(output$nrows, NA)
expect_error(output$unique_mpgs, NA)
# or just
output$nrows
output$unique_mpgs
# since these are rendered as UI, so the output is HTML
expect_equal(
as.character(output$nrows$html),
'14'
)
expect_equal(
as.character(output$unique_mpgs$html),
'10'
)
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论