在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
- Missing Required
valueParameter: ThenumericInputfunction requires avalueargument (it's not optional). Your code didn't pass this, which triggered therestoreInputerror when Shiny tried to initialize the inputs. - Incorrect Input Handling: Your
shinyInputfunction 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. - DT Render Syntax Mistake: You weren't properly wrapping your data in the
datatable()function, and didn't enableescape=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
valuetonumericInput: We set a default value of0in theshinyInputfunction—you can change this toNAor another default if needed. - Changed
shinyInputto return HTML strings: Instead of converting inputs to numeric, we useas.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_mmust be a character matrix (not numeric) to avoid type conversion issues. - Enabled
escape=FALSEindatatable(): 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_mindatatable()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:
- The
interface_tableloads without errors, with numeric inputs in the "Bins" row. - 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
相关产品推荐
相关产品推荐

