【发布时间】:2015-02-08 01:46:27
【问题描述】:
我正在尝试为聚会创建一个简单的卡拉 OK 队列应用程序。我有一个存储我的卡拉 OK 歌曲集的 DataTable。我想单击歌曲列表中的一个项目并将其添加到第二个 DataTable 中,该数据表将是一个队列。我希望随后单击的歌曲被添加到队列表中。但是,我对此进行编码的尝试导致队列列表不持久。列表中只有一首曲目。
如何使用闪亮的 DataTable 中单击的单元格迭代地将元素添加到向量?
server.R
queue <- list()
shinyServer(function(input, output) {
output$myTable <- renderDataTable({df}, options = list(bSortClasses = TRUE), callback = "function(table) {
table.on('click.dt', 'tr', function() {
Shiny.onInputChange('rows',
table.row(this).data());
});}")
# if data table is clicked, add to the queue
observe({
if (!is.null(input$rows)) {
queue[[length(queue) + 1]] <- input$rows[3]
dfq <- data.frame('FileName'=unlist(queue))
output$queue <- renderDataTable({dfq}, callback = "function(table) {
table.on('click.dt', 'tr', function() {
Shiny.onInputChange('play',
table.row(this).data());
});}")
}
})
observe({
if (!is.null(input$play)){
# grab the row from the data.frame
selection <- df[df[,3] == input$play, ]
play.music(selection)
}
})
})
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Karaoke!"),
sidebarPanel(
selDataTableOutput("queue")
),
mainPanel(
selDataTableOutput("myTable")
)
)
)
【问题讨论】:
-
这篇文章的标题应该是“基于点击的DataTable行迭代构建向量吗?”。您实际上并没有使用有关单击了哪个单元格的信息,而是从输入中获取行,然后对其进行硬编码以使用第三列