Skip to contents

find_vars()

Test function (is.double)

testthat::test_that("find_vars", {
    testthat::expect_equal(
    object = find_vars(datasets::airquality,
                            filter = is.double),
    expected = names(airquality)[vapply(airquality, is.double, logical(1))])
    test_cmt("find_vars", "is.double()")
})
#> 
#>     find_vars: is.double() 
#> Test passed

Test function (is.character)

testthat::test_that("find_vars", {
  testthat::expect_equal(
    object = find_vars(datasets::mtcars,
                            filter = is.character),
    expected = datasets::mtcars |>
      dplyr::select(where(is.character)) |>
      names())
  test_cmt("find_vars", "is.character()")
})
#> 
#>     find_vars: is.character() 
#> Test passed

Test error: state.abb is a vector–pass is.character function to filter

find_vars(data = datasets::state.abb, is.character)
#> Error in find_vars(data = datasets::state.abb, is.character): is.data.frame(data) is not TRUE
testthat::test_that("find_vars", {
  testthat::expect_error(object =
      find_vars(datasets::state.abb, filter = is.character))
  test_cmt("find_vars", "error: data")
})
#> 
#>     find_vars: error: data 
#> Test passed

Test error: pass is.miracle function to filter

find_vars(data = datasets::mtcars, is.miracle)
#> Error in eval(expr, envir, enclos): object 'is.miracle' not found
testthat::test_that("find_vars", {
  testthat::expect_error(object =
      find_vars(datasets::mtcars, filter = is.miracle))
  test_cmt("find_vars", "error: filter")

})
#> 
#>     find_vars: error: filter 
#> Test passed

selectVarServer()

The selectVarServer() function has additional arguments, so the args list is used (with reactive()):

shiny::testServer(selectVarServer,

  args = list(data = reactive(datasets::mtcars),
              filter = is.numeric), expr = {

  testthat::expect_true(
    object = is.reactive(data))
  test_cmt("selectVarServer", "is.reactive(data())")
})
#> Loading required package: shiny
#> 
#>     selectVarServer: is.reactive(data())

Test passing different arguments to find_vars()

shiny::testServer(selectVarServer,
  args = list(data = reactive(datasets::chickwts),
              filter = is.numeric), expr = {
  testthat::expect_equal(
    object = find_vars(data(), is.factor),
    expected = "feed")
  test_cmt("selectVarServer", "find_vars()")
})
#> 
#>     selectVarServer: find_vars()

Test passing filter and setting input$var:

shiny::testServer(selectVarServer,
  args = list(data = reactive(datasets::chickwts),
              filter = is.numeric), expr = {

  session$setInputs(var = "weight")
  testthat::expect_equal(object = input$var,
      expected = "weight")
  test_cmt("selectVarServer", "input$var")
})
#> 
#>     selectVarServer: input$var

Test passing filter, setting input$var, and session$returned():

shiny::testServer(selectVarServer,
  args = list(data = shiny::reactive(datasets::chickwts),
              filter = is.numeric), expr = {
  session$setInputs(var = "feed")
  testthat::expect_equal(object = session$returned(),
      expected = datasets::chickwts[["feed"]])
  test_cmt("selectVarServer", "session$returned()")

})
#> 
#>     selectVarServer: session$returned()

selectVarApp()

Test is.reactive(var)

sv_app <- selectVarApp()
shiny::testServer(app = sv_app, expr = {
  session$setInputs(`var-var` = "Ozone",
                    `data-dataset` = "airquality")
  
  testthat::expect_true(object = is.reactive(var))
  test_cmt("selectVarApp", "is.reactive(var)")
})
#> 
#>     selectVarApp: is.reactive(var)

Test the inputs with module namespaces

sv_app <- selectVarApp()
shiny::testServer(app = sv_app, expr = {
  session$setInputs(`var-var` = "Ozone",
                    `data-dataset` = "airquality")

  testthat::expect_equal(
    object = input$`var-var`,
    expected = "Ozone")
  test_cmt("selectVarApp", "input$`var-var`")

})
#> 
#>     selectVarApp: input$`var-var`

Test data() is reactive

sv_app <- selectVarApp()
shiny::testServer(app = sv_app, expr = {
  session$setInputs(`var-var` = "Ozone",
                    `data-dataset` = "airquality")

  testthat::expect_true(object = is.reactive(data))
  test_cmt("selectVarApp", "is.reactive(data)")

})
#> 
#>     selectVarApp: is.reactive(data)

Test class() of data()

sv_app <- selectVarApp()
shiny::testServer(app = sv_app, expr = {
  session$setInputs(`var-var` = "Ozone",
                    `data-dataset` = "airquality")
  
  testthat::expect_true(
    object = is.data.frame(data()))
  test_cmt("selectVarApp", "is.data.frame(data())")

})
#> 
#>     selectVarApp: is.data.frame(data())

Test specific variables from data()

sv_app <- selectVarApp()
shiny::testServer(app = sv_app, expr = {
  session$setInputs(`var-var` = "Ozone",
                    `data-dataset` = "airquality")

  testthat::expect_equal(
    object = data()[[input$`var-var`]],
    expected = airquality[["Ozone"]])
  test_cmt("selectVarApp", "data()[[input$`var-var`]]")

})
#> 
#>     selectVarApp: data()[[input$`var-var`]]