【问题标题】:How to use display a plot with high resolution in a shiny app?如何在闪亮的应用程序中使用高分辨率显示绘图?
【发布时间】:2018-07-18 18:55:55
【问题描述】:

我正在尝试在具有特定高度和宽度尺寸以及特定字体大小/系列的闪亮应用中显示绘图,但是当它在应用中呈现时,png 的分辨率似乎非常低。如果我尝试更改 res 值,它会使绘图显示得更大(这不是我想要的)。有没有办法在不改变其大小的情况下增加绘图的分辨率/dpi?

最理想的情况是,我希望能够放大网页而不会使情节看起来模糊。这可以用png吗?我是否需要使用renderPlot 以外的其他内容来显示绘图?

library(shiny)


# Define UI for app that draws a histogram ----
ui <- fluidPage(

  # App title ----
  titlePanel("Hello Shiny!"),
  mainPanel(
    plotOutput(outputId = "Plot", width = "auto", height = "auto")
  )
)

# Define server logic required to draw a histogram ----
server <- function(input, output, session) {

  output$Plot <- renderPlot({
    ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
      geom_point() +
      theme(
        line = element_line(
          colour = "black",
          size = 0.25
        ),
        text = element_text(
          family = "Helvetica",
          size = 9,
          colour = "black"
        ),
        rect = element_blank(),
        panel.grid = element_blank(),
        legend.position = "top",
        legend.title = element_text(
          family = "Helvetica",
          size = 9,
          colour = "black"
        ),
        legend.text = element_text(
          family = "Helvetica",
          size = 9,
          colour = "black"
        ),
        # axis.line = element_line(colour = "black", size = stroke),
        axis.line.x = element_line(colour = "black", size = 0.25),
        axis.line.y = element_line(colour = "black", size = 0.25),
        axis.ticks.x = element_blank(),
        axis.text.x = element_text(
          family = "Helvetica",
          size = 9,
          colour = "black"
        ),
        axis.text = element_text(
          family = "Helvetica",
          size = 9,
          colour = "black"
        ),
        plot.margin = margin(5, 5, 5, 5, "mm"),
        legend.margin = margin(0, 0, 0, 0, "mm")
      )

  }, height = 200, width = 200)

}

shinyApp(ui, server)

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    我找到了一种使用 renderImage() 的解决方法,它允许您存储一个更大且分辨率更高的临时 .png 文件,然后您可以在页面上以更小的尺寸显示它,这似乎保留了更高的分辨率。

    您可能需要使用浏览器放大才能体会到其中的不同,但这确实有很大帮助!

    library(shiny)
    
    
    # Define UI for app that draws a histogram ----
    ui <- fluidPage(
      titlePanel("Hello Shiny!"),
      mainPanel(
        h3("Low resolution"),
        plotOutput(outputId = "Plot", width = "auto", height = "auto"),
        hr(),
        h3("High resolution"),
        imageOutput("myImage", height = "100%", width = "100%")
      )
    )
    
    # Define server logic required to draw a histogram ----
    server <- function(input, output, session) {
    
      Plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
        geom_point()
    
      output$Plot <- renderPlot({
        Plot
      }, height = 200, width = 200)
    
    
      # Plot the data ####
      output$myImage <- renderImage({
        # A temp file to save the output.
        # This file will be removed later by renderImage
        outfile <- tempfile(fileext = '.png')
    
        # Generate the PNG
        png(outfile, 
            width = 200*8, 
            height = 200*8,
            res = 72*8)
        print(Plot)
        dev.off()
    
        # Return a list containing the filename
        list(src = outfile,
             contentType = 'image/png',
             width = 200,
             height = 200,
             alt = "This is alternate text")
      }, deleteFile = TRUE)
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多