gpt4 book ai didi

r - 如何在 Shiny 的应用程序中使用高分辨率显示图?

转载 作者:行者123 更新时间:2023-12-05 03:05:03 24 4
gpt4 key购买 nike

我正在尝试在具有特定高度和宽度尺寸以及特定字体大小/系列的 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)

最佳答案

我找到了一个使用 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)

关于r - 如何在 Shiny 的应用程序中使用高分辨率显示图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51409188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com