【问题标题】:How do I log or trace using shinytest?如何使用 shinytest 记录或跟踪?
【发布时间】:2018-10-19 22:10:06
【问题描述】:

我希望能够查看我的代码中 reactive({...}) 部分内部发生的情况。我认为使用 shinytest 可能是一种执行我的应用程序部分的方法,这些应用程序使用 Shiny Modules 并了解 callModule

我在我的代码中尝试了以下记录/跟踪/打印。

print("hello1")
message("hello2")
cat(file=stderr(), "hello3")
logging::loginfo("hello4")

runtest.R

library(shinytest)
testApp("/home/eddy/rwork/filters", "mytest")
viewTestDiff("/home/eddy/rwork/filters", interactive = FALSE)

输出:

Rscript runtest.R
Running mytest.R 
==== Comparing mytest... No changes.
==== mytest ====
No differences between expected and current results

如何在测试运行中添加一些跟踪输出?

【问题讨论】:

    标签: r shiny rstudio


    【解决方案1】:

    我认为现在还没有一种非常方便的方法可以在 shinytest 中查看应用程序输出,但是 ShinyDriver 对象上有这个方法:

    app$getDebugLog() 查询一个或多个调试日志:shiny_consolebrowsershinytest

    https://rstudio.github.io/shinytest/reference/ShinyDriver.html

    您可以将其与 shiny_console 选项一起使用,以在单个测试中打印应用程序输出,例如:

    # mytest.R
    
    app <- ShinyDriver$new()
    
    ...
    
    log <- app$getDebugLog("shiny_console")
    print(log)
    

    【讨论】:

      【解决方案2】:

      shinytest 在测试时集成了 shiny 导出值的概念,因此您可以使用函数shiny::exportTestValues() 来创建具有值的命名表达式,包括反应式,你想导出。

      例如,如果您有响应式 data.frame scaledData,它在您的应用代码中使用了某种输入绑定,您可以执行以下操作:

      scaledData <- reactive({
        dummyData[, "y"] <- dummyData[, "y"] * input$scale
        return(dummyData)
      })
      
      # The scaledData will be captured as a json object in the shinytest output
      exportTestValues(scaledData = scaledData())
      

      这将捕获 json 文件中 exports 键下快照中的反应值,因此您可以在测试比较中使用它(如果您愿意,还可以查看数据)。

      最后一点是,这些导出值仅在应用程序处于测试模式时运行,例如isTRUE(getOption("shiny.testmode")).

      我写了一篇关于如何使用它来测试闪亮的 DataTables 的博客文章,您可以在此处阅读:https://nadirsidi.github.io/Shinytest/

      【讨论】:

        【解决方案3】:

        您可以在反应函数中分配 print()、cat()、warning() 来检查 R 提示符中对象的值。这适用于我只在 RStudio 中使用 Shinny 而没有闪亮测试。 另外,正如您所说的那样,以前的选项不起作用,您可以放置​​一个 write.myformat() 函数来编写任何类型的对象并在外部检查它。

        【讨论】:

          猜你喜欢
          • 2021-02-07
          • 1970-01-01
          • 2011-04-06
          • 1970-01-01
          • 2016-05-12
          • 1970-01-01
          • 1970-01-01
          • 2021-01-15
          • 1970-01-01
          相关资源
          最近更新 更多