From 11908ffe13fa4c247848e5fe5428d645152446dc Mon Sep 17 00:00:00 2001 From: Max Czapanskiy Date: Sun, 25 Oct 2020 20:30:55 -0700 Subject: [PATCH 1/5] Fix #1870 by explicitly including milliseconds when converting to JSON --- R/utils.R | 3 ++- tests/testthat/test-ggplot-lines.R | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index 5ebecd2ca7..20a37a46ce 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-%dT%H:%M:%OS3%z", ...) } # preferred defaults for toJSON mapping diff --git a/tests/testthat/test-ggplot-lines.R b/tests/testthat/test-ggplot-lines.R index e0d437985a..9d41056ee4 100644 --- a/tests/testthat/test-ggplot-lines.R +++ b/tests/testthat/test-ggplot-lines.R @@ -41,7 +41,18 @@ 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-%dT%H:%M:%OS%z") + expect_equal(as.numeric(mean(diff(t2))), 0.1) +}) test_that("Translates both dates and datetimes (with dynamic ticks) correctly", { From 83c0ee751c07593ccdd07cad1cd66a8887db8e60 Mon Sep 17 00:00:00 2001 From: Max Czapanskiy Date: Sun, 25 Oct 2020 20:55:56 -0700 Subject: [PATCH 2/5] Added visual test for #1870 --- tests/figs/lines/line-milliseconds.svg | 1 + tests/testthat/test-ggplot-lines.R | 1 + 2 files changed, 2 insertions(+) create mode 100644 tests/figs/lines/line-milliseconds.svg diff --git a/tests/figs/lines/line-milliseconds.svg b/tests/figs/lines/line-milliseconds.svg new file mode 100644 index 0000000000..8e61eec881 --- /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 9d41056ee4..a4cf539f3b 100644 --- a/tests/testthat/test-ggplot-lines.R +++ b/tests/testthat/test-ggplot-lines.R @@ -52,6 +52,7 @@ test_that("Milliseconds are preserved with dynamic ticks", { t2 <- jsonlite::fromJSON(j)$data$x[[1]] %>% as.POSIXct(format = "%Y-%m-%dT%H:%M:%OS%z") 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", { From 6a8a6865ef88061c8c4331773d7ede0c50770961 Mon Sep 17 00:00:00 2001 From: Max Czapanskiy <47009796+FlukeAndFeather@users.noreply.github.com> Date: Mon, 26 Oct 2020 14:28:38 -0700 Subject: [PATCH 3/5] Fix POSIXt format for plotly JSON MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to plotly documentation (https://plotly.com/chart-studio-help/date-format-and-time-series/): "Chart Studio’s date format is 'yyyy-mm-dd HH:MM:SS.ssssss'.", so time_format in jsonlite::toJSON() changed to match. --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 20a37a46ce..32587eef3e 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1022,7 +1022,7 @@ try_file <- function(f, what) { to_JSON <- function(x, ...) { jsonlite::toJSON(x, digits = 50, auto_unbox = TRUE, force = TRUE, null = "null", na = "null", - time_format = "%Y-%m-%dT%H:%M:%OS3%z", ...) + time_format = "%Y-%m-%d %H:%M:%OS6", ...) } # preferred defaults for toJSON mapping From 2569d03c9b9f2bc0f6c4c12e226d874819133858 Mon Sep 17 00:00:00 2001 From: Max Czapanskiy Date: Thu, 29 Oct 2020 11:15:39 -0700 Subject: [PATCH 4/5] Fixing time format in test re: #1871 --- tests/testthat/test-ggplot-lines.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-ggplot-lines.R b/tests/testthat/test-ggplot-lines.R index a4cf539f3b..1fe3feb455 100644 --- a/tests/testthat/test-ggplot-lines.R +++ b/tests/testthat/test-ggplot-lines.R @@ -50,7 +50,7 @@ test_that("Milliseconds are preserved with dynamic ticks", { p <- ggplotly(gg, dynamicTicks = TRUE) j <- plotly_json(p, jsonedit = FALSE) t2 <- jsonlite::fromJSON(j)$data$x[[1]] %>% - as.POSIXct(format = "%Y-%m-%dT%H:%M:%OS%z") + as.POSIXct(format = "%Y-%m-%d %H:%M:%OS") expect_equal(as.numeric(mean(diff(t2))), 0.1) expect_doppelganger_built(p, "line-milliseconds") }) From c81d65796b65bc7264738cc1350b8103785ec673 Mon Sep 17 00:00:00 2001 From: Max Czapanskiy Date: Wed, 4 Nov 2020 11:05:22 -0800 Subject: [PATCH 5/5] Update test image for line-milliseconds.svg --- tests/figs/lines/line-milliseconds.svg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/figs/lines/line-milliseconds.svg b/tests/figs/lines/line-milliseconds.svg index 8e61eec881..bd9c39f10b 100644 --- a/tests/figs/lines/line-milliseconds.svg +++ b/tests/figs/lines/line-milliseconds.svg @@ -1 +1 @@ -00:00:00Jan 1, 197000:00:1500:00:3000:00:4500:01:0000:01:1500:01:30−1−0.500.51ty +00:00:00Jan 1, 197000:00:1500:00:3000:00:4500:01:0000:01:1500:01:30−1−0.500.51ty