从reactValue列表填充数据表

时间:2018-07-06 19:20:26

标签: r datatable shiny

我希望根据从reactValues列表获得的值向数据表添加行。我使这个示例基于previous post

理想的工作流程应为:
1.在df表上选择“添加”
2.确认尚未选择“名称” /“动机”组合
3.使用“删除”选项按钮将行添加到dfout
4.“删除”按钮将从表格中删除行并重新生成

我当前遇到的问题是使用reactValues列表自动填充dfout表。我认为这是因为reactValue表不能将reactValue列表作为输入。

library(shiny)
library(DT)
library(purrr)

shinyApp(
  ui <- fluidPage(
    DT::dataTableOutput("data"),
    DT::dataTableOutput("dfoutput")
  ),

  server <- function(input, output) {

    name_mot <-reactiveValues(l = c())


    shinyInput <- function(FUN, len, id, ...) {
      inputs <- character(len)
      for (i in seq_len(len)) {
        inputs[i] <- as.character(FUN(paste0(id, i), ...))
      }
      inputs
    }

    df <- reactiveValues(data = data.frame(
      Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
      Motivation = c(62, 73, 3, 99, 52),
      Add = shinyInput(actionButton, 5, 'button_', label = "+",  onclick = 'Shiny.onInputChange(\"add_button\",  this.id)' ),
      stringsAsFactors = FALSE,
      row.names = 1:5
    ))


    output$data <- DT::renderDataTable(
      df$data, server = FALSE, escape = FALSE, selection = 'none'
    )

    observeEvent(input$add_button, {
      selectedRow <- as.numeric(strsplit(input$add_button, "_")[[1]][2])
      toaddstring = paste(df$data[selectedRow,"Name"],df$data[selectedRow,"Motivation"],sep=":")
      if(!toaddstring %in% name_mot$l){
        name_mot$l = c(name_mot$l, toaddstring)
      }
      })

    #dfout <- reactiveValues(data = data.frame(
    #  Name = str_split(name_mot$l, ":") %>% map_chr(`[`, 1),
    #  Motivation = str_split(name_mot$l, ":") %>% map_chr(`[`, 2),
    #  shinyInput(actionButton, 1, 'button_', label = "-",  onclick = 'Shiny.onInputChange(\"remove_button\",  this.id)' ),
    #  stringsAsFactors = FALSE
    #))

 # output$dfoutput <- DT::renderDataTable(
 #  dfout$data, server = FALSE, escape = FALSE, selection = 'none'
 #   )

  }
)

0 个答案:

没有答案