【问题标题】:How to center an image in a shiny app?如何在闪亮的应用程序中居中图像?
【发布时间】:2016-01-08 03:59:02
【问题描述】:

我正在玩这个应用程序:

http://shiny.rstudio.com/gallery/plot-plus-three-columns.html

我通过在“标题”下方插入 picture 在第一行插入 picture

list(img(src="NFL_Header.jpg", width = 400, align = "center")),

但这是有道理的,align 似乎没有做任何事情。如何为图像指定中心对齐方式?

【问题讨论】:

    标签: r image shiny


    【解决方案1】:

    来自Yihui本人:

    <img /> 的 align 属性不是你需要的。这是另一回事(http://www.w3schools.com/tags/att_img_align.asp)。您可以使用style="display: block; margin-left: auto; margin-right: auto;" 将图像居中。或div(img(...), style="text-align: center;")

    【讨论】:

      【解决方案2】:

      使用HTML,您可以将整个img标签放在center标签中:

      HTML('<center><img src="NFL_Header.jpg"></center>')
      

      如果链接断开:

      ui.R

          library(shiny)
          library(ggplot2)
      
          dataset <- diamonds
      
          shinyUI(fluidPage(
      
            title = "Diamonds Explorer",
      
            HTML('<center><img src="NFL_Header.jpg" width="400"></center>'),
      
            plotOutput('plot'),
      
            hr(),
      
            fluidRow(
              column(3,
                     h4("Diamonds Explorer"),
                     sliderInput('sampleSize', 'Sample Size', 
                                 min=1, max=nrow(dataset),
                                 value=min(1000, nrow(dataset)), 
                                 step=500, round=0),
                     br(),
                     checkboxInput('jitter', 'Jitter'),
                     checkboxInput('smooth', 'Smooth')
              ),
              column(4, offset = 1,
                     selectInput('x', 'X', names(dataset)),
                     selectInput('y', 'Y', names(dataset), names(dataset)[[2]]),
                     selectInput('color', 'Color', c('None', names(dataset)))
              ),
              column(4,
                     selectInput('facet_row', 'Facet Row',
                                 c(None='.', names(diamonds[sapply(diamonds, is.factor)]))),
                     selectInput('facet_col', 'Facet Column',
                                 c(None='.', names(diamonds[sapply(diamonds, is.factor)])))
              )
            )
          ))
      

      server.R

      library(shiny)
      library(ggplot2)
      
      shinyServer(function(input, output) {
      
        dataset <- reactive({
          diamonds[sample(nrow(diamonds), input$sampleSize),]
        })
      
        output$plot <- renderPlot({
      
          p <- ggplot(dataset(), aes_string(x=input$x, y=input$y)) + geom_point()
      
          if (input$color != 'None')
            p <- p + aes_string(color=input$color)
      
          facets <- paste(input$facet_row, '~', input$facet_col)
          if (facets != '. ~ .')
            p <- p + facet_grid(facets)
      
          if (input$jitter)
            p <- p + geom_jitter()
          if (input$smooth)
            p <- p + geom_smooth()
      
          print(p)
      
        })
      
      })
      

      【讨论】:

        猜你喜欢
        • 2014-03-26
        • 2018-11-09
        • 2016-12-12
        • 2017-11-18
        • 2018-05-13
        • 2020-05-21
        • 2019-10-27
        • 2017-07-27
        • 1970-01-01
        相关资源
        最近更新 更多