daattali / timevis

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

addItem does not work in modular shiny app #92

Closed am2222 closed 4 years ago

am2222 commented 4 years ago

Hi, In a modular shiny app we define the ids by a ns(id) function which gives us the id of the html element based on namespace in a format of namespace-id. this causes a problem in some functions such as addItem which gets the id of the html object. I tried to use namespace-id as well but it seems it is not working, is there any workaround for it?

daattali commented 4 years ago

Could you please include a small reproducible example so I can test

daattali commented 4 years ago

All functions should work in modules, here's an example of using additem in a module

library(shiny)
library(timevis)

timevis_ui <- function(id) {
  ns <- NS(id)
  tagList(
    timevisOutput(ns("timevisui")),
    actionButton(ns("add"), "Add")
  )
}
timevis_server <- function(input, output, session) {
  output$timevisui <- renderTimevis({
    timevis()
  })
  observeEvent(input$add, {
    addItem("timevisui", list(start = Sys.Date(), content = "Today"))
  })
}

ui <- fluidPage(
  timevis_ui("test")
)
server <- function(input, output, session) {
  callModule(timevis_server,  "test")
}
shinyApp(ui, server)

I'm closing this issue for now. If you still think something isn't working, feel free to open a new issue and please include sample code

am2222 commented 4 years ago

@daattali Thanks for the example, But in your example the add button adds a line on the current date time which there is one by default in the current date and time. If I change the start to Sys.Date()-2 it does not add anything

library(shiny)
library(timevis)

timevis_ui <- function(id) {
  ns <- NS(id)
  tagList(
    timevisOutput(ns("timevisui")),
    actionButton(ns("add"), "Add")
  )
}
timevis_server <- function(input, output, session) {
  output$timevisui <- renderTimevis({
    timevis()
  })
  observeEvent(input$add, {
    addItem("timevisui", list(start = Sys.Date()-2, content = "Two days ago"))
  })
}

ui <- fluidPage(
  timevis_ui("test")
)
server <- function(input, output, session) {
  callModule(timevis_server,  "test")
}
shinyApp(ui, server)