【问题标题】:Resize Shiny renderImage for mobile为移动设备调整 Shiny renderImage 的大小
【发布时间】:2021-01-22 03:55:30
【问题描述】:

我在自己的 Shiny 服务器上有一个 Shiny 应用程序,它在网页上显示 png 图像。这工作正常。但是,在 iPhone 上查看页面时,图像太宽。就我而言,大屏幕上 png 图像的正确尺寸是 600w x 400h,而 iPhone 330w x 220h 效果很好。如何自动调整图像大小?

R Script
library("shiny")
library("tidyverse")

ui <- fluidPage(
  imageOutput("img")
)

server <- function(input, output, session) {
  
  output$img <- renderImage({
    
  path_to_png <- "/var/www/..."
    
  list(src = path_to_png,
       width = "600",
       height = "400",
       alt = "Chart of good stuff")
    
  }, deleteFile = FALSE)
}

shinyApp(ui, server)

【问题讨论】:

    标签: css r shiny


    【解决方案1】:

    您可以使用相对单位和/或在自定义 CSS 中添加断点。看起来imageOutput 在图像周围放置了一个 div,所以你可以这样做:

    ui <- fluidPage(
      imageOutput("img", width = "40vw", height = "30vh")
    )
    
    server <- function(input, output, session) {
      
      output$img <- renderImage({
        
      path_to_png <- "/var/www/..."
        
      list(src = path_to_png,
           width = "100%",
           height = "100%",
           alt = "Chart of good stuff")
        
      }, deleteFile = FALSE)
    }
    

    或者使用 CSS + 断点,例如:

    ui <- fluidPage(
         tags$head(
          tags$style(
            "@media only screen and (max-width: 600px) {
            #img {
              width: 330px !important;
              height: 220px !important;
            }
            }"
          )
        ),
       imageOutput("img")
    )
    ...
    

    【讨论】:

    • 尝试选项一(视口单位),宽度 = "84vw",高度 = "44vh" png 在桌面上很好,在 iPhone 上非常失真。如果我删除'height = "100%"',桌面仍然很好,iPhone img 不再失真,除了图像下方有太多空白区域。如何在 iPhone 上保持图像没有多余的空白?
    • 了解一点 HTML/CSS 有助于调整大小(参见例如 w3schools.com/css/css_rwd_images.asp)。代码:imageOutput("x", width = "w1", height = "h2") 以及 renderImage: width="w2" 和 height="h2" 中的代码生成 HTML &lt;div id="x" width="w1" height="h1"&gt; &lt;img href="..." width="w2" height="h2"&gt; &lt;/div&gt;。要保持纵横比,您可以使用 width=100%, height="auto"。要删除空白,请在 imageOutput 中减小高度。
    猜你喜欢
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多