tests/testthat/test-driveplot.R

test_that("dataframe causes warning", {
  skip_on_os(os = "linux")
  expect_warning(
    driveplot(
      shareddata = drive7,
      x = time_cst,
      ys = speed_mph
    ),
    "Converted `shareddata` to a SharedData object."
  )
})

# Test calls to driveplot_map() ----
test_that("if providing lng/lat, must provide both", {
  drive1 <- nds_data |>
    dplyr::filter(drive == 1)
  drive1shared <- crosstalk::SharedData$new(drive1)
  # lng is provided, but lat is NULL
  expect_error(
    driveplot(
      shareddata = drive1shared,
      lng = gps_long,
      lat = NULL,
      x = time_cst,
      ys = speed_mph
    ),
    "If providing `lng` and `lat`, must provide both."
  )
  # lat is provided, but not lng
  expect_error(
    driveplot(
      shareddata = drive1shared,
      lat = gps_lat,
      x = time_cst,
      ys = speed_mph
    ),
    "If providing `lng` and `lat`, must provide both."
  )
})

test_that("check spelling of lng/lat", {
  drive1 <- nds_data |>
    dplyr::filter(drive == 1)
  drive1shared <- crosstalk::SharedData$new(drive1)
  # Misspelled lng
  expect_error(
    driveplot(
      shareddata = drive1shared,
      lng = gps_lng,
      lat = gps_lat,
      x = time_cst,
      ys = speed_mph
    ),
    "Can't find `gps_lng` in `shareddata`."
  )
  # Misspelled lat
  expect_error(
    driveplot(
      shareddata = drive1shared,
      lng = gps_long,
      lat = gps_lt,
      x = time_cst,
      ys = speed_mph
    ),
    "Can't find `gps_lt` in `shareddata`."
  )
})

test_that("don't throw error when lat/lng can be inferred", {
  skip_on_os(os = "linux")
  drive1 <- nds_data |>
    dplyr::filter(drive == 1)
  drive1shared <- crosstalk::SharedData$new(drive1)
  expect_message(
    driveplot(
      shareddata = drive1shared,
      x = time_cst,
      ys = speed_mph
    ),
    'Assuming "gps_long" and "gps_lat" are longitude and latitude, respectively'
  )
  drive1rename <- drive1 |>
    dplyr::rename(
      latitude = gps_lat,
      longitude = gps_long
    )
  drive1renameshared <- crosstalk::SharedData$new(drive1rename)
  expect_message(
    driveplot(
      shareddata = drive1renameshared,
      x = time_cst,
      ys = speed_mph
    ),
  'Assuming "longitude" and "latitude" are longitude and latitude, respectively'
  )
})

test_that("throw error when sf column geometry type isn't POINT", {
  drive7_ls <- drive7 |>
    sf::st_cast("LINESTRING", warn = FALSE)
  drive7_ls_sd <- crosstalk::SharedData$new(drive7_ls)
  expect_error(
    driveplot(
      shareddata = drive7_ls_sd,
      x = time_cst,
      ys = speed_mph
    )
  )
})

test_that("works for both ways to specify coordinates", {
  skip_on_os(os = "linux")
  drive1 <- nds_data |>
    dplyr::filter(drive == 1)
  drive1shared <- crosstalk::SharedData$new(drive1)
  shared_drive <- crosstalk::SharedData$new(drive7)
  # Specify lat/lng
  expect_no_error(
    driveplot(
      shareddata = drive1shared,
      lng = gps_long,
      lat = gps_lat,
      x = time_cst,
      ys = speed_mph
    )
  )

  # Provide a geometry column with type POINT
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_mph
    )
  )
})

test_that("provide lat/lng but not x and ys", {
  drive1 <- nds_data |>
    dplyr::filter(drive == 1)
  drive1shared <- crosstalk::SharedData$new(drive1)
  expect_error(
    driveplot(
      shareddata = drive1shared,
      lng = gps_long,
      lat = gps_lat
    ),
    "argument is missing, with no default"
  )
})

test_that("provide geometry column but not x and ys", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(shareddata = shared_drive),
    "argument is missing, with no default"
  )
})

test_that("width and height are not numeric", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  # Check width
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_mph,
      width = 100,
      height = "100vh"
    ),
    'Arguments `width` and `height` must be provided as strings,
      e.g., "100%" or "400px".'
  )

  # Check height
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_mph,
      width = "100%",
      height = 100
    ),
    'Arguments `width` and `height` must be provided as strings,
      e.g., "100%" or "400px".'
  )
})

test_that("width and height use a correct unit", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  # Check width
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_mph,
      width = "100",
      height = "100vh"
    ),
    "Arguments `width` and `height` must be provided as strings
          with one of the following units: %, vh, vw, or px."
  )
  # Check height
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_mph,
      width = "100%",
      height = "100p"
    ),
    "Arguments `width` and `height` must be provided as strings
          with one of the following units: %, vh, vw, or px."
  )
})

# Test calls to driveplot_companions() ----
test_that("x not provided", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(shareddata = shared_drive, ys = speed_mph),
    "argument is missing, with no default"
  )
})

test_that("ys not provided", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(shareddata = shared_drive, x = time_cst),
    "argument is missing, with no default"
  )
})

test_that("misspelled x", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_est,
      ys = speed_mph
    ),
    "object 'time_est' not found"
  )
})

test_that("misspelled first y", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_kph
    ),
    "object 'speed_kph' not found"
  )
})

test_that("misspelled second y", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph, gps_headin)
    ),
    "object 'gps_headin' not found"
  )
})

test_that("misspelled third y", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph, gps_heading, gyro_headin)
    ),
    "object 'gyro_headin' not found"
  )
})

test_that("misspelled fourth y", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(
        speed_mph, gps_heading,
        gyro_heading, gps_dop
      )
    ),
    "object 'gps_dop'"
  )
})

test_that("specify only one y without c() or list()", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_mph
    )
  )
})

test_that("specify only one y with c()", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph)
    )
  )
})

test_that("specify only one y with list()", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = list(speed_mph)
    )
  )
})

test_that("specify two ys with c()", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph, gyro_heading)
    )
  )
})

test_that("specify two ys with list()", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = list(speed_mph, gyro_heading)
    )
  )
})

test_that("specify five ys with c()", {
  skip_on_os(os = "linux")
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_warning(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(
        speed_mph, gyro_heading,
        gps_heading, gps_pdop,
        gps_hdop
      )
    )
  )
})

test_that("specify five ys with list()", {
  skip_on_os(os = "linux")
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_warning(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = list(
        speed_mph, gyro_heading,
        gps_heading, gps_pdop,
        gps_hdop
      )
    )
  )
})

test_that("provide two ys and one label", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = list(speed_mph, gyro_heading),
      ylabels = "Speed"
    ),
    "If providing `ylabels`, `ys` and `ylabels` must be the same length."
  )
})

test_that("provide only one y label without c() or list()", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_mph,
      ylabels = "Speed"
    )
  )
})

test_that("provide only one y label with c()", {
  skip_on_os(os = "linux")
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_mph,
      ylabels = c("Speed")
    )
  )
})

test_that("provide only one y label with list()", {
  skip_on_os(os = "linux")
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = speed_mph,
      ylabels = list("Speed")
    )
  )
})

test_that("specify two y labels with c()", {
  skip_on_os(os = "linux")
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph, gyro_heading),
      ylabels = c("Speed", "Gyro Heading")
    )
  )
})

test_that("specify two y labels with list()", {
  skip_on_os(os = "linux")
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph, gyro_heading),
      ylabels = list("Speed", "Gyro Heading")
    )
  )
})

test_that("specify ylabels without quotes", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph, gyro_heading),
      ylabels = list(Speed, Heading)
    )
  )
})

test_that("NA ylabel", {
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph, gyro_heading),
      ylabels = NA
    ),
    "`ylabels` cannot be NA."
  )
})


test_that("can modify function arguments", {
  skip_on_os(os = "linux")
  shared_drive <- crosstalk::SharedData$new(drive7)
  # Modify x
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = as.POSIXct(time_cst, tz = "UTC"),
      ys = c(speed_mph, gyro_heading)
    )
  )
  # Modify one y
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph * 1.609, gyro_heading)
    )
  )
  # Modify two ys
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = c(speed_mph * 1.609, gyro_heading %% 360)
    )
  )
  # Modify colorvar and maplabel
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = list(speed_mph, gps_pdop),
      colorvar = gyro_heading %% 360,
      maplabel = gyro_heading %% 360,
      colorpalette = "viridis"
    )
  )
  # Many modifications
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = as.POSIXct(time_cst, tz = "UTC"),
      ys = list(speed_mph * 1.609, gyro_heading %% 360),
      colorvar = paste("Minute:", gps_minute),
      maplabel = paste("Minute:", gps_minute),
      colorpalette = "viridis"
    )
  )
})

test_that("add a title to the plot map", {
  skip_on_os(os = "linux")
  shared_drive <- crosstalk::SharedData$new(drive7)
  expect_no_error(
    driveplot(
      shareddata = shared_drive,
      x = time_cst,
      ys = list(speed_mph, gyro_heading),
      plottitle = "Speed (MPH) and Gyro Heading for a Drive"
    )
  )
})

test_that("plot map has htmlwidget class", {
  skip_on_os(os = "linux")
  shared_drive <- crosstalk::SharedData$new(drive7)
  testplotmap <- driveplot(
    shareddata = shared_drive,
    x = time_cst,
    ys = list(speed_mph, gyro_heading),
    plottitle = "Speed (MPH) and Gyro Heading for a Drive"
  )
  expect_s3_class(testplotmap, class = "htmlwidget")
})

Try the DrivePlotR package in your browser

Any scripts or data that you put into this service are public.

DrivePlotR documentation built on Nov. 22, 2026, 5:06 p.m.