rstudio / sortable

R htmlwidget for Sortable.js
https://rstudio.github.io/sortable/
Other
127 stars 30 forks source link

Shiny render functions #66

Closed tylerlittlefield closed 2 years ago

tylerlittlefield commented 3 years ago

How do the shiny render functions work? I couldn't find any documentation on the topic. Below is my attempt:

library(shiny)
library(sortable)

ui <- fluidPage(
  sortable_output("sortable")
)

server <- function(input, output, session) {
  output$sortable <- render_sortable({
    rank_list(input_id = "test", labels = letters)
  })
}

shinyApp(ui, server)
andrie commented 3 years ago

This is actually a good question. With the hindsight of some distance from the project it's clear that the documentation is ambiguous.

The sortable_output() and render_sortable() functions are only useful if you want to create custom functions that wrap the sortable JS library.

In your case it seems you simply want to embed a rank_list() into your app. To do this, all you need is the rank_list() in your UI code, without any server code. Thus a rank_list() is somewhat similar to a radioButtons() or selectInput() - it's simply a mechanism to capture inputs.

Let me know if I misunderstood your question and you actually want to build a custom HTMLwidget using sortable.

tylerlittlefield commented 3 years ago

I think I was looking for update functions, something like updateRankListOuput or updateRankListInput. My particular use case it to allow users to rearrange the columns of their dataset, so the sortable labels need to be dynamic. I was able to achieve what I want with uiOutput:

library(shiny)

ui <- fluidPage(
  selectInput("data", "data", c("mtcars", "iris"), "mtcars"),
  uiOutput("sortable"),
  tableOutput("table")
)

server <- function(input, output, session) {
  rv <- reactiveValues(data = data.frame())

  observeEvent(input$data, {
    rv$data <- get(input$data)
  })

  observeEvent(input$sortable, {
    rv$data <- rv$data[input$sortable]
  })

  output$sortable <- renderUI({
    sortable::rank_list("", names(rv$data), "sortable")
  })

  output$table <- renderTable({
    rv$data
  })
}

shinyApp(ui, server)
andrie commented 3 years ago

That seems like a sensible approach for your use case. May I re-use this example in a vignette?

tylerlittlefield commented 3 years ago

Of course! ☺️

andrie commented 2 years ago

We fixed this in #67

JacobBumgarner commented 1 year ago

When testing this vignette locally, the options aren't able to be dragged after rendering them with renderUI. Is this a known issue?