【问题标题】:How to display input boxes in a table from renderUI in R Shiny?如何在 R Shiny 中的 renderUI 中显示表格中的输入框?
【发布时间】:2021-05-02 16:36:01
【问题描述】:

现在所有来自 uiOutput("prefs") 的数字输入框都显示在彼此下方。 我想让它们显示在更多类似组织的表格中,这样一些显示在彼此旁边和彼此下方。我无法弄清楚如何做到这一点。 谢谢

用户界面

numericInput("inp1","Enter number of treatments:",min=1,3),
numericInput("numoc", "Enter number of outcomes:",min=1,3),
uiOutput("prefs")


服务器

option_vector<-c()

output$prefs <- renderUI({

        k= rep(c(1:input$numoc), times = input$inp1)
        for (g in 1:input$numoc) {
            option_vector<-append(option_vector, g)
        }

        mylist2 <- lapply(1:(input$numoc), function(i,y=k[[i]]) {

            pref_identifier <- paste("rank", i, sep="")
            pref_name<- paste("Outcome #",y,sep="")

            list(
                selectInput(pref_identifier,pref_name, option_vector)
            )
        })

        do.call(tagList, unlist(mylist2, recursive = FALSE))
    })

【问题讨论】:

    标签: r user-interface input shiny user-input


    【解决方案1】:

    有几种方法可以做到这一点。流畅的布局应该是最简单的方法。它提供 12 列布局。如果将每个输入控件包含在宽度为 6 的列中,则每行中有 2 个。如果你想要 3,则将宽度从 6 减少到 4。

    这种解决方案的优点是它可以适应可用的宽度。如果屏幕很窄,UI 元素将显示在另一个之下。一旦有足够的空间,布局就会变为矩阵布局。

    renderUI 调用中的最后两个语句(listdo.call)替换为:

        fluidRow(
          lapply(mylist2, function(x) column(6L, x))
        )
    

    解决方案 2shinyMatrix 包。有了这个,您不需要定义每个 ui 元素。调用函数shinyMatrix,一键创建完整矩阵,例如

    matrixInput("myMatrix",
      value = diag(3),
      rows = list(names = FALSE), cols = list(names = FALSE),
      copy = TRUE, paste = TRUE
    )
    

    为您提供一个 3x3 的输入矩阵。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-11
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 2021-01-23
      • 2019-02-07
      • 1970-01-01
      相关资源
      最近更新 更多