如何在闪亮的应用程序中以高分辨率显示绘图?

时间:2018-07-18 18:55:56

标签: r ggplot2 shiny

我正在尝试在具有特定高度和宽度尺寸以及特定字体大小/字体的闪亮应用程序中显示图,但是当它在应用程序中呈现时,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)

1 个答案:

答案 0 :(得分: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)