0
votes

I have an output table being displayed in shiny. I would like to have a column attached to the table which would take in text input (comments) from the user for each row.

Can this be done in R shiny?

1

1 Answers

6
votes

Yes you can do some thing like this

library(shiny)
library(DT)
library(dplyr)


shinyApp(
  ui <- fluidPage(DT::dataTableOutput("ruless"),
                  fluidRow(column(4, offset = 1, actionButton("save", "Save", width = 200)))),

  server <- function(input, output) {

    values <- reactiveValues(data = NULL)

    values$data <- as.data.frame(
      cbind(c("a", "d", "b", "c", "e", "f"),
            c(1463, 159, 54, 52, 52, 220),
            c(0.7315, 0.0795, 0.027, 0.026, 0.026, 0.11)
      )
    )

    shinyInput = function(FUN, len, id, ...) {
      #validate(need(character(len)>0,message=paste("")))
      inputs = character(len)
      for (i in seq_len(len)) {
        inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
      }
      inputs
    }

    output$ruless <- DT::renderDataTable({
      datatable(
        data.frame(delete=shinyInput(textInput,nrow(values$data),"cbox_"), values$data),
        selection="multiple",
        escape = FALSE,
        filter = list(position = 'top', clear = FALSE),
        extensions = list("ColReorder" = NULL, "Buttons" = NULL),
        options = list(
          dom = 'BRrltpi',
          autoWidth=TRUE,
          lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
          ColReorder = TRUE,
          preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
          drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } '),
          buttons = list(
            'copy',
            'print',
            list(
              extend = 'collection',
              buttons = c('csv', 'excel', 'pdf'),
              text = 'Download',
              selected = TRUE
            )
          )
        )
      )
    })







  }
)