Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to update rank_list labels and exports update_rank_list #95

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
95d0fd0
Add missing `@examples` to silence roxygen2 warning
yogat3ch Apr 8, 2023
3d37c7c
`update_rank_list`: add `labels` argument for updating labels, namesp…
yogat3ch Apr 8, 2023
32a1655
Add `update-labels` message handler
yogat3ch Apr 8, 2023
2448b17
Modify update example to demo label update
yogat3ch Apr 8, 2023
ee8a459
Docs & namespace
yogat3ch Apr 8, 2023
526e1a1
Add `labels` argument to `update_*` methods, remove `ns` of inputId
yogat3ch Apr 10, 2023
53033a6
Update Docs
yogat3ch Apr 10, 2023
fdd7092
Remove custom messsage handler
yogat3ch Apr 10, 2023
5de5d68
Create `setValue` methods for `rank/buckerlistBinding`s
yogat3ch Apr 10, 2023
ad609f2
handle case when rank_list starts with no labels
yogat3ch Apr 20, 2023
559db6c
docs: add missing vignette example code #97
andrie May 24, 2023
cbcb580
Merge branch 'update_rank_list-labels' of https://github.com/yogat3ch…
andrie May 24, 2023
af3acd7
Merge branch 'rstudio:main' into update_rank_list-labels
yogat3ch Jun 1, 2023
5c9f4f7
Add debuggers to methods, use module
yogat3ch Jun 1, 2023
8857ddf
Add helper methods to create the ids for list containers
schloerke Jun 5, 2023
f22c489
Use new list names and namespace the css id values
schloerke Jun 5, 2023
133b879
Update module to leverage `ns`
schloerke Jun 5, 2023
2d309a2
Use two modules to show they do not cross
schloerke Jun 5, 2023
ec083cb
Update NEWS.md
schloerke Jun 5, 2023
1e86780
Remove debugger
schloerke Jun 5, 2023
0e99a9a
Leverage R folder to auto source file. Use library in app
schloerke Jun 5, 2023
82aa158
Update README.md
schloerke Jun 5, 2023
5825c6b
Update NEWS.md
schloerke Jun 5, 2023
5b75f11
Merge branch 'main' into update_rank_list-labels
schloerke Jun 5, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(sortable_js_capture_input)
export(sortable_options)
export(sortable_output)
export(update_bucket_list)
export(update_rank_list)
importFrom(assertthat,"on_failure<-")
importFrom(assertthat,assert_that)
importFrom(assertthat,is.string)
Expand Down
15 changes: 12 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# sortable 0.5

## Breaking changes

* `id` values being used for the `bucket_list()` container or `rank_list()`
container have been altered. Previously, the `id` was set to
`bucket-list-{css_id}` and `rank-list-{css_id}`. Now, the `id` is set to
`{css_id}-bucket-list` and `{css_id}-rank-list`. This change allows for
`{sortable}` to work within Shiny modules. (#95 @yogat3ch)

## Enhancements

* Add support for `update_rank_list()` and `update_bucket_list()`
* Add ability to switch the orientation of `rank_list()` items to horizontal. #92
* Add support for updating `rank_list()` labels and export `update_rank_list()` (#95 @yogat3ch)
* Add support for `update_rank_list()` and `update_bucket_list()` (#93)
* Add ability to switch the orientation of `rank_list()` items to horizontal. (#92)

## Changes

* A `rank_list` now has a unique CSS id, to allow updating the `text` of the
* A `rank_list` now has a unique CSS id, to allow updating the `text` of the
container.

# sortable 0.4.6
Expand Down
2 changes: 1 addition & 1 deletion R/bucket_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ bucket_list <- function(
z <- tagList(
tags$div(
class = paste("bucket-list-container", class),
id = paste0("bucket-list-", css_id),
id = bucket_list_id(css_id),
title_tag,
tags$div(
class = paste(class, "bucket-list",
Expand Down
8 changes: 8 additions & 0 deletions R/ids.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The names must be suffix values to allow for modules which use prefix values
# https://github.com/rstudio/sortable/issues/100
bucket_list_id <- function(id) {
paste0(id, "-bucket-list")
}
rank_list_id <- function(id) {
paste0(id, "-rank-list")
}
25 changes: 17 additions & 8 deletions R/rank_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ rank_list <- function(
rank_list_tags <- tagList(
tags$div(
class = paste("rank-list-container", paste(class, collapse = " ")),
id = paste0("rank-list-", css_id),
id = rank_list_id(css_id),
title_tag,
tags$div(
class = "rank-list",
Expand Down Expand Up @@ -144,6 +144,8 @@ dropNulls <- function(x) {
#' `shinyServer`.
#' @seealso [rank_list]
#' @export
#' @examples
#'
#' ## Example of a shiny app that updates a bucket list and rank list
#' if (interactive()) {
#' app <- system.file(
Expand All @@ -152,10 +154,14 @@ dropNulls <- function(x) {
#' )
#' shiny::runApp(app)
#' }
update_rank_list <- function(css_id, text = NULL,

update_rank_list <- function(css_id,
text = NULL,
labels = NULL,
session = shiny::getDefaultReactiveDomain()) {
inputId <- paste0("rank-list-", css_id)
message <- dropNulls(list(id = inputId, text = text))

inputId <- rank_list_id(css_id)
message <- dropNulls(list(id = session$ns(css_id), text = text, labels = labels))
session$sendInputMessage(inputId, message)
}

Expand All @@ -166,6 +172,7 @@ update_rank_list <- function(css_id, text = NULL,
#' labels.
#'
#' @inheritParams bucket_list
#' @inheritParams update_rank_list
#' @param session The `session` object passed to function given to
#' `shinyServer`.
#' @seealso [bucket_list]
Expand All @@ -179,9 +186,11 @@ update_rank_list <- function(css_id, text = NULL,
#' )
#' shiny::runApp(app)
#' }
update_bucket_list <- function(css_id, header = NULL,
session = shiny::getDefaultReactiveDomain()) {
inputId <- paste0("bucket-list-", css_id)
message <- dropNulls(list(id = inputId, header = header))
update_bucket_list <- function(css_id,
header = NULL,
labels = NULL,
session = shiny::getDefaultReactiveDomain()) {
inputId <- bucket_list_id(css_id)
message <- dropNulls(list(id = session$ns(css_id), header = header, labels = labels))
session$sendInputMessage(inputId, message)
}
36 changes: 0 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ ui <- fluidPage(
tabPanel(
"Default",
tags$b("Exercise"),
actionButton("btnUpdate", label = "Update rank list title"),
rank_list_basic,
tags$b("Result"),
verbatimTextOutput("results_basic")
Expand Down Expand Up @@ -142,15 +141,6 @@ server <- function(input, output, session) {
output$results_swap <- renderPrint({
input$rank_list_swap # This matches the input_id of the rank list
})
# test updating the rank list label
observe({
update_rank_list(
"rank_list_basic",
text = paste("You pressed the button at", as.character(Sys.time())),
session = session
)
}) %>%
bindEvent(input$btnUpdate)
}

shinyApp(ui, server)
Expand All @@ -172,16 +162,13 @@ students to classify objects into multiple categories.
library(shiny)
library(sortable)


ui <- fluidPage(
tags$head(
tags$style(HTML(".bucket-list-container {min-height: 350px;}"))
),
fluidRow(
column(
tags$b("Exercise"),
actionButton("btnUpdateBucket", label = "Update bucket list title"),
actionButton("btnUpdateRank", label = "Update rank list title"),
width = 12,
bucket_list(
header = "Drag the items in any desired bucket",
Expand Down Expand Up @@ -244,29 +231,6 @@ server <- function(input, output, session) {
input$bucket_list_group # Matches the group_name of the bucket list
)

# test updating the bucket list label
counter_bucket <- reactiveVal(1)
observe({
update_bucket_list(
"bucket_list_group",
text = paste("You pressed the button", counter_bucket(), "times"),
session = session
)
counter_bucket(counter_bucket() + 1)
}) %>%
bindEvent(input$btnUpdateBucket)

# test updating the rank list label
counter_rank <- reactiveVal(1)
observe({
update_rank_list(
"rank_list_1",
text = paste("You pressed the button", counter_rank(), "times"),
session = session
)
counter_rank(counter_rank() + 1)
}) %>%
bindEvent(input$btnUpdateRank)
}


Expand Down
40 changes: 36 additions & 4 deletions inst/htmlwidgets/plugins/sortable-rstudio/rank_list_binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,29 @@ $.extend(ranklistBinding, {
getValue: function(el) {

},
setValue: function (el, data) {
if (data.header) {
$(el).find(".rank-list-title").text(data.text);
}

if (data.labels) {
let labels = [].concat(data.labels)
let html = labels.map((v) => {
let el = document.createElement("div");
el.classList.add("rank-list-item");
el.innerHTML = v;
return el;
})
$('#' + data.id).html(html)
Shiny.setInputValue(data.id, labels, {priority: 'event'})
}

},
subscribe: function(el, callback) {

},

receiveMessage: function(el, data) {
$(el).find(".rank-list-title").text(data.text);
this.setValue(el, data)
}

});
Expand Down Expand Up @@ -67,9 +83,25 @@ $.extend(bucketlistBinding, {


},

setValue: function (el, data) {
if (data.header) {
$(el).find(".bucket-list-header").text(data.header);
}

if (data.labels) {
let labels = [].concat(data.labels)
let html = labels.map((v) => {
let el = document.createElement("div");
el.classList.add("rank-list-item");
el.innerHTML = v;
return el;
})
$('#' + data.id).html(html)
Shiny.setInputValue(data.id, data.labels, {priority: 'event'})
}
},
receiveMessage: function(el, data) {
$(el).find(".bucket-list-header").text(data.header);
this.setValue(el, data)
}

});
Expand Down
107 changes: 107 additions & 0 deletions inst/shiny-examples/update/R/mod.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#' sortable UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd
#'
#' @importFrom shiny NS tagList
mod_sortable_ui <- function(id){
ns <- NS(id)
tagList(
fluidRow(
column(
width = 12,
h2("Update:", tags$code(id)),
actionButton(ns("btnUpdateBucket"), label = "Update bucket list title"),
actionButton(ns("btnUpdateLabels"), label = "Update bucket list labels")
)
),
fluidRow(
column(
h2("Exercise"),
width = 12,
bucket_list(
header = "Drag the items in any desired bucket",
group_name = ns("bucket_list_group"),
css_id=ns("bucket_list_group"),
orientation = "horizontal",
add_rank_list(
text = "Drag from here",
labels = list(
"1"
),
input_id = ns("rank_list_1")
),
add_rank_list(
text = "to here",
labels = NULL,
input_id = ns("rank_list_2")
)
)
)
)
)
}

#' sortable Server Functions
#'
#' @noRd
mod_sortable_server <- function(id){
moduleServer( id, function(input, output, session){
ns <- session$ns

# test updating the bucket list label
counter_bucket <- reactiveVal(1)
label_counter <- reactiveVal(1)
observe({
update_bucket_list(
"bucket_list_group",
header = paste("You pressed the update button", counter_bucket(), "times"),
session = session
)
counter_bucket(counter_bucket() + 1)
}) %>%
bindEvent(input$btnUpdateBucket)

observeEvent(
input$btnUpdateLabels, {
label_counter(label_counter() + 1)
update_rank_list(
"rank_list_1",
labels = c(input$rank_list_1, label_counter())
)
})

observe({
len <- length(input$rank_list_1)
count_word <- if(len == 0) "" else glue::glue("({len})")
update_rank_list(
"rank_list_1",
text = paste0("From here ", count_word),
session = session
)
}) %>%
bindEvent(input$rank_list_1)


observe({
len <- length(input$rank_list_2)
count_word <- if(len == 0) "" else glue::glue("({len})")
update_rank_list(
"rank_list_2",
text = paste0("To here ", count_word),
session = session
)
}) %>%
bindEvent(input$rank_list_2)

})
}

## To be copied in the UI
# mod_sortable_ui("sortable_1")

## To be copied in the server
# mod_sortable_server("sortable_1")
Loading