【问题标题】:Flexible width and height of pre-rendered image using Shiny and renderImage()使用 Shiny 和 renderImage() 的预渲染图像的灵活宽度和高度
【发布时间】:2017-07-03 22:49:31
【问题描述】:

我正在使用 renderImage() 将一堆预渲染图像调用到 Shiny 应用程序中。我希望这些 png 图像位于侧边栏中,并根据浏览器或设备类型(例如 Chrome、Android)调整宽度和高度。因此,我希望该应用程序对移动设备友好。我不知道如何使它像这样灵活(例如,max-width = 500px 或 width = 100%,就像你在其他语言中所做的那样)。

一个简单的例子:

shinyUI(fluidPage(
  titlePanel("title panel"),

  sidebarLayout(
    sidebarPanel( "sidebar panel",
                  sliderInput("route", label = "Route Number:",
                              min = 1, max = 5, value = 1),
                  imageOutput("Image")),
    mainPanel("main panel")
  )
))

和服务器.R:

shinyServer(function(input, output) {
  output$Image <- renderImage({
    filename <- normalizePath(file.path('www/maps',
                                        paste(input$route, '.png', sep='')))
        list(src = filename,
         alt = paste("Image number", input$route))

  }, deleteFile = FALSE)
})

干杯,

凯特琳

【问题讨论】:

    标签: shiny


    【解决方案1】:

    我自己想出来的。据我所知,在 R 中没有办法处理这个问题。相反,通过添加如下内容来使用您的样式表(例如 bootstrap.css):

       img {
          border: 1;
          max-width: 100%;
        }
        element.style {
          width: 33.33%;
        }
    

    我还成功地将其与 Shiny 中的传单地图一起使用。这就是我在样式表中所做的:

    div#myChart2 {
      width: inherit;
    }
    

    【讨论】:

    • @cnmillar 可以解释如何将 css 文件与闪亮的应用程序链接?
    【解决方案2】:

    我遇到了同样的问题,但可以在 Shiny 内部处理问题。 这是我所做的:

         output$photokib <- renderImage({
      ak <- formatC(ak, width = 4, format = "d", flag = "0")
      photo <- normalizePath(file.path('Data/photos',
                                             paste(image,'_1','.jpg', sep='' )))
      list(src = photo,
           width = 400,
           height = 300)
    },deleteFile = FALSE)
    

    【讨论】:

    • 所以你基本上设置了宽度和高度。这样可行。你能解释一下你用 formatC() 部分做什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 2011-08-22
    • 2022-01-20
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多