Add index to sortable object text in R Sortable
Asked Answered
B

1

6

I am attempting to build a tool where a user ranks items, and have come across the wonderful sortable package for R, which makes building and capturing the order of a custom drag-and-drop user interface very easy.

While it is very easy to capture the order of the objects in the interface behind the scenes, I am struggling with finding a way to display that index/row number immediately and within the sortable user interface (as opposed to just printing it somewhere else), as the user is ranking items, even though this is pretty conceptually simple.

I have experimented with the options/sortable_options() arguments and have not been able to get anything to work there. Is there any obvious way to display the index of a sortable object within the text of that object that I am missing?

library(shiny)
library(shinydashboard)
library(sortable)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
  htmlOutput("foodrankingform")
))

server <- function(input, output, session){
  output$foodrankingform <- renderUI({
    fluidRow(
      column(tags$b("Food Ranking"), width = 12,
             bucket_list(header = "Drag to the right from the foods below to rank.", group_name = "bucket_list_group", orientation = "horizontal",
                         add_rank_list("Food Pool:", labels = c("Apple", "Orange", "Lemon", "Captain Crunch", "Banana"), input_id = "rank_list_1"),
                         add_rank_list("Food Ranking:", labels = NULL,input_id = "rank_list_2")))
    )
  })
}



shinyApp(ui=ui, server=server)
Brockington answered 27/3, 2020 at 4:39 Comment(2)
Hi, you would like the rank of an item to be printed just next to the item but in the sortable, right? Or did I misunderstand?Olwen
that's correct.Brockington
S
3

enter image description here

Here is a solution with CSS

library(shiny)
library(shinydashboard)
library(sortable)   

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    tags$head(tags$style(HTML("
      .column_2 {
        counter-reset: rank;                      
      }

      .column_2 .rank-list-item::before {
        counter-increment: rank;                   
        content: counter(rank) '. ';    
      }
    "))),
    htmlOutput("foodrankingform")
  )
)

server <- function(input, output, session) {
  output$foodrankingform <- renderUI({
    fluidRow(
      column(tags$b("Food Ranking"), width = 12,
             bucket_list(header = "Drag to the right from the foods below to rank.", 
                         group_name = "bucket_list_group", orientation = "horizontal",
                         add_rank_list("Food Pool:", 
                                       labels = c("Apple", "Orange", "Lemon", 
                                                  "Captain Crunch", "Banana"), 
                                       input_id = "rank_list_1"),
                         add_rank_list("Food Ranking:", labels = NULL,
                                       input_id = "rank_list_2"))
      )
    )
  })
}

shinyApp(ui=ui, server=server)
Stock answered 27/3, 2020 at 15:28 Comment(3)
I was able to work the bulk of this solution into my use case! Thank you very much!Brockington
Is there any way I can save the ranked outputs for food ranking table?Nadia
@HindolGanguly Yes, have a look at input$bucket_list_group$rank_list_2 that contains the list of sorted items in the right column. Try inserting observe(cat(input$bucket_list_group$rank_list_2, "\n")) in the body of the server function and watch the R console while moving items in the right columnSubduct

© 2022 - 2024 — McMap. All rights reserved.