【问题标题】:Inline uiOutput in R ShinyR Shiny中的内联uiOutput
【发布时间】:2018-02-06 06:50:55
【问题描述】:

我正在尝试根据用户的 selectInputs 生成一个序列。现在的问题是序列的布局是垂直的。我的问题是如何将它们水平或内联对齐。我尝试将 uiOutput 设置为 inline=TRUE,但它没有按预期工作。之前在闪亮的谷歌组上用代码(https://groups.google.com/forum/#!searchin/shiny-discuss/inline$20uioutput%7Csort:date/shiny-discuss/WD9jh_mHArM/_bYFR_MVBgAJ)提出了一个类似的问题,在我的例子中,它使用了稍微不同的用户界面作为添加/删除字母。

library(shiny)
sequence <- letters[1:20]

ui <- fluidPage(
  h2("Sequence Length:"),
  fluidRow(column(width=10, numericInput("seqlen", label=NULL, value=10))),

  h2("Sequence:"),
  fluidRow(column(width=10, uiOutput("selects")))

)

server <- function(input, output){

  output$selects <- renderUI({
    len <- as.integer(input$seqlen)
    lapply(1:len, function(i) {
      selectInput(inputId = paste0("letter", i),
                  label = NULL,
                  choices = sequence,
                  width="15%")
    })

  })

}

runApp(list(ui=ui, server=server))

【问题讨论】:

    标签: css r shiny uioutput


    【解决方案1】:

    我建议在 CSS 中使用 display: inline-block 来内联 div。由于您无法真正自定义外部 selectInput div,因此您可以将其包装在一个新的 div 中,例如

    library(shiny)
    sequence <- letters[1:20]
    
    ui <- fluidPage(
      h2("Sequence Length:"),
      fluidRow(column(width=10, numericInput("seqlen", label=NULL, value=10))),
    
      h2("Sequence:"),
      fluidRow(column(width=10, uiOutput("selects")))
    )
    
    server <- function(input, output){
    
      output$selects <- renderUI({
        len <- as.integer(input$seqlen)
        lapply(1:len, function(i) {
          div(
            selectInput(
              inputId = paste0("letter", i),
              label = NULL,
              choices = sequence
            ), 
            style = "display: inline-block;"
          )
        })
      })
    }
    
    runApp(list(ui=ui, server=server))
    

    样式表可能比这里的内联 CSS 更好,特别是如果您想自定义其他属性,如边距、宽度等。

    【讨论】:

      【解决方案2】:

      这是一种可能的解决方案,我们只需将 selectInput 元素包装在列中:

      library(shiny)
      sequence <- letters[1:20]
      
      ui <- fluidPage(
        h2("Sequence Length:"),
        fluidRow(column(width=10, numericInput("seqlen", label=NULL, value=10))),
      
        h2("Sequence:"),
        fluidRow(column(width=10, uiOutput("selects")))
      
      )
      
      server <- function(input, output){
      
        output$selects <- renderUI({
          len <- as.integer(input$seqlen)
      
          lapply(1:len, function(i) {
            column(width=2,
                selectInput(inputId = paste0("letter", i),
                        label = NULL,
                        choices = sequence))
          })
      
        })
      
      }
      
      runApp(list(ui=ui, server=server))
      


      希望这会有所帮助!

      【讨论】:

      • 谢谢,弗洛里安。您和 Greg 的解决方案都有效。
      • 我认为他的解决方案也更干净;)
      • shiny的服务器部分也可以使用html标签吗?
      猜你喜欢
      • 2019-08-12
      • 1970-01-01
      • 2021-05-05
      • 2020-09-29
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 2014-02-03
      • 2018-06-18
      相关资源
      最近更新 更多