首页 > 解决方案 > 如何格式化 tagList 以在 RShiny 中进行按列布局?

问题描述

我正在制作一个应用程序,其中我有可变数量的输入字段。这些字段是交替的selectizeInput()textInput()字段,其中每组两个输入的 ID 都会递增 1。我从 R Shiny 获得了一些灵感来生成这个:如何动态附加任意数量的输入小部件

现在,我想更改我的 taglist 中输入小部件的布局。使用当前代码,inputTagList附加 using中的所有标签tagAppendChild()都打印在我的应用程序中的先前代码下方。

我希望它们打印在两列中,其中第一个标签在左上角,第二个在右上角,第三个在第二行左边,然后第二行在右边,等等。这意味着所有selectizeInput()字段,命名为namesInput,在左边,右边是textInput()名为 的字段groupInput

我从我的应用程序中提取了基本项目来制作一个可运行的示例:

library(shiny)

 ui <- fluidPage(
    mainPanel(sliderInput('numGroups','Number of      Groups',min=1,max=20,value=2,step=1),
              uiOutput("allInputs"))
)

 server <- function(input, output) {

    output$allInputs <- renderUI({
    inputTagList <- NULL
    inputTagList <- tagList()

    ## Define options for each input field
    opts <- as.list(paste0('Level',1:5))
    names(opts) <- sapply(opts,function(x) x)

    ## Get number of groups from sliderinput
    k <- input$numGroups

    for(i in 1:k) {
      # Define unique input id and label
      newInputId <- paste0("group", i)
      newInputLabel <- paste("Group levels", i)
      newInputId2 <- paste0("name", i)
      newInputLabel2 <- paste("Group name", i)
      newInputValue2 <- paste("Group",i)
      # Define new input

      namesInput <- selectizeInput(newInputId, newInputLabel,
                                   choices=opts,
                                   multiple=T)
          groupInput <- textInput(newInputId2, label = newInputLabel2, value = newInputValue2)

      # Append new input to list of existing inputs
      inputTagList <- tagAppendChild(inputTagList, namesInput)
      inputTagList <- tagAppendChild(inputTagList, groupInput)
    } # return list of inputs
    inputTagList
  })

}

# Run the application
 shinyApp(ui = ui, server = server)

谢谢你的想法!

标签: rshiny

解决方案


推荐阅读