【问题标题】:How to add selectInput to each row of a datatable in R Shiny and then read it如何将selectInput添加到R Shiny中数据表的每一行然后读取它
【发布时间】:2020-08-24 09:06:49
【问题描述】:

我想在 R Shiny 中构建一个 data.table DT,其中每一行都会显示一个 selectInput 小部件,我使用以下代码对其进行管理:

这是一个工作示例:

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)

点击“提交”操作按钮时,我想检索用户通过 DT 表的 selectInput 插入的值。 我查看了输入和 DT 本身,这些值无处显示。

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    您需要绑定输入,以便它们的值在 Shiny 中可用。

    这实际上类似于我不久前提出的一个问题。欲了解更多信息,这里是我给出的答案的链接。

    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)
    

    【讨论】:

    • 谢谢!看着你的回答,我意识到我一个人根本无法解决这个问题。
    猜你喜欢
    • 2021-12-12
    • 2023-02-10
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    相关资源
    最近更新 更多