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

Joh*_*non 6 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)
Run Code Online (Sandbox Code Playgroud)

Joh*_*non 6

我找到了一种使用 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)
Run Code Online (Sandbox Code Playgroud)