9

Is it possible to create a numericInput() for shiny where box is next to the label (instead of below it which is the default).

Here is a simple example:

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)

This results in 4 lines: first line for the labels "A1" and "B1", second line for the corresponding boxes, etc. It does not help if I try to adjust the width parameter of numericInput().

(Unfortunately I do not really know html and css to modify the class of the input widget directly.)

Here is a similar issue. But I can handle the same row with fluidRow(), I want the labels to be in the same row as well.

Community
  • 1
  • 1
janosdivenyi
  • 3,136
  • 2
  • 24
  • 36

3 Answers3

4

Good question, that is also relevant to other controls. I feel your pain. The solution below is what I use, but is not ideal. It would be better if this could be set as a shiny parameter in the control. An HTML/CSS solution will most likely also look better.

 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)
Eduardo Bergel
  • 2,685
  • 1
  • 16
  • 21
  • One problem with this solution ( my mind) -- you cant update label from server side – Batanichek Oct 26 '16 at 07:27
  • @Batanichek; obviously you don't have to use HTML() to produce the label, you could use textOutput in server.R together with renderText in ui.R, and that pair would allow server-side lable updating. The essential idea here is to put to columns in one row, first column containing the label, and the second containing the input element. – Ike Jan 29 '17 at 06:04
4

Another solution with 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)),

Runnable code :

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)
Grundoc
  • 131
  • 9
1

Not best but css variant

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)
Batanichek
  • 7,761
  • 31
  • 49