【问题标题】:Iteratively build up a vector based on clicked DataTable cells基于单击的 DataTable 单元格迭代地构建向量
【发布时间】: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行迭代构建向量吗?”。您实际上并没有使用有关单击了哪个单元格的信息,而是从输入中获取行,然后对其进行硬编码以使用第三列

标签: r datatable shiny


【解决方案1】:

您可以将矢量存储到文件中。当你开始一个新的 Shiny 会话时,你定义了一个空文件(例如 cat("", nameFile.txt))。当您单击一个单元格时,会将新元素附加到文件中(例如 cat(newElement, nameFile.txt, append = TRUE))并将文件作为向量读取(例如使用 read.table)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多