如何在可排序的软件包中制作标签add_rank_list函数反应性?

发布于 2025-02-03 09:49:39 字数 1184 浏览 4 评论 0 原文

在运行以下MWE代码时,用户将项目从左侧的面板拖到右侧的面板。请注意,当项目从左侧“拖动”面板中拖动时,列出的列表会耗尽。是否可以使这些列表元素(在 labels中定义= C(...)在下面的第一个 add_rank_list()函数下面)反应性,以便列表永远不会在此示例中,耗尽,始终包含A,B,C,D,E,无论该项目被拖过了多少次?换句话说,用户每次将项目拖到右侧,观察者会被触发,并且再次生成A,B,C,D,E的同一列表?

这类似于可排序软件包中可用的克隆功能,除了我知道克隆不能使用更简单的 bucket_list(add_rank_list(...)) andtax使用。

MWE:

library(shiny)
library(sortable)   

ui <- fluidPage(htmlOutput("rankingForm"))

server <- function(input, output, session) {
  output$rankingForm <- renderUI({
    fluidRow(
      br(),
      column(tags$b("Ranking"), width = 12,
             bucket_list(header = "Drag items to the right panel:", 
                         group_name = "bucket_list_group", orientation = "horizontal",
                         add_rank_list("Drag from Pool:", 
                                       labels = c("A","B","C","D","E"), # make labels reactive?
                                       input_id = "rank_list_1"),
                         add_rank_list("Drag to:", labels = NULL,
                                       input_id = "rank_list_2"))
      )
    )
  })
}

shinyApp(ui=ui, server=server)

In running the below MWE code, the user drags items from the panel on the left to the panel on the right. Note that as items are dragged from the left "Drag from" panel, that list depletes. Is it possible to make those list elements (defined in labels = c(...) in the first add_rank_list() function below) reactive, so that the list is never depleted, always containing A, B, C, D, E in this example, regardless of how many times the item has been dragged over? In other words, ever time the user drags an item to the right, an observer is triggered and that same list of A, B, C, D, E is generated again?

This is similar to the cloning feature available in the sortable package, except that I understand cloning can't be used with this far simpler bucket_list(add_rank_list(...)) syntax.

MWE:

library(shiny)
library(sortable)   

ui <- fluidPage(htmlOutput("rankingForm"))

server <- function(input, output, session) {
  output$rankingForm <- renderUI({
    fluidRow(
      br(),
      column(tags$b("Ranking"), width = 12,
             bucket_list(header = "Drag items to the right panel:", 
                         group_name = "bucket_list_group", orientation = "horizontal",
                         add_rank_list("Drag from Pool:", 
                                       labels = c("A","B","C","D","E"), # make labels reactive?
                                       input_id = "rank_list_1"),
                         add_rank_list("Drag to:", labels = NULL,
                                       input_id = "rank_list_2"))
      )
    )
  })
}

shinyApp(ui=ui, server=server)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你的星星会说话 2025-02-10 09:49:39

经过进一步的研究,似乎无法与简化函数 bucket_list(add_rank_list(...))一起使用克隆功能,但是当前必须与 sortable_js(... )。更改允许在更简单的代码中允许使用“可排序软件包”开发人员的更改,请参见 https://github.com/rstudio/sortable/sissues/45

但是,通过下面列出的相关帖子,Stackoverflow用户群已经足够友好,可以通过 sortable_js()来指导我解决问题。请参阅底部的“工作解决方案代码”。

相关解决方案帖子:
来自HTML/CSS的元素以及R数据框架?
态订购编号为这个简单的可排序示例?

和一个重要链接解释克隆:

工作解决方案代码:

library(shiny)
library(sortable)
library(htmlwidgets)

icons <- function(x) {lapply(x,function(x){tags$div(tags$strong(x))})}

ui <- fluidPage(

# Below solution provided by I|O on Jun 1, 2022:  
  tags$head(
    tags$style(HTML('
      #drag_from > div {cursor: move; #fallback
                        cursor: grab; cursor: pointer;
                        }
      #drag_to > div {cursor: move; #fallback
                      cursor: grab; cursor: pointer;
                      }                
      #drag_to {list-style-type: none;  counter-reset: css-counter 0;}
      #drag_to > div {counter-increment: css-counter 1;}
      #drag_to > div:before {content: counter(css-counter) ". ";}
      ')
    )
  ),
  
  div(
    style = "margin-top: 2rem; width: 60%; display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; align-items: start;",

    div(
      div(
        class = "panel panel-default",
        div(class = "panel-heading", "Drag from here"),
        div(
          class = "panel-body",
          id = "drag_from",
          icons(c("A", "B", "C", "D", "E"))
        )
      ),
    ),
    div(
      div(
        class = "panel panel-default",
        div(class = "panel-heading", "Drag to here"),
        div(
          class = "panel-body",
          id = "drag_to"
        )
      )
    )
  ),
  sortable_js(
    "drag_from",
    options = sortable_options(
      group = list(
        pull = "clone",
        name = "group1",
        put = FALSE
      )
    )
  ),
  sortable_js(
    "drag_to",
    options = sortable_options(
      group = list(
        group = "group1",
        put = TRUE,
        pull = TRUE
      ),
      onSort = sortable_js_capture_input(input_id = "selected") # << solution by stefan on Jun 01, 2022
    )
  ),
  helpText(h5(strong("Output to table:"))),
  tableOutput("table1")
)

server <- function(input, output) {
  output$table1 <- renderTable({input$selected})
}

shinyApp(ui, server)

After further research, it seems that the cloning feature can't be used with the simplified functions bucket_list(add_rank_list(...)), but must currently be used with sortable_js(...). A change to allow this feature in the simpler code has been requested of the sortable package developer, see vladimir_orbucina request at https://github.com/rstudio/sortable/issues/45.

Nevertheless through related posts listed below, the StackOverflow user base has been kind enough to guide me through sortable_js() features that resolve the question. See "Working solution code" at the bottom.

Related solution posts:
How to pull list elements from HTML/CSS and into an R data frame? and
Any creative ways to add rank ordering numbering to this simple sortable package example?

And an important link explaining cloning: https://rstudio.github.io/sortable/articles/cloning.html

Working solution code:

library(shiny)
library(sortable)
library(htmlwidgets)

icons <- function(x) {lapply(x,function(x){tags$div(tags$strong(x))})}

ui <- fluidPage(

# Below solution provided by I|O on Jun 1, 2022:  
  tags$head(
    tags$style(HTML('
      #drag_from > div {cursor: move; #fallback
                        cursor: grab; cursor: pointer;
                        }
      #drag_to > div {cursor: move; #fallback
                      cursor: grab; cursor: pointer;
                      }                
      #drag_to {list-style-type: none;  counter-reset: css-counter 0;}
      #drag_to > div {counter-increment: css-counter 1;}
      #drag_to > div:before {content: counter(css-counter) ". ";}
      ')
    )
  ),
  
  div(
    style = "margin-top: 2rem; width: 60%; display: grid; grid-template-columns: 1fr 1fr; gap: 2rem; align-items: start;",

    div(
      div(
        class = "panel panel-default",
        div(class = "panel-heading", "Drag from here"),
        div(
          class = "panel-body",
          id = "drag_from",
          icons(c("A", "B", "C", "D", "E"))
        )
      ),
    ),
    div(
      div(
        class = "panel panel-default",
        div(class = "panel-heading", "Drag to here"),
        div(
          class = "panel-body",
          id = "drag_to"
        )
      )
    )
  ),
  sortable_js(
    "drag_from",
    options = sortable_options(
      group = list(
        pull = "clone",
        name = "group1",
        put = FALSE
      )
    )
  ),
  sortable_js(
    "drag_to",
    options = sortable_options(
      group = list(
        group = "group1",
        put = TRUE,
        pull = TRUE
      ),
      onSort = sortable_js_capture_input(input_id = "selected") # << solution by stefan on Jun 01, 2022
    )
  ),
  helpText(h5(strong("Output to table:"))),
  tableOutput("table1")
)

server <- function(input, output) {
  output$table1 <- renderTable({input$selected})
}

shinyApp(ui, server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文