library(slickR)
library(svglite)
library(gdtools)
library(shiny)
The Following example is a Shiny App that creates
plot_num
histograms from rnorm
with
n_obs
observations. These plots are placed in a slick
carousel.
The use can choose between the different slick layouts
<- fluidPage(
ui sidebarLayout(
sidebarPanel(
sliderInput(inputId = "plot_num",
label = "Number of Plots:",
min = 1, max = 20, value = 5),
sliderInput(inputId = "n_obs",
label = "Number of observations:",
min = 10, max = 500, value = 100),
::radioButtons('slick_type',
shinylabel = 'Carousel Type',
choices = c('single','stack','synch'),
selected = 'single',
inline = TRUE),
::verbatimTextOutput('current')
shiny
),mainPanel(
slickROutput("slick_output",width='100%',height='200px')
)
) )
<- function(input, output) {
server
# Create content for the carousel
<- eventReactive(c(input$n_obs,input$plot_num),{
plots
replicate(input$plot_num,{
xmlSVG({hist(rnorm(input$n_obs),
col = 'darkgray',
border = 'white')},
standalone=TRUE)
simplify = FALSE)
},
})
# renderSlickR (We create the slickR objects here)
$slick_output <- renderSlickR({
output
<- slickR(plots(),
x slideId = 'slick1',
height = 600,
width = '50%') +
settings(slidesToShow=3,centerMode=TRUE)
<- slickR(plots(),
y slideId = 'slick2',
height = 600,
width = '50%') +
settings(slidesToShow=3,centerMode=TRUE)
switch(input$slick_type,
'single' = x,
'stack' = x %stack% y,
'synch' = x %synch% y
)
})
# Observe the active slick
# The htmlwidget is observed by shiny and information can be retrieved.
# Using the output name you set for the `renderSlick` object in this example
# it is `output$slick_output`
# Using this you can interact server-side "on click" of the active carousel
# by accessing elements in `input$slick_output_current$`
# `.clicked` : The index of the clicked element|
# `.relative_clicked`: The relative position of the clicked element|
# `.center` : The index of the center element|
# `.total` : The total number of elements in the carousel|
# `.active` : The ID of the active carousel|
# We will store this information in a new reactive environment
<- shiny::reactiveValues()
active_slick
::observeEvent(input$slick_output_current,{
shiny$clicked <- input$slick_output_current$.clicked
active_slick$relative_clicked <- input$slick_output_current$.relative_clicked
active_slick$center <- input$slick_output_current$.center
active_slick$total <- input$slick_output_current$.total
active_slick$active <- input$slick_output_current$.slide
active_slick
})
# Show in the UI the values in active_slick
$current <- renderText({
output<- unlist(shiny::reactiveValuesToList(active_slick))
l paste(gsub('_',' ', names(l)), l, sep=' = ', collapse='\n')
})
}
shinyApp(ui = ui, server = server)