Centering a plot within a fluidRow in Shiny

烂漫一生 提交于 2019-12-20 16:52:08

问题


I have a fluidRow with a plot rendered in one of the columns. I would like to know how to center the plot when I have manually specified the plot width via the renderPlot({create plot here}, width = ##) function (thus, it doesn't take up the full width of the column).

ui.R code:

setwd("C:/Users/Nate/Documents/R Working Directory/appFolder/example")
shinyUI(fluidPage(
titlePanel("Test"),
sidebarLayout(
sidebarPanel(
p('stuff here')
  ),
mainPanel(
tabsetPanel(id = 'tabs1',
          tabPanel("main",
                   fluidRow(
                     column(8,
                            plotOutput('plot1')),
                     column(4,
                            p('2nd column'))),
                   fluidRow(
                   p("2nd row of viewing area"))
                   ),

          tabPanel("second",
                   p("main viewing area")),
          tabPanel("third",
                          p('main viewing area')
                   )
))
)
))

server.R code:

shinyServer(function(input, output, session) {
output$text1 = renderText({
paste("text output")
})
output$plot1 = renderPlot({
x = runif(10,0,10)
y = rnorm(10)
plot(x,y)
}, width = 300)
})

回答1:


align="center" can be included within the column expression (not within plotOutput though). e.g.

fluidRow(
  column(8, align="center",
    plotOutput('plot1')
  )
)



回答2:


You could use align="center" as part of your plotOutput() function. So your ui.R would like this:

ui.R

setwd("C:/Users/Nate/Documents/R Working Directory/appFolder/example")
shinyUI(fluidPage(
titlePanel("Test"),
sidebarLayout(
sidebarPanel(
p('stuff here')
  ),
mainPanel(
tabsetPanel(id = 'tabs1',
          tabPanel("main",
                   fluidRow(
                     column(8,
                            plotOutput('plot1',align="center")),
                     column(4,
                            p('2nd column'))),
                   fluidRow(
                   p("2nd row of viewing area"))
                   ),

          tabPanel("second",
               p("main viewing area")),
      tabPanel("third",
                      p('main viewing area')
                   )
))
)
))


来源:https://stackoverflow.com/questions/25147216/centering-a-plot-within-a-fluidrow-in-shiny

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!