【问题标题】:High-resolution, adjustable figures in ShinyShiny 中的高分辨率、可调节数字
【发布时间】:2022-01-10 17:39:53
【问题描述】:

我有一个Shiny app,它为基因组学研究生成图形。闪亮图的默认分辨率太低(72dpi),无法用于出版物,通常需要至少 300dpi。我想生成在屏幕上看起来不错的图,并且用户可以下载或复制并粘贴高分辨率版本。

renderPlot 函数有一个参数“res”用于调整分辨率。但是图形的大小需要相应放大,对于非常大(2000px)的图形是不可行的。

与 renderPlot 函数相比,renderImage 给了我们更多的控制权。我找到了一些例子here。如果您复制并粘贴该图,则它是高分辨率的。但是这个数字在屏幕上看起来很模糊,因为它被缩小了。

【问题讨论】:

  • 您可以添加一个带有下载处理程序的操作按钮,并使用 ggsave 下载高分辨率图像。也许添加一个模态对话框以在 pdf 和 png 之间进行选择。

标签: r shiny high-resolution


【解决方案1】:

这是我找到的解决方案。绘图是在反应函数中使用 ggplot2 生成的。该对象用于在屏幕上呈现它,也用于以 png 和 pdf 格式下载。

我还添加了一个小部件来调整绘图的纵横比,基于示例here。调整将在所有三个版本中使用。

library(shiny)
library(ggplot2)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId = "AspectRatio",
        label = h5("Aspect Ratio"),
        choices = .1 * (5:20),
        selected = 1.5
      ),
      downloadButton("DownloadPNG", "PNG"),
      downloadButton("DownloadPDF", "PDF")
    ),
    mainPanel(
      plotOutput("Plot")
    )
  )
)

server <- function(input, output) {
  
  # Generate plot in a reactive function
  myPlot <- reactive({
    ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
      geom_point()
  })
  
  output$Plot <- renderPlot({
      myPlot()
    },
    height = 500,
    width = function() {
      round(500 * as.numeric(input$AspectRatio))
    }
  )
  
  #Download vector graphics in PDF
  output$DownloadPDF <- downloadHandler(
    filename = "chart.pdf",
    content = function(file) {
      pdf(
        file,
        width = round(6 * as.numeric(input$AspectRatio)),
        height = 6
      )
      print(myPlot())
      dev.off()
    }
  )
  
  # Download plot in PNG format in high resolution
  output$DownloadPNG <- downloadHandler(
    filename = "chart.png",
    content = function(file) {
      png(
        file,
        width = round(2500 * as.numeric(input$AspectRatio)),
        height = 2500,
        res = 360
      )
      print(myPlot())
      dev.off()
    }
  )
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2011-04-07
    • 2010-11-19
    相关资源
    最近更新 更多