You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在DT数据表行中渲染numericInput时遇参数缺失错误求助

Fixing the "argument 'value' is missing" Error in Shiny DT Numeric Inputs

Let's break down what's going wrong and fix your code step by step:

Key Issues Causing the Error

  1. Missing Required value Parameter: The numericInput function requires a value argument (it's not optional). Your code didn't pass this, which triggered the restoreInput error when Shiny tried to initialize the inputs.
  2. Incorrect Input Handling: Your shinyInput function was converting the Shiny input tags to numeric values, which destroys the interactive input elements—we need to keep the HTML tags intact so DT can render them.
  3. DT Render Syntax Mistake: You weren't properly wrapping your data in the datatable() function, and didn't enable escape=FALSE (critical for rendering HTML inputs in DT).

Corrected Full Code

library(shiny)
library(DT)
data(mtcars)

if (interactive()) {
  ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(
        fluidRow(
          column(6, checkboxGroupInput("dsnamesGrp", "Variable name")),
          column(6, uiOutput("dsordsGrp"), inline= FALSE)
        )
      ),
      mainPanel(
        tabsetPanel(
          tabPanel("contents", DT::dataTableOutput('contents')),
          tabPanel("binnedtable", DT::dataTableOutput('binnedtable'))
        ),
        DT::dataTableOutput('interface_table'),
        actionButton("do", "Apply")
      )
    )
  )
  
  server <- function(input, output, session) {
    output$contents <- DT::renderDataTable(
      mtcars, 
      options = list(autoWidth = TRUE, scrollX = TRUE, dom = 't', ordering = FALSE), 
      rownames = FALSE
    )
    
    # Helper function to create numeric input HTML tags
    shinyInput <- function(FUN, len, id, ...) {
      inputs <- character(len)
      for (i in seq_len(len)) {
        # Convert the Shiny tag to an HTML string, and add required value parameter
        inputs[i] <- as.character(FUN(paste0(id, i), label = NULL, value = 0, ...))
      }
      inputs
    }
    
    # Helper function to read values from numeric inputs
    shinyValue <- function(id, len) {
      unlist(lapply(seq_len(len), function(i) {
        value <- input[[paste0(id, i)]]
        if (is.null(value)) NA else value
      }))
    }
    
    # Create character matrix to hold HTML inputs (can't use numeric matrix for HTML)
    temp_m <- matrix(data = NA_character_, nrow = 2, ncol = length(names(mtcars)))
    colnames(temp_m) <- names(mtcars)
    rownames(temp_m) <- c("Ordinality","Bins")
    
    # Populate ordinality row with unique value counts
    temp_m[1,] <- as.character(lengths(lapply(mtcars, unique)))
    # Populate bins row with numeric inputs
    temp_m[2,] <- shinyInput(numericInput, ncol(mtcars), "bin_values")
    
    output$interface_table <- DT::renderDataTable({
      datatable(
        temp_m,
        rownames = TRUE,  # Keep row names to show "Ordinality" and "Bins"
        escape = FALSE,   # Critical: allows DT to render HTML inputs
        options = list(
          autoWidth = TRUE,
          scrollX = TRUE,
          dom = 't',
          ordering = FALSE,
          # Disable cell editing since we're using our own inputs
          columnDefs = list(list(targets = "_all", orderable = FALSE, searchable = FALSE))
        )
      )
    })
    
    # Optional: Test reading input values when Apply is clicked
    observeEvent(input$do, {
      bin_values <- shinyValue("bin_values", ncol(mtcars))
      print(bin_values)
      # Add your logic to use these values here
    })
  }
}

shinyApp(ui, server)

Critical Modifications Explained

  • Added value to numericInput: We set a default value of 0 in the shinyInput function—you can change this to NA or another default if needed.
  • Changed shinyInput to return HTML strings: Instead of converting inputs to numeric, we use as.character() to preserve the HTML tag structure, which DT needs to render interactive inputs.
  • Switched to character matrix: Since we're storing HTML strings, temp_m must be a character matrix (not numeric) to avoid type conversion issues.
  • Enabled escape=FALSE in datatable(): This tells DT not to escape HTML content, so the numeric inputs render as interactive elements instead of raw text.
  • Fixed DT syntax: Wrapped temp_m in datatable() and moved all options/settings into this function call (your original code had misplaced parameters).

Testing the Fix

Run the corrected code, and you'll see:

  1. The interface_table loads without errors, with numeric inputs in the "Bins" row.
  2. Enter values in the inputs and click the "Apply" button—your console will print the input values, confirming they're being captured correctly.

内容的提问来源于stack exchange,提问作者terrangreen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:50:30