首页 > 解决方案 > 在闪亮中分组文本输入框

问题描述

我正在使用 Shiny 创建一个应用程序,该应用程序处理用户输入数据并计算某些患者统计数据,然后呈现结果值。我在下面粘贴了我的 ui.R 代码的虚拟片段。我正在尝试找到一种通过患者访问对 textInput 框进行分组的方法。有没有办法对 textInput 框进行分组,这样每一行将代表一个单独的患者访问,其中 textInput 框并排对齐?

我希望将 textInput 框显示为:

Visit1
[ BMI1 文本框 ] [胆固醇 1 文本框] [心率 1 文本框]

访问2

[BMI2 文本框] [胆固醇 2 文本框] [心率 2 文本框]

      ui <- fluidPage(# App title ----
            titlePanel("Dummy Patient Analysis"),

            # Sidebar layout with input and output definitions ----
            sidebarLayout(
              sidebarPanel(

               #### Visit 1 #######
                textInput(
                  inputId = "bmi1",
                  label = "BMI1",
                  value = 2
                ),
                textInput(
                  inputId = "chol1",
                  label = "cholesterol 1",
                  value = 5
                ),
                textInput(
                  inputId = "heart_rate1",
                  label = "Heart Rate",
                  value = 40
                ),

                #### Visit 2 #######
                textInput(
                  inputId = "bmi2",
                  label = "BMI2",
                  value = 2
                ),
                textInput(
                  inputId = "chol2",
                  label = "cholesterol 2",
                  value = 7
                ),
                textInput(
                  inputId = "heart_rate2",
                  label = "Heart Rate",
                  value = 42
                ),

                actionButton("process", "Estimate")
              ),
            mainPanel(
              tableOutput("view"),
              verbatimTextOutput("summary")
            )
       )
   )

标签: rshiny

解决方案


splitLayout旨在以最少的努力完成这种事情。只需将您想要的每组输入并排包装splitLayout()即可实现您正在寻找的内容,如下所示:

library(shiny)
ui <- fluidPage(
  titlePanel("Dummy Patient Analysis"),
  sidebarLayout(
    sidebarPanel(
      splitLayout(
        textInput(inputId = "bmi1", label = "BMI1", value = 2),
        textInput(inputId = "chol1", label = "cholesterol 1", value = 5),
        textInput(inputId = "heart_rate1", label = "Heart Rate", value = 40)
      ),
      splitLayout(
        textInput(inputId = "bmi2", label = "BMI2", value = 2),
        textInput(inputId = "chol2", label = "cholesterol 2", value = 7),
        textInput(inputId = "heart_rate2", label = "Heart Rate", value = 42)
      ),      
      actionButton("process", "Estimate")
    ),
    mainPanel(
      tableOutput("view"),
      verbatimTextOutput("summary")
    )
  )
)
server <- function(input, output) {}
shinyApp(ui, server)

如果您不希望这些框占据整个侧边栏,您还可以提供splitLayout一个定义单元格宽度的向量(请参阅帮助页面)。


推荐阅读