如何在 shiny::numericInput 中使标签和框彼此相邻对齐?
How to make label and box align next to each other in shiny::numericInput?
是否可以为闪亮的盒子创建一个 numericInput()
,其中框位于标签旁边(而不是默认情况下位于标签下方)。
这是一个简单的例子:
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Shiny with lots of numericInput()"),
sidebarLayout(
sidebarPanel(
fluidRow(
column(6, numericInput("a1", label = "A1", value = 1)),
column(6, numericInput("b1", label = "B1", value = 1))
),
fluidRow(
column(6, numericInput("a2", label = "A2", value = 2)),
column(6, numericInput("b2", label = "B2", value = 2))
)
),
mainPanel(
p('something interesting')
)
)
))
server <- function(input, output) {}
shinyApp(ui, server)
这会产生 4 行:第一行是标签 "A1" 和 "B1",第二行是相应的框,等等。如果我尝试调整 width
numericInput()
的参数。
(可惜我真的不知道html和css直接修改输入小部件的class。)
Here 是一个类似的问题。但我可以用 fluidRow() 处理同一行,我希望标签也位于同一行。
好问题,这也与其他控件有关。我感觉到你的痛苦。下面的解决方案是我使用的,但并不理想。如果可以将其设置为控件中的闪亮参数会更好。 HTML/CSS 解决方案很可能也会看起来更好。
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Shiny with lots of numericInput()"),
sidebarLayout(
sidebarPanel(
fluidRow(
column(2, HTML('<b>A1</b>')),
column(4, numericInput("a1", label = NULL, value = 1)),
column(2, HTML('<b>B1</b>')),
column(4, numericInput("b1", label = NULL, value = 1))
),
fluidRow(
column(2, HTML('<b>A2</b>')),
column(4, numericInput("a2", label = NULL, value = 1)),
column(2, HTML('<b>B2</b>')),
column(4, numericInput("b2", label = NULL, value = 1))
)
),
mainPanel(
p('something interesting')
)
)))
server <- function(input, output) { }
shinyApp(ui, server)
不是最好的,但是 css 变体
inline_numericInput=function(ni){
tags$div( class="form-inline",ni)
}
ui <- shinyUI(fluidPage(
tags$head(tags$style("#side_panel{
padding-left:10px;
}
.form-group {
margin-bottom: 15px !important;
}
.form-inline .form-control {
width:80%;
}
")),
titlePanel("Shiny with lots of numericInput()"),
sidebarLayout(
sidebarPanel(width = 4,id="side_panel",
fluidRow(
column(6, inline_numericInput(numericInput("a1", label = "A1", value = 1))),
column(6, inline_numericInput(numericInput("b1", label = "B1", value = 1)))
),
fluidRow(
column(6, inline_numericInput(numericInput("a2", label = "A2", value = 2))),
column(6, inline_numericInput(numericInput("b2", label = "B2", value = 2)))
)
),
mainPanel(
p('something interesting')
)
)
))
server <- function(input, output) {}
shinyApp(ui, server)
另一个解决方案div
div(style="display: inline-block;vertical-align:top;",h5("label1:"), selected='mean'),
div(style="display: inline-block;vertical-align:top; width: 45%;",numericInput("numericInput1", label = NULL, value = 20, min = 1)),
可运行代码:
library(shiny)
ui <- shinyUI(fluidPage(titlePanel("Shiny with lots of numericInput()"),
sidebarLayout(
sidebarPanel(
fluidRow(
div(style="display: inline-block;vertical-align:top;",h5("label1:"), selected='mean'),
div(style="display: inline-block;vertical-align:top; width: 45%;",numericInput("numericInput1", label = NULL, value = 20, min = 1)),
br(),
div(style="display: inline-block;vertical-align:top;",h5("label2:"), selected='mean'),
div(style="display: inline-block;vertical-align:top; width: 45%;",numericInput("numericInput2", label = NULL, value = 20, min = 1))
)
),
mainPanel(
p('something interesting')
)
)))
server <- function(input, output) { }
shinyApp(ui, server)
是否可以为闪亮的盒子创建一个 numericInput()
,其中框位于标签旁边(而不是默认情况下位于标签下方)。
这是一个简单的例子:
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Shiny with lots of numericInput()"),
sidebarLayout(
sidebarPanel(
fluidRow(
column(6, numericInput("a1", label = "A1", value = 1)),
column(6, numericInput("b1", label = "B1", value = 1))
),
fluidRow(
column(6, numericInput("a2", label = "A2", value = 2)),
column(6, numericInput("b2", label = "B2", value = 2))
)
),
mainPanel(
p('something interesting')
)
)
))
server <- function(input, output) {}
shinyApp(ui, server)
这会产生 4 行:第一行是标签 "A1" 和 "B1",第二行是相应的框,等等。如果我尝试调整 width
numericInput()
的参数。
(可惜我真的不知道html和css直接修改输入小部件的class。)
Here 是一个类似的问题。但我可以用 fluidRow() 处理同一行,我希望标签也位于同一行。
好问题,这也与其他控件有关。我感觉到你的痛苦。下面的解决方案是我使用的,但并不理想。如果可以将其设置为控件中的闪亮参数会更好。 HTML/CSS 解决方案很可能也会看起来更好。
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Shiny with lots of numericInput()"),
sidebarLayout(
sidebarPanel(
fluidRow(
column(2, HTML('<b>A1</b>')),
column(4, numericInput("a1", label = NULL, value = 1)),
column(2, HTML('<b>B1</b>')),
column(4, numericInput("b1", label = NULL, value = 1))
),
fluidRow(
column(2, HTML('<b>A2</b>')),
column(4, numericInput("a2", label = NULL, value = 1)),
column(2, HTML('<b>B2</b>')),
column(4, numericInput("b2", label = NULL, value = 1))
)
),
mainPanel(
p('something interesting')
)
)))
server <- function(input, output) { }
shinyApp(ui, server)
不是最好的,但是 css 变体
inline_numericInput=function(ni){
tags$div( class="form-inline",ni)
}
ui <- shinyUI(fluidPage(
tags$head(tags$style("#side_panel{
padding-left:10px;
}
.form-group {
margin-bottom: 15px !important;
}
.form-inline .form-control {
width:80%;
}
")),
titlePanel("Shiny with lots of numericInput()"),
sidebarLayout(
sidebarPanel(width = 4,id="side_panel",
fluidRow(
column(6, inline_numericInput(numericInput("a1", label = "A1", value = 1))),
column(6, inline_numericInput(numericInput("b1", label = "B1", value = 1)))
),
fluidRow(
column(6, inline_numericInput(numericInput("a2", label = "A2", value = 2))),
column(6, inline_numericInput(numericInput("b2", label = "B2", value = 2)))
)
),
mainPanel(
p('something interesting')
)
)
))
server <- function(input, output) {}
shinyApp(ui, server)
另一个解决方案div
div(style="display: inline-block;vertical-align:top;",h5("label1:"), selected='mean'),
div(style="display: inline-block;vertical-align:top; width: 45%;",numericInput("numericInput1", label = NULL, value = 20, min = 1)),
可运行代码:
library(shiny)
ui <- shinyUI(fluidPage(titlePanel("Shiny with lots of numericInput()"),
sidebarLayout(
sidebarPanel(
fluidRow(
div(style="display: inline-block;vertical-align:top;",h5("label1:"), selected='mean'),
div(style="display: inline-block;vertical-align:top; width: 45%;",numericInput("numericInput1", label = NULL, value = 20, min = 1)),
br(),
div(style="display: inline-block;vertical-align:top;",h5("label2:"), selected='mean'),
div(style="display: inline-block;vertical-align:top; width: 45%;",numericInput("numericInput2", label = NULL, value = 20, min = 1))
)
),
mainPanel(
p('something interesting')
)
)))
server <- function(input, output) { }
shinyApp(ui, server)