Although sortable
is designed to be a low-level wrapper
around the SortableJS
library, the package also exposes a
few higher level functions.
These functions enable you to easily using drag-and-drop widgets into a Shiny app for specific tasks:
rank_list()
bucket_list()
This is a rank list app, allowing you to change the order of items in a list. The app demonstrates three types of drag-and-drop behaviour:
This is the source code:
## Example shiny app with rank list
library(shiny)
library(sortable)
<- list(
labels "one",
"two",
"three",
::tags$div(
htmltools::em("Complex"), " html tag without a name"
htmltools
),"five" = htmltools::tags$div(
::em("Complex"), " html tag with name: 'five'"
htmltools
)
)
<- rank_list(
rank_list_basic text = "Drag the items in any desired order",
labels = labels,
input_id = "rank_list_basic"
)
<- rank_list(
rank_list_swap text = "Notice that dragging causes items to swap",
labels = labels,
input_id = "rank_list_swap",
options = sortable_options(swap = TRUE)
)
<- rank_list(
rank_list_multi text = "You can select multiple items, then drag as a group",
labels = labels,
input_id = "rank_list_multi",
options = sortable_options(multiDrag = TRUE)
)
<- fluidPage(
ui fluidRow(
column(
width = 12,
$h2("Default, multi-drag and swapping behaviour"),
tagstabsetPanel(
type = "tabs",
tabPanel(
"Default",
$b("Exercise"),
tags
rank_list_basic,$b("Result"),
tagsverbatimTextOutput("results_basic")
),tabPanel(
"Multi-drag",
$b("Exercise"),
tags
rank_list_multi,$b("Result"),
tagsverbatimTextOutput("results_multi")
),tabPanel(
"Swap",
$b("Exercise"),
tags
rank_list_swap,$b("Result"),
tagsverbatimTextOutput("results_swap")
)
)
)
)
)
<- function(input, output, session) {
server $results_basic <- renderPrint({
output$rank_list_basic # This matches the input_id of the rank list
input
})$results_multi <- renderPrint({
output$rank_list_multi # This matches the input_id of the rank list
input
})$results_swap <- renderPrint({
output$rank_list_swap # This matches the input_id of the rank list
input
})
}
shinyApp(ui, server)
This is a bucket list app, where the bucket list allows you to drag from one bucket to another.
This is the source code:
## Example shiny app with bucket list
library(shiny)
library(sortable)
<- fluidPage(
ui $head(
tags$style(HTML(".bucket-list-container {min-height: 350px;}"))
tags
),fluidRow(
column(
$b("Exercise"),
tagswidth = 12,
bucket_list(
header = "Drag the items in any desired bucket",
group_name = "bucket_list_group",
orientation = "horizontal",
add_rank_list(
text = "Drag from here",
labels = list(
"one",
"two",
"three",
::tags$div(
htmltools::em("Complex"), " html tag without a name"
htmltools
),"five" = htmltools::tags$div(
::em("Complex"), " html tag with name: 'five'"
htmltools
)
),input_id = "rank_list_1"
),add_rank_list(
text = "to here",
labels = NULL,
input_id = "rank_list_2"
)
)
)
),fluidRow(
column(
width = 12,
$b("Result"),
tagscolumn(
width = 12,
$p("input$rank_list_1"),
tagsverbatimTextOutput("results_1"),
$p("input$rank_list_2"),
tagsverbatimTextOutput("results_2"),
$p("input$bucket_list_group"),
tagsverbatimTextOutput("results_3")
)
)
)
)
<- function(input, output, session) {
server $results_1 <-
outputrenderPrint(
$rank_list_1 # This matches the input_id of the first rank list
input
)$results_2 <-
outputrenderPrint(
$rank_list_2 # This matches the input_id of the second rank list
input
)$results_3 <-
outputrenderPrint(
$bucket_list_group # Matches the group_name of the bucket list
input
)
}
shinyApp(ui, server)