I am using R and ojs code chunks in a quarto document where I do data manipulation in R and the pass the data frame to the ojs chunk by using the ojs_define
function.
My issue is that date seems to be interpreted as string in ojs chunk. The result is bad formatting in plots.
Any tips on how to pass a date from r to ojs in a way that the ojs plot function recognizes it as a date?
Example:
---
title: "Code example"
format: html
editor: visual
execute:
echo: false
warning: false
---
```{r}
#Some data with a date column
library(tidyverse)
df<-data.frame(day=seq.Date(from=as.Date('2023-01-01'),
to=as.Date('2023-06-15'),
by='days'))%>%
mutate(values=rnorm(mean= 10, sd=2, n =n()))
#Passing this to ojs through ojs_define
ojs_define(plot_data=df)
```
```{ojs}
Chart = Plot.plot({
marks: [
Plot.line(transpose(plot_data),
{x: "day", y: "values"},
{ stroke: "black" }
)
]}
)
```
Since dates are being parsed as string when passed through the
ojs_define
, we can transform back the string-dates type to datetime type in the ojs chunk.Here we will use
d3.timeParse
to create a datetime parser and will use theArquero
library (inspired by the design ofdplyr
) toderive
(i.e mutate) theday
column to have datetime values.Two things to note here,
Arquero
work with data tables. But theplot_data
is of JSON format. So we need totranspose
it to convert it to an array of objects which then passed toaq.from
to convert as table.To use that
parser
withinderive
, we need to wrapd => parse(d)
withaq.escape