R/tw_image_grid.R

Defines functions tw_image_grid_server tw_image_grid_ui

Documented in tw_image_grid_server tw_image_grid_ui

#' Trailers Widget Image Grid UI
#'
#'
#' The image grid shows the examples of the styles selected from the
#' csv passed into the environment
#'
#' @param id The id of the image grid UI
#'
#' @return html code for the UI of the shiny app
#'
#'@importFrom shiny uiOutput
#'
#'@export
tw_image_grid_ui <- function(id) {
  ns <- NS(id)
  uiOutput(ns("image_grid"))
}
#' Trailers Widget Image Grid Server
#'
#'
#' @description The image grid module used to display the
#' styles selected in the trailers widget app
#'
#' @param input list of inputs used in the shiny application session
#' @param output list of outputs used the shiny application session
#' @param session The shiny app session object
#' @param base_data The  dataframe reactive function from the trailers app that adds in styles
#'
#'@importFrom shiny tabsetPanel
#'@importFrom shiny tabPanel
#'@importFrom shiny fluidRow
#'
#'@export
#'
#'
tw_image_grid_server <- function(input, output, session, base_data) {

  output[["image_grid"]] <- renderUI({
    #Make sure that the data is not empty
    req(base_data()[1, 1] != 0)
    #Get the data for the styles
    df <- base_data()
    # First make the cards
    #Go column by column to determine which images to print out
    do.call(shiny::tabsetPanel,
            #Creating the first tab panel according to the first segment in the template
            lapply(1:length(df), function(a) {
              #Grab every style for a corresponding segment
              int_df <- df[, a]
              #Create each of the cards which includes the position of the image
              args <-
                lapply(1:dim(int_df)[1], function(x)
                  item(
                    img = paste0('https://i1.adis.ws/i/harryrosen/', int_df[x, 1], '?h=288'),
                    pos = x
                  ))

              # Make sure to add other arguments to the list:
              args$cellArgs <- list(style = "
            width: 200px;
            height: auto;
            margin: 5px;
            ")

              #Create the columns according to the location of the items
              cols <-
                lapply(1:4, function(x) {
                  #Select each columns' index of styles
                  idx <- x
                  if (nrow(base_data()) > 4)
                    idx <- c(x, x + 4)
                  if (nrow(base_data()) > 8)
                    idx <- c(x, x + 4, x + 8)
                  if (nrow(base_data()) > 12)
                    idx <- c(x, x + 4, x + 8, x + 12)

                  shiny::column(width = 2, verticalLayout(args[idx], fluid = TRUE))
                })

              # then use fluidRow to arrange the columns
              #used the naming convention of their original names (ie luxury, brand_focus, work_wear, general)
              shiny::tabPanel(tolower(colnames(int_df)), do.call(shiny::fluidRow, cols))
            }))
  })


}
HarryRosen/hrimodules documentation built on Jan. 11, 2022, 12:36 a.m.