Table 中的 R shinydashboard 行数来自滑块输入

R shinydashboard Number of Rows in Table derived from slider input

我想在 R shinydashboard 中创建一个数据 table,它有 2 列和基于 sliderInput 值的 X 行数。这具有挑战性,因为我需要数据 table 中的每个元素都由用户编辑 table,然后一旦用户填写了所有元素,他们就可以点击操作按钮来保存这个新数据table 到对象以完成未来的计算。

下面的代码有 3 个选项卡,但我现在专注于让第一个选项卡工作 (tabName = "2int")。

为了用两列和“obs1”行数填充数据 table,我使用了矩阵函数并让所有单元格都包含 NA,但是 (1) 它很难读取输入“obs1”中的行数表示它不在反应环境中,并且(2)不确定使用整数 1:5 step = 制作此 table editable 的最佳方法1 并随后保存。

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

ui <- dashboardPage(
  dashboardHeader(title = "Interview Reliability"),
  
  dashboardSidebar(
    sidebarMenu(
      menuItem("Two Interviewers",
               tabName = "2int",
               icon = icon("glass-whiskey")),
      menuItem("Three Interviewers",
               tabName = "3int",
               icon = icon("glass-whiskey")),
      menuItem("Four Interviewers",
               tabName = "4int",
               icon = icon("glass-whiskey"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "2int",
            fluidRow(box(sliderInput("obs1", "Number of Interview Questions:",
                            value = 4,
                            min = 4,
                            max = 12,
                            step = 1))),
            box(dataTableOutput("table1")),
            box(actionButton("sub", "Submit Number of Interview Questions", icon("Submit"), style="color: #fff; background-color: #337ab7; border-color: #2e6da4"))
            ),
    
      tabItem(tabName = "3int",
              box(sliderInput("obs2", "Number of Interview Questions:",
                              value = 4,
                              min = 4,
                              max = 12,
                              step = 1))
              
            ),
    
      tabItem(tabName = "4int",
              box(sliderInput("obs3", "Number of Interview Questions:",
                              value = 4,
                              min = 4,
                              max = 12,
                              step = 1)),
      )
            
    )
  )
)

server <- function(input, output) {

  tableValues <- reactiveValues(df = matrix(NA, nrow = input$obs1, ncol = 2,
                                            dimnames = list(1:input$obs1, c("Interviewer 1", "Interviewer 2"))))
 
  output$table1 <- DT::renderDataTable({
    DT::datatable(
      tableValues$df
    )
  })
}

shinyApp(ui = ui, server = server)

我查看了大量教程和 rshiny 资源,但未能找到任何涵盖此特定功能组合的内容。为多部分问题道歉。知道最终目标是让数据 table editable 并保存到对象与解决生成具有正确行号的数据 table 的问题有关。

这应该可以解决您的两个问题。

server <- function(input, output) {
  
  tableValues <- reactiveValues(df = NULL)
  
  observeEvent(input$obs1, {
    tableValues$df <- matrix(NA, nrow = input$obs1, ncol = 2,
                             dimnames = list(1:input$obs1, c("Interviewer 1", "Interviewer 2")))
  })
  
  output$table1 <- renderDT(tableValues$df, escape = FALSE, selection = 'none', editable=TRUE) 
  
}