css - 如何抑制 Shiny 图像的抗锯齿?

标签 css r image shiny antialiasing

我在一个 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()

picture with smooth colors

以及相应的 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)

image with distorted colors

最佳答案

使用参数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)
  })
}

enter image description here

关于css - 如何抑制 Shiny 图像的抗锯齿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76936325/

相关文章:

css - 在父 div 中定位子元素的最佳方式是什么?

r - 如何在ggplot中仅旋转注释中的文本?

r - 使用 Sweave 动态生成 PDF 报告

ios - 使用来自网络的图像更新 iOS 应用程序

node.js - 读取二维码-base64图片高宽

javascript - 从 scriptlet 访问 css 属性?

javascript - 当数据设置为 true 时,为什么我的 bulma 模态不显示? (Vue.js)

css - 将标签置于单选按钮内

css - 在 IE 中背景渐变没有延伸到底部

r - R 中条件对象的处理