0

I have been trying to get my renderUI code to respond to slick.js implementation shared by @Stéphane Laurent.

Basically I have modules that create tables. I want the user to select the number of tables to be displayed. But I want those tables to be scrollable and layerd ontop of each other

here is reproducible example that should work on your machine

chartTableBoxUI <- function(id) {

  ns <- NS(id)


  div(
    tags$div(DTOutput(ns("chart"))),
    tags$div(DTOutput(ns("table")))

  )



}

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

  ns <- session$ns

  vals <- reactiveValues()

  observeEvent(input$chart_rows_selected,{

    vals$sel<- (input$chart_rows_selected)


  })

  output$chart <- renderDT({
    DT::datatable(
      mtcars,options = list(
        dom='t', pageLength = 5)

    )
  })

  output$table <- renderDT({


    DT::datatable(
      mtcars[vals$sel, 1:3],options = list(dom='t')
    )




  })

}

library(shiny)
library(shinydashboard)
library(tidyverse)
library(highcharter)
library(DT)
library(shinyjs)

ui <- fluidPage(

  fluidRow(
    tags$head(
      tags$link(rel="stylesheet", type="text/css",
                href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick-theme.css"),
      tags$link(rel="stylesheet", type="text/css",
                href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.css"),
      tags$script(type="text/javascript", 
                  src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.js"),
      tags$script(HTML(
        "$(document).ready(function(){
        $('#tables').slick({
        arrows: true,
        dots:true
        });
        });")),
    tags$style(HTML(
      "#tables .slick-prev {
      position:absolute;
      top:65px; 
      left:-100px;
      }
      #tables .slick-next {
      position:absolute;
      top:95px; 
      left:-100px;
      }
      .slick-prev:before, .slick-next:before { 
      color:red !important;
      }
      .content {
      margin: auto;
      padding: 20px;
      width: 80%;
      }"))
  ),



  sliderInput("dr", "Num of tables:",
              min = 0, max = 12,
              value = 2),
  uiOutput("tabs")
  #verbatimTextOutput("dr2")





    )

    )





server <- function(input, output, session) {
  for(i in 1:5)
    callModule(chartTableBox,i)

  output$tabs <- renderUI({
     num_tables<- input$dr


    tags$div(class="content",
             tags$div(id="tables",
                      lapply(1:num_tables,chartTableBoxUI)



             ))


  })





}

shinyApp(ui, server)
1
  • You mean the slideshow does not work ? Commented Jun 9, 2019 at 11:46

1 Answer 1

1

Here is a solution with a MutationObserver. It observes whether there's a change in the container and then it applies slick().

chartTableBoxUI <- function(id) {

  ns <- NS(id)
  div(
    h2(id),
    tags$div(DTOutput(ns("chart"))),
    tags$div(DTOutput(ns("table")))
  )

}

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

  ns <- session$ns

  vals <- reactiveValues()

  observeEvent(input$chart_rows_selected, {
    vals$sel<- input$chart_rows_selected
  }, ignoreNULL = FALSE)

  output$chart <- renderDT({
    datatable(
      mtcars, options = list(dom='t', pageLength = 5), class = "compact stripe"
    )
  })

  output$table <- renderDT({
    datatable(
      mtcars[vals$sel, 1:3], options = list(dom='t'), class = "compact stripe"
    )
  })

}

library(shiny)
library(DT)
library(shinyjs)

js <- "
$(document).ready(function(){
    var container = document.getElementById('tabs');
    // create an observer instance
    var observer = new MutationObserver(function(mutations) {
      setTimeout(function(){
        $('#tables').slick({
          arrows: true,
          dots: true
        });
      }, 0);
    });
    // observe 
    observer.observe(container, {subtree: false, childList: true});
});
"
ui <- fluidPage(

  fluidRow(
    tags$head(
      tags$link(rel="stylesheet", type="text/css",
                href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick-theme.css"),
      tags$link(rel="stylesheet", type="text/css",
                href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.css"),
      tags$script(type="text/javascript", 
                  src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.9.0/slick.js"),
      tags$script(HTML(js)),
      tags$style(HTML(
        "#tables .slick-prev {
      position:absolute;
      top:65px; 
      left:-100px;
      }
      #tables .slick-next {
      position:absolute;
      top:95px; 
      left:-100px;
      }
      .slick-prev:before, .slick-next:before { 
      color:red !important;
      }
      .content {
      margin: auto;
      padding: 20px;
      width: 80%;
      }"))
    ),

    sliderInput("dr", "Num of tables:", min = 0, max = 12, value = 2),
    div(style = "margin-top: -60px"),
    uiOutput("tabs")

  )

)

server <- function(input, output, session) {
  for(i in 1:12)
    callModule(chartTableBox, paste0("Table",i))

  output$tabs <- renderUI({
    num_tables <- input$dr
    tags$div(class="content",
             tags$div(id="tables",
                      lapply(paste0("Table",1:num_tables), chartTableBoxUI)
             ))
  })

}

shinyApp(ui, server)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.