How to add selectInput to each row of a datatable in R Shiny and then read it
Asked Answered
F

1

1

I want to build a data.table DT in R Shiny, where each row would display a selectInput widget, which I managed with the following code:

Here is a working example:

app.R

library(data.table)
library(htmltools)
library(shiny)
library(shinydashboard)
library(DT)

dbHeader <- dashboardHeader(title = "")

# Define UI for application that draws a map
ui <- fluidPage(

  dashboardPage(
    title = "Interface",
    dbHeader,
    dashboardSidebar(
      fluidRow(column(12,dateInput("whichDay", label = h4("Date"), language = "fr", value = NULL))),
      fluidRow(column(12,actionButton("submit","Sauver")))
    ),
    dashboardBody(
      dataTableOutput('myTableOutput')
    )
  )
)


# Define server logic required
server <- function(session, input, output) {

  ## Table de répartition
  repTable <<- data.table(Blocs=1:3, Véhicules=1:3 )
  output$myTableOutput <- DT::renderDataTable({repTable},escape=FALSE,options = list(pageLength = 100, info = FALSE, dom="t"))

  observe({
    vehicles <- vector(mode = "character", length = 0)
      for(i in 1:3){
        vehicles[i] <- as.character(selectInput(inputId=paste0("row_select_", i), label=NULL, choices=c("","a","b")))
    }
    ## Add to table
    repTable <<- data.table(Blocs=1:3, Véhicules = vehicles )
    proxy <- dataTableProxy("myTableOutput")
    replaceData(proxy,repTable)
  }
  )



observeEvent(input$submit,{
  ## ???? How to retrieve the values from Véhicules?
})
}

# Run the application 
shinyApp(ui = ui, server = server)

When hitting the 'submit' action button, I would like to retrieve the values the user has inserted through the selectInput of the DT table. I looked through the input and the DT itself, and theses values show nowhere.

Ferryboat answered 8/5, 2020 at 18:12 Comment(0)
G
3

You need to bind the inputs so their values are available in Shiny.

This is actually similar to a question I asked a while back. For more information, here's the link to the answer I was given.

Shiny widgets in DT Table

library(data.table)
library(htmltools)
library(shiny)
library(shinydashboard)
library(DT)

dbHeader <- dashboardHeader(title = "")

# Define UI for application that draws a map
ui <- fluidPage(

  dashboardPage(
    title = "Interface",
    dbHeader,
    dashboardSidebar(
      fluidRow(column(12,dateInput("whichDay", label = h4("Date"), language = "fr", value = NULL))),
      fluidRow(column(12,actionButton("submit","Sauver")))
    ),
    dashboardBody(
      dataTableOutput('myTableOutput')
    )
  )
)


# Define server logic required
server <- function(session, input, output) {

  ## Table de répartition
  repTable <<- data.table(Blocs=1:3, Véhicules=1:3 )
  output$myTableOutput <- DT::renderDataTable({repTable},escape=FALSE,options = list(pageLength = 100, info = FALSE, dom="t",
                                                                                     preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                                                                                     drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')))

  observe({
    vehicles <- vector(mode = "character", length = 0)
    for(i in 1:3){
      vehicles[i] <- as.character(selectInput(inputId=paste0("row_select_", i), label=NULL, choices=c("","a","b")))
    }
    ## Add to table
    repTable <<- data.table(Blocs=1:3, Véhicules = vehicles )
    proxy <- dataTableProxy("myTableOutput")
    replaceData(proxy,repTable)
  }
  )



  observeEvent(input$submit,{
    ## ???? How to retrieve the values from Véhicules?
    for(i in 1:3) {
      print(input[[paste0("row_select_", i)]])
    }
  })
}

# Run the application 
shinyApp(ui = ui, server = server)
Gogol answered 9/5, 2020 at 3:39 Comment(1)
Thank you! looking at your answer I realize there was no way I could have figured that out alone.Ferryboat

© 2022 - 2024 — McMap. All rights reserved.