daattali / timevis

📅 Create interactive timeline visualizations in R
http://daattali.com/shiny/timevis-demo/
Other
650 stars 157 forks source link

Avoid converting logical vectors to character #117

Closed mhtorp closed 2 years ago

mhtorp commented 2 years ago

This fix allows specifying subgroupStack = TRUE or subgroupStack = list(subgroup_id = TRUE) in the groups data.frame.

Current behaviour converts subgroupStack = "TRUE" to {subgroupStack: {0: "T", 1: "R", 2: "U", 3: "E"}}

daattali commented 2 years ago

Is there an open issue that corresponds to this or is this a new issue?

mhtorp commented 2 years ago

This is a new issue I just discovered

daattali commented 2 years ago

Can you please share an example (for testing) with the full code of a timeline that uses this feature?

mhtorp commented 2 years ago

Here's a reprex that works using my fork and not using the current release.

## Example from https://visjs.github.io/vis-timeline/examples/timeline/groups/subgroups.html

library(jsonlite)
library(shiny)
library(timevis)

ui <- fluidPage(
    titlePanel("Timevis stacking bug"),

    sidebarLayout(
        sidebarPanel(
            checkboxInput("stack", "Stack nodes")
        ),
        mainPanel(
           timevis::timevisOutput("timeline")
        )
    )
)

vis_nodes <- jsonlite::parse_json('[
  {
    "id": "A",
    "start": "2014-01-20",
    "end": "2014-01-22",
    "type": "background",
    "group": "foo"
  },
  {
    "id": "B",
    "start": "2014-01-22",
    "end": "2014-01-23",
    "type": "background",
    "group": "foo",
    "className": "negative"
  },
  {
    "id": 0,
    "content": "no subgroup",
    "start": "2014-01-20",
    "end": "2014-01-22",
    "group": "foo"
  },
  {
    "id": "SG_1_1",
    "start": "2014-01-25",
    "end": "2014-01-27",
    "type": "background",
    "group": "bar",
    "subgroup": "sg_1",
    "subgroupOrder": 0
  },
  {
    "id": "SG_1_2",
    "start": "2014-01-26",
    "end": "2014-01-27",
    "type": "background",
    "className": "positive",
    "group": "bar",
    "subgroup": "sg_1",
    "subgroupOrder": 0
  },
  {
    "id": 1,
    "content": "subgroup0_1",
    "start": "2014-01-23T12:00:00",
    "end": "2014-01-26T12:00:00",
    "group": "bar",
    "subgroup": "sg_1",
    "subgroupOrder": 0
  },
  {
    "id": 2,
    "content": "subgroup0_2",
    "start": "2014-01-22T12:00:01",
    "end": "2014-01-25T12:00:00",
    "group": "bar",
    "subgroup": "sg_1",
    "subgroupOrder": 0
  },
  {
    "id": "SG_2_1",
    "start": "2014-02-01",
    "end": "2014-02-02",
    "type": "background",
    "group": "bar",
    "subgroup": "sg_2",
    "subgroupOrder": 1
  },
  {
    "id": "SG_2_2",
    "start": "2014-02-2",
    "end": "2014-02-03",
    "type": "background",
    "className": "negative",
    "group": "bar",
    "subgroup": "sg_2",
    "subgroupOrder": 1
  },
  {
    "id": 3,
    "content": "subgroup1_1",
    "start": "2014-01-27T02:00:00",
    "end": "2014-01-29",
    "group": "bar",
    "subgroup": "sg_2",
    "subgroupOrder": 1
  },
  {
    "id": 4,
    "content": "subgroup1_2",
    "start": "2014-01-28",
    "end": "2014-02-02",
    "group": "bar",
    "subgroup": "sg_2",
    "subgroupOrder": 1
  },
  {
    "id": "SG_3_1",
    "start": "2014-01-23",
    "end": "2014-01-25",
    "type": "background",
    "group": "bar",
    "subgroup": "sg_3",
    "subgroupOrder": 2,
    "content": "a"
  },
  {
    "id": "SG_3_2",
    "start": "2014-01-26",
    "end": "2014-01-28",
    "type": "background",
    "className": "positive",
    "group": "bar",
    "subgroup": "sg_3",
    "subgroupOrder": 2,
    "content": "b"
  },
  {
    "id": 5,
    "content": "subgroup2_1",
    "start": "2014-01-23T12:00:00",
    "end": "2014-01-26T12:00:00",
    "group": "bar",
    "subgroup": "sg_3",
    "subgroupOrder": 2
  },
  {
    "id": 6,
    "content": "subgroup2_2",
    "start": "2014-01-26T12:00:01",
    "end": "2014-01-29T12:00:00",
    "group": "bar",
    "subgroup": "sg_3",
    "subgroupOrder": 2
  },
  {
    "id": "background",
    "start": "2014-01-29",
    "end": "2014-01-30",
    "type": "background",
    "className": "negative",
    "group": "bar"
  },
  {
    "id": "background_all",
    "start": "2014-01-31",
    "end": "2014-02-02",
    "type": "background",
    "className": "positive"
  }
]', simplifyVector = TRUE)

vis_groups <- jsonlite::parse_json('[
  {
    "id": "bar", "content":"bar", "subgroupStack": true
  },{
    "id": "foo", "content": "foo", "subgroupStack": true
  }
]', simplifyVector = TRUE)

server <- function(input, output) {

    output$timeline <- timevis::renderTimevis({
      timevis::timevis(
        data = vis_nodes,
        groups = vis_groups,
        options = list(
          stack = input$stack,
          stackSubgroups = TRUE
        )
      )
    })
}

# Run the application
shinyApp(ui = ui, server = server)
daattali commented 2 years ago

Thanks for the great code, added!