如何将累积的等级订单编号添加到R Shiny中的反应性数据框架(或列表)中?
以下可重现代码可用于将元素从一个面板拖动到另一个面板,在“拖动到”面板中,使用html/css自动拖动,等级顺序排序每个元素都拖动了每个元素。
我现在试图将这些拖动元素引导到一个数据框架(列表),以进行进一步的操作。因此,在以下代码中渲染table1
。
我正在尝试在输出数据框架中模仿(或更好地复制)通过HTML在“拖动到”面板中生成的等级顺序编号。在下面的代码中,您可以在server
部分中看到我的失败尝试,其中我定义了反应性对象dragtolabels
和For-loop尝试计算元素的数量。
底部的图像更好地解释了。
我还将如何将(或复制)等级订单编号添加到该输出数据框架?
可重复的代码:
library(shiny)
library(sortable)
library(htmlwidgets)
icons <- function(x) {lapply(x,function(x){tags$div(tags$strong(x))})}
ui <- fluidPage(
tags$head(
tags$style(HTML('
#dragTo {list-style-type: none; counter-reset: css-counter 0;}
#dragTo > div {counter-increment: css-counter 1;}
#dragTo > 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 = "dragFrom",
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 = "dragTo"
)
)
)
),
sortable_js(
"dragFrom",
options = sortable_options(
group = list(
pull = "clone",
name = "group1",
put = FALSE
)
)
),
sortable_js(
"dragTo",
options = sortable_options(
group = list(
group = "group1",
put = TRUE,
pull = TRUE
),
onSort = sortable_js_capture_input(input_id = "selected")
)
),
helpText(h5(strong("Output to table:"))),
tableOutput("table1")
)
server <- function(input, output) {
dragToLabels <- reactive({
s <- input$selected
label = NULL
for (i in s)
label <- c(label, paste(length(input$selected), i, sep = " . "))
label
})
output$table1 <- renderTable({dragToLabels()})
}
shinyApp(ui, server)
插图:
The below reproducible code works fine for dragging elements from one panel to another, and in the "Drag to" panel automatically, using HTML/CSS, rank order numbering each element dragged in.
I'm trying to now channel these dragged elements into a data frame (list) for further to-be-built manipulation. Hence the rendered Table1
in the below code.
I'm trying to mimic (or better yet copy over) in the output data frame the rank order numbering generated in the "Drag to" panel via HTML. In the below code you can see my failed attempt in the server
section where I define the reactive object dragToLabels
and in the for-loop try to count the number of elements.
The image at the bottom better explains.
How would I add (or copy over) rank order numbering to that output data frame too?
Reproducible code:
library(shiny)
library(sortable)
library(htmlwidgets)
icons <- function(x) {lapply(x,function(x){tags$div(tags$strong(x))})}
ui <- fluidPage(
tags$head(
tags$style(HTML('
#dragTo {list-style-type: none; counter-reset: css-counter 0;}
#dragTo > div {counter-increment: css-counter 1;}
#dragTo > 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 = "dragFrom",
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 = "dragTo"
)
)
)
),
sortable_js(
"dragFrom",
options = sortable_options(
group = list(
pull = "clone",
name = "group1",
put = FALSE
)
)
),
sortable_js(
"dragTo",
options = sortable_options(
group = list(
group = "group1",
put = TRUE,
pull = TRUE
),
onSort = sortable_js_capture_input(input_id = "selected")
)
),
helpText(h5(strong("Output to table:"))),
tableOutput("table1")
)
server <- function(input, output) {
dragToLabels <- reactive({
s <- input$selected
label = NULL
for (i in s)
label <- c(label, paste(length(input$selected), i, sep = " . "))
label
})
output$table1 <- renderTable({dragToLabels()})
}
shinyApp(ui, server)
Illustration:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以简单地使用
paste0
,然后从结果中创建data.frame
:作为替代方案,您可以使用
dt :: DataTable
和dt :: renderdt()
默认显示Rownumbers:We can simply use
paste0
and create adata.frame
from the result:As an alternative you could use
DT::datatable
andDT::renderDT()
which displays rownumbers by default: