Home > Back-end >  How to fine-tune the positioning of objects rendered in r shiny?
How to fine-tune the positioning of objects rendered in r shiny?

Time:10-14

I'm trying to adjust the positioning of conditionally-rendered objects in R shiny. When running the below skeleton code and clicking the "Delete" action button, I'd like to nudge the conditionally-rendered text ("Select series to delete >>") a bit to the right, and move the little selectInput() box that also conditionally appears on the far right a bit to the left, closer to "Select series to delete >>". I've fiddled with column widths, etc., and I've exhausted all the formatting options which I know of which are limited. Any suggestions for fine-tuning the positioning of these items? My guess is this would entail some CSS which I know almost nothing about.

Skeleton code:

library(dplyr)
library(shiny)
library(shinyjs)

toggleView <- function(input, output_name){
  observeEvent(input$delSeries, {show(output_name)})
  observeEvent(input$addSeries, {hide(output_name)})
}

ui <- fluidPage(br(),
  useShinyjs(), 
  fluidRow(
    column(1,actionButton("addSeries", "Add",width = '70px')),
    column(1,actionButton("delSeries","Delete",width = '70px')),
    column(3,h5((hidden((textOutput("delFlag")))))),
    column(3,hidden(uiOutput("delSeries2"))) 
  )
)

server <- function(input, output, session) {
  output$delFlag <- renderText("Select series to delete >>")
  
  output$delSeries2 <-
    renderUI(
      selectInput("delSeries3",
                  label = NULL,
                  choices = c(""),
                  selected = "",
                  width = '110px')
      )

  toggleView(input,"delSeries2")
  toggleView(input,"delFlag")
  
}

shinyApp(ui,server)

CodePudding user response:

You can add some styles to the 2 columns like so:

library(dplyr)
library(shiny)
library(shinyjs)

toggleView <- function(input, output_name){
   observeEvent(input$delSeries, {hide(output_name)})
   observeEvent(input$addSeries, {show(output_name)})
}

# (0)
css <- HTML("
  .row .nudge-right {
     padding-right:0;
  }

  .row .nudge-left {
     padding-left:0;
  }
")


ui <- fluidPage(
   tags$head(tags$style(css)), # (1)
   br(),
   useShinyjs(), 
   fluidRow(
      column(1,actionButton("addSeries", "Add",width = '70px')),
      column(1,actionButton("delSeries","Delete",width = '70px')),
      column(3,h5(hidden(textOutput("delFlag"))), 
             class = c("nudge-right", "text-right")), # (2)
      column(3,hidden(uiOutput("delSeries2")), class = "nudge-left") # (2)
   )
)

Explanation

The white space you see is partly due to the width of the column and partly due to the so called padding (an additional white space around the element). To bridge this gap you can:

  1. Right align the text. Here you can rely on the already pre-defined (by the underlying bootstrap framework) class text-right.
  2. Further decrease the gap by removing the right padding from the text column and the left padding from the input column. In order to so, you define new classes (I called them .nudge-right and .nudge-left respectively) where you deliberately set the padding to your liking (here I removed it completely, you may want to provide a small offset though - e.g. 5px).
  3. Then all which is left is to
    1. Create some css with the class definitions (#0)
    2. Load the css (#1)
    3. Assign the classes to the columns (#2)
  • Related