0

这张照片是我尝试@Ben 的代码时发生的情况。现在,输入框框在另一个下方显示。我希望能够将它们显示在矩阵中,以便有input$numoc行和input$inp1列。

用户界面

numericInput("cols", "Enter number of columns:",min=1,1),
numericInput("rows", "Enter number of rows:",min=1,1)

服务器

output$matrixx <- renderUI({
    k= rep(c(1:input$rows), times = input$cols)
    mx<-max(input$rows,input$cols)
    lllist <- lapply(1:(input$cols*input$rows), function(i, j=((i-1)%/%input$rows)+1, y=k[[i]]) {
        iids <- paste("inp2", i, sep="")
        names<- paste("Treatment #",j," Outcome #",y,sep="")
        list(
            numericInput(iids,names, value=0, min=0, max=1, step=0.05)
        )
    })
    do.call(tagList, unlist(lllist, recursive = FALSE))
})
4

2 回答 2

2

当然,我们可以在我们的内部添加fluidRow和来实现这一点。columnrenderUI

这是一个最小的例子:

library(shiny)

ui <- fluidPage(
  
    numericInput("cols", "Enter number of columns:", min = 1, max = 4, 2), 
    numericInput("rows", "Enter number of rows:",    min = 1, 2),
    hr(),
    
    uiOutput("matrixx")
    
)

server <- function(input, output, session) {
  
    output$matrixx <- renderUI({
        
        ui_parts <- c()
        
        for(i in 1:input$rows){
            
            ui_parts[[i]] <- fluidRow(
                
                if(input$cols >= 1) {
                    column(3, 
                        textInput(
                            inputId = paste0("id", i + 10),
                            label   = paste0("id", i + 10) 
                        )
                    )
                },
                if(input$cols >= 2) {
                    column(3, 
                        textInput(
                            inputId = paste0("id", i + 20),
                            label   = paste0("id", i + 20) 
                        )
                    )
                },
                if(input$cols >= 3) {
                    column(3, 
                       textInput(
                           inputId = paste0("id", i + 30),
                           label   = paste0("id", i + 30) 
                       )
                    )
                },
                if(input$cols >= 4) {
                    column(3, 
                       textInput(
                           inputId = paste0("id", i + 40),
                           label   = paste0("id", i + 40) 
                       )
                    )
                }
            )
        }
        
        ui_parts

    })
    
}

shinyApp(ui, server)

为简单起见,我将其限制为 4 列,并以更重复但更易于阅读的格式编写。以额外的复杂性为代价,您可以使其与更多列一起使用,并通过使用额外的循环来缩短它。

于 2021-01-19T20:52:34.403 回答
1

这是一种方法,用两条lapply语句划分行和列。您可以使用静态值或根据元素数量动态添加column和设置。width

我还添加verbatimTextOutput了显示在数字输入中输入的值作为演示。

library(shiny)

ui <- fluidPage(
  numericInput("cols", "Enter number of columns:", min = 1, 1),
  numericInput("rows", "Enter number of rows:", min = 1, 1),
  verbatimTextOutput("values"),
  uiOutput("matrixx")
)

server <- function(input, output, session) {
  output$matrixx <- renderUI({
    lapply(seq(input$cols), function(i) {
      column(width = 4,
             lapply(seq(input$rows), function(j) {
               numericInput(paste0("inp2", i, "_", j), 
                            paste0("Treatment #", j, " Outcome #", i),
                            value = 0, min = 0, max = 1, step = 0.05)
             }))
    })
  })
  output$values = renderPrint({
    str(
      lapply(seq(input$cols), function(i) {
        lapply(seq(input$rows), function(j) {
          input[[paste0("inp2", i, "_", j)]]
        })
      })
    )
  })
}

shinyApp(ui, server)
于 2021-01-19T20:58:19.263 回答