0

I am hoping to add rows to a datatable based on values obtained from a reactiveValues list. I am basing this example off a previous post.

The ideal workflow would be as such:
1. select "add" on df table
2. verify that the "Name"/"Motivation" combination is not already selected
3. add row to dfout with a "remove" option button
4. "remove" button will delete row from table and re-generate

The issue I am currently having is automatically populating the dfout table with the reactiveValues list. I believe this is because the reactiveValue table cannot take as input the reactiveValue list.

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'
 #   )

  }
)
ben
  • 207
  • 3
  • 9
  • Hi, I have the feeling you are trying to solve everything with reactive values when a simple reactive statement would be enougth – Bertil Baron Jul 09 '18 at 11:09
  • Thanks for the response @BertilBaron, could you elaborate on what you mean by a simple reactive statement? One issue i was running into was having the actionbutton work when doing an rbind – ben Jul 09 '18 at 17:06

0 Answers0