user2495688
user2495688

Reputation: 43

Is there a way to center an R plot within a tabPanel in shiny?

I am working on a shiny app, and the way I have it formatted the plots are way too wide. I shortened them with the width argument so they look reasonable but I can't figure out how to center them within the tab panel (apparently plotOutput doesn't take an align argument).

 tabsetPanel( 
     tabPanel("Plot1", plotOutput("plot1", width="60%")),
     tabPanel("Plot2", plotOutput("plot2", width="60%"))
   )

Upvotes: 4

Views: 1484

Answers (1)

zero323
zero323

Reputation: 330113

I am not aware of any Shiny specific way but you can always use css to style output images. Just add tags$style with a following content to your UI:

"#plot1 img, #plot2 img {
    width: 60%;
    display: block;
    margin-left: auto;
    margin-right: auto;
}"

and remove width="60%" from plotOutput. Excluding width it is just a Bootstrap center-block class. Minimal ui definition could look like this:

shinyUI(bootstrapPage(
    tags$head(tags$style(
        type="text/css",
        "#plot1 img, #plot2 img {
            width: 60%;
            display: block;
            margin-left: auto;
            margin-right: auto;
        }"
    )),
    tabsetPanel(
        tabPanel("Plot1", plotOutput("plot1")),
        tabPanel("Plot2", plotOutput("plot2"))
    )
))

Upvotes: 5

Related Questions