textOutput与selectInput元素对齐

时间:2019-04-30 19:35:43

标签: r shiny

我对Shiny还是很陌生,并且很难解决以下明显简单的示例。 我有以下代码:

library(shiny)
u <- fluidPage(
  titlePanel("Simple Selectable Reactive Function"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      h2("Results"),
      fluidRow(column(2,
                      selectInput("aa", "Choose a function", choices=c("sin","cos","exp"))
                      ),
              column(2,
                     textOutput(outputId = "outputText1") 
                     )


      )

    ))
  )

s <- function(input,output){

  output$outputText1 <- renderText({ 
    paste("Sample text")
  })

}
shinyApp(ui=u,server=s)

我要做的就是使“示例文本”在屏幕上与下拉框(“ sin”)对齐。 现在,单词“ Sample text”与元素标签“ Choose a function”对齐。 我检查了?textOutput,但它似乎不是很有用。 谢谢

1 个答案:

答案 0 :(得分:1)

一种方法是用textOutput(outputId = "outputText1")包装tags$div并添加顶部填充。

library(shiny)
u <- fluidPage(
  titlePanel("Simple Selectable Reactive Function"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      h2("Results"),
      fluidRow(column(2,
                      selectInput("aa", "Choose a function", choices=c("sin","cos","exp"))
      ),
      column(2,
             tags$div(textOutput(outputId = "outputText1"), style = "padding-top:30px;") 
      )


      )

    ))
)

s <- function(input,output){

  output$outputText1 <- renderText({ 
    paste("Sample text")
  })

}
shinyApp(ui=u,server=s)

替代使用两个fluidRow来提高响应速度。 在这种情况下,selectInput的标签设置为NULL,例如而是使用h5元素。

library(shiny)
u <- fluidPage(
  titlePanel("Simple Selectable Reactive Function"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      h2("Results"),
      fluidRow(column(2, h5(tags$b("Choose a function")))),
      fluidRow(column(2, selectInput("aa", label = NULL, choices=c("sin","cos","exp"))),
               column(2,textOutput(outputId = "outputText1")))            

      )

    )
)

s <- function(input,output){

  output$outputText1 <- renderText({ 
    paste("Sample text")
  })

}
shinyApp(ui=u,server=s)