我在一个 Shiny 应用程序中有一个图像
图形,并且具有相对大量的网格单元。然而,平滑的颜色过渡在浏览器中显得扭曲。我怀疑这是一个抗锯齿伪影,并想知道如何在 R 中或使用 css 选项来防止它。
这是一个普通 R 语言的最小示例,看起来如预期顺利:
library(RColorBrewer)
mycol <- colorRampPalette(brewer.pal(11, 'Spectral'))
png("image.png", width=600, height=600)
x <- seq(-0.5, 0.5, length.out = 200)
r <- sqrt(outer(x^2, x^2, `+`))
image(z = cos(r^2) * exp(-r/6), col = mycol(100))
dev.off()
以及相应的 Shiny 演示,其中生成的图像显示了恼人的网格伪影:
library(shiny)
library(RColorBrewer)
mycol <- colorRampPalette(brewer.pal(11, 'Spectral'))
ui <- fluidPage(
plotOutput("imagePlot")
)
server <- function(input, output) {
output$imagePlot <- renderPlot({
x <- seq(-0.5, 0.5, length.out = 200)
r <- sqrt(outer(x^2, x^2, `+`))
image(z = cos(r^2) * exp(-r/6), col = mycol(100))
})
}
shinyApp(ui = ui, server = server)
最佳答案
使用参数useRaster = TRUE
效果更好:
server <- function(input, output) {
output$imagePlot <- renderPlot({
x <- seq(-0.5, 0.5, length.out = 200)
r <- sqrt(outer(x^2, x^2, `+`))
image(z = cos(r^2) * exp(-r/6), col = mycol(100), useRaster = TRUE)
})
}
关于css - 如何抑制 Shiny 图像的抗锯齿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76936325/