Setting editable options for individual items in Timevis when using R/Shiny

195 Views Asked by At

I am using the Timevis package to create a timeline in R/Shiny. I would like to set specific editability options for individual items. Some items should be able to move between groups (but not in time), and some items should not be editable at all. I did find this previous question: Setting editable options in Timevis when using R/Shiny?, but it does not mention how to set the options for individual items.

At https://search.r-project.org/CRAN/refmans/timevis/html/timevis.html I found that the underlying Javascript options can be set using the htmlwidgets::JS() command. Using this command I can set the options for the entire timeline as follows:

server <- function(input, output, session)
{
  
  # render timeline
  output$timeline <- renderTimevis({
    tv <- timevis( data = timevisData, groups = timevisDataGroups, options=list(editable = htmlwidgets::JS("{updateGroup: true, updateTime: false }")))
  })
  
  
  timevisDataGroups <- data.frame(id=c("group 1", "group 2"),
                                  content = c("Group 1", "Group 2")
  )
  
  timevisData <- data.frame(id = 1:2,
                            content = c("name 1", "name 2"),
                            start = c("2022-01-01", "2022-01-05"),
                            end = c("2022-01-03", "2022-01-06"),
                            group = c("group 1", "group 2"),
                            type = c("range", "range"))
  
}

However, this does not allow me to distinguish between different items. I tried to set the options for individual items as follows:

server <- function(input, output, session)
{

  # render timeline
  output$timeline <- renderTimevis({
    tv <- timevis( data = timevisData, groups = timevisDataGroups, options=list(editable = TRUE))
  })


  timevisDataGroups <- data.frame(id=c("group 1", "group 2"),
                                  content = c("Group 1", "Group 2")
  )

  timevisData <- data.frame(id = 1:2,
               content = c("name 1", "name 2"),
               start = c("2022-01-01", "2022-01-05"),
               end = c("2022-01-03", "2022-01-06"),
               group = c("group 1", "group 2"),
               options = c(htmlwidgets::JS("editable: {updateGroup: true, updateTime: false }"), htmlwidgets::JS("editable: {updateGroup: false, updateTime: false }")),
               type = c("range", "range"))
}

However, the item specific options do not seem to have any effect.

1

There are 1 best solutions below

2
On

I was also stuck in a similar problem. I found addItem function of timevis as an alternative way to create items and add them to an empty timevis object.

Inspired by your example :

timevisDataGroups <- data.frame(id = c("group 1", "group 2"),
                                content = c("Group 1", "Group 2"))

timevisData <- data.frame(
  id = 1:2,
  content = c("name 1", "name 2"),
  start = c("2022-01-01", "2022-01-05"),
  end = c("2022-01-03", "2022-01-06"),
  group = c("group 1", "group 2"),
  type = c("range", "range")
)


tv <- timevis::timevis(groups = timevisDataGroups,
                       options = list(
                         editable = list(
                           updateTime = FALSE, #set default 
                           updateGroup = TRUE, #set default 
                           overrideItems = FALSE #to allow item-specific configuration to override the default
                         )
                       )
) %>%
  timevis::addItem(
    apply(timevisData, 1, function(item) {
      editable = if(item["start"] < as.Date("2022-01-04")){
        editable = list(
          updateGroup = FALSE,
          updateTime = FALSE
        )
      } else {
        editable = list(
          updateGroup = TRUE,
          updateTime = FALSE
        )
      }
      as.list(c(item, list(editable = editable)))
    })
  ) %>%
  timevis::fitWindow()

Using addItem enables creating an item using a list (and not a data.frame) to specify remove, updateGroup and updateTime parameters (see timeline javascript documentation: https://visjs.github.io/vis-timeline/docs/timeline/) With item["start"] > as.Date("2022-01-04") rule I made up, it should enable you to move name 1 from one group to another.

NB : According to timeline R package documentation, you should be able to add a column named "editable" in the timevisData to specify editable for each item (but without controlling the state of remove, updateGroup and updateTime separately) but for some reason it does not work for me. It could be a bug to report.