diff --git a/R/utils.R b/R/utils.R index 5ebecd2ca7..32587eef3e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1021,7 +1021,8 @@ try_file <- function(f, what) { # preferred defaults for toJSON mapping to_JSON <- function(x, ...) { jsonlite::toJSON(x, digits = 50, auto_unbox = TRUE, force = TRUE, - null = "null", na = "null", ...) + null = "null", na = "null", + time_format = "%Y-%m-%d %H:%M:%OS6", ...) } # preferred defaults for toJSON mapping diff --git a/tests/figs/lines/line-milliseconds.svg b/tests/figs/lines/line-milliseconds.svg new file mode 100644 index 0000000000..bd9c39f10b --- /dev/null +++ b/tests/figs/lines/line-milliseconds.svg @@ -0,0 +1 @@ +00:00:00Jan 1, 197000:00:1500:00:3000:00:4500:01:0000:01:1500:01:30−1−0.500.51ty diff --git a/tests/testthat/test-ggplot-lines.R b/tests/testthat/test-ggplot-lines.R index e0d437985a..1fe3feb455 100644 --- a/tests/testthat/test-ggplot-lines.R +++ b/tests/testthat/test-ggplot-lines.R @@ -41,7 +41,19 @@ test_that("different colored lines become different colored traces", { expect_identical(info$data[[2]]$x[1:n], x) }) - +test_that("Milliseconds are preserved with dynamic ticks", { + d <- data.frame( + t = as.POSIXct("1970-01-01 00:00") + (0:999) / 10, + y = sin((0:999) * 4 * pi / 1000) + ) + gg <- ggplot(d, aes(t, y)) + geom_line() + p <- ggplotly(gg, dynamicTicks = TRUE) + j <- plotly_json(p, jsonedit = FALSE) + t2 <- jsonlite::fromJSON(j)$data$x[[1]] %>% + as.POSIXct(format = "%Y-%m-%d %H:%M:%OS") + expect_equal(as.numeric(mean(diff(t2))), 0.1) + expect_doppelganger_built(p, "line-milliseconds") +}) test_that("Translates both dates and datetimes (with dynamic ticks) correctly", {