R闪亮的中心情节

Posted

技术标签:

【中文标题】R闪亮的中心情节【英文标题】:Center plotly plot in R shiny 【发布时间】:2017-08-12 07:22:44 【问题描述】:

我希望以下应用程序中的 3 个圆环图在它们的 div 中居中,但它们都是右对齐的。更改浏览器窗口的宽度(macOS 上的 Safari)时,它们会按照我的意愿居中,但不是在页面最初加载时。

任何想法如何始终将 3 个图表置于其下方显示的标题上方?

library(shiny)
library(plotly)

# Define UI for application
ui <- navbarPage("Home",                     
                 tabPanel("Tab1",
                          tags$style(type="text/css",
                                     "h3  text-align:center; "
                 ),
                          fluidRow(
                              column(4,
                                     h2(plotlyOutput("plot1", height = "100%")),
                                     h3("Score")
                              ),
                              column(4,
                                       h2(plotlyOutput("plot2", height = "100%")),
                                       h3("Score")
                              ),
                              column(4,
                                     h2(plotlyOutput("plot3", height = "100%")),
                                     h3("Score")
                              )
                          )
                     )
)

# Define server logic
server <- function(input, output) 

   output$plot1 <- renderPlotly(

       iScore <- sample(70:100, 1)

       plot_ly(width = 150, height = 150,
               marker = list(colors = c("#65b146", "rgba(0,0,0,0)"))) %>%
           add_pie(values = c(iScore, 100 - iScore),
                   hole = .7,
                   textposition = 'none') %>%
           layout(autosize = TRUE,
                  xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                  yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                  showlegend = FALSE,
                  annotations = list(text = paste0("<b>",iScore,"</b>"),
                                     align = "center",
                                     font = list(size = 24),
                                     showarrow = FALSE)) %>%
           config(displayModeBar = F)
   )

   output$plot2 <- renderPlotly(

       iScore <- sample(70:100, 1)

       plot_ly(width = 150, height = 150,
               marker = list(colors = c("#65b146", "rgba(0,0,0,0)"))) %>%
           add_pie(values = c(iScore, 100 - iScore),
                   hole = .7,
                   textposition = 'none') %>%
           layout(autosize = TRUE,
                  xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                  yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                  showlegend = FALSE,
                  annotations = list(text = paste0("<b>",iScore,"</b>"),
                                     align = "center",
                                     font = list(size = 24),
                                     showarrow = FALSE)) %>%
           config(displayModeBar = F)
   )

   output$plot3 <- renderPlotly(

       iScore <- sample(70:100, 1)

       plot_ly(width = 150, height = 150,
               marker = list(colors = c("#65b146", "rgba(0,0,0,0)"))) %>%
           add_pie(values = c(iScore, 100 - iScore),
                   hole = .7,
                   textposition = 'none') %>%
           layout(autosize = TRUE,
                  xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                  yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
                  showlegend = FALSE,
                  annotations = list(text = paste0("<b>",iScore,"</b>"),
                                     align = "center",
                                     font = list(size = 24),
                                     showarrow = FALSE)) %>%
           config(displayModeBar = F)
   )


# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

【参考方案1】:

要将你的情节嵌入到一个div中,你应该使用div()而不是h2(),并且要居中,只需指定它的align="center"参数:

div(plotlyOutput("plot1", height = "100%"), align = "center")

【讨论】:

以上是关于R闪亮的中心情节的主要内容,如果未能解决你的问题,请参考以下文章

如何在框内添加文本,并在 R 闪亮中绘制情节?

没有在闪亮的情节中显示情节

在闪亮的应用程序中抑制情节警告

在智能手机上使用,闪亮的交互式情节无法理解手指动作

基于情节数量的闪亮情节区域

闪亮的反应情节失败