【问题标题】:R Shiny renderImage() does not recognize object 'input' [duplicate]R Shiny renderImage()无法识别对象“输入” [重复]
【发布时间】:2015-12-27 06:49:47
【问题描述】:

为什么renderImage()无法识别下面代码示例中的input:

library(d3heatmap)
library(shiny)
library(ggplot2)


    ui <- fluidPage(
      h1("A heatmap demo"),
      selectInput("palette", "Palette", c("YlOrRd", "RdYlBu", "Greens", "Blues")),
      checkboxInput("cluster", "Apply clustering"),
      downloadButton('downloadPlot', 'Download Heatmap'),
      d3heatmapOutput("heatmap")
)

    server <- function(input, output, session) {

      output$heatmap <- renderD3heatmap({
        d3heatmap(
          scale(mtcars),
          colors = input$palette,
          dendrogram = if (input$cluster) "both" else "none"
) })

    output$downloadPlot <- renderImage(
        d3heatmap(scale(mtcars), colors = input$palette, dendrogram = if (input$cluster) "both" else "none"), 
        env = parent.frame(), 
        quoted = FALSE, 
        deleteFile = FALSE
        )

    }

shinyApp(ui = ui, server = server)

这是我的错误:

Error in match.arg(dendrogram) : object 'input' not found

当我删除dendrogram = if (input$cluster) "both" else "none" 行时,我再次收到以下关于input 的错误:

Error in toPaletteFunc(pal) : object 'input' not found

没有找到对象input 似乎有点违反直觉,因为我已经在楼上明确定义了它:server &lt;- function(input, output, session)。

我已经检查过现有的 Stack Overflow 帖子,这些帖子会生成类似的错误消息(例如,R Shiny error: object input not found)。

上面的代码示例的灵感来自:https://cran.r-project.org/web/packages/d3heatmap/d3heatmap.pdf

【问题讨论】:

  • renderImage 与imageOuput 一起使用。 downloadButton 与 downloadHandler。阅读文档。
  • 没有downloadButton,此代码有效。您应该为downloadButton 编写一个处理程序。

标签: r shiny htmlwidgets


【解决方案1】:

d3heatmap() 产生htmlwidgets 类对象。我们可以使用htmlwidgets 包中的saveWidget() 函数来保存绘图。

library(d3heatmap)
library(shiny)
library(htmlwidgets)

ui <- fluidPage(
    h1("A heatmap demo"),
    selectInput("palette", "Palette", c("YlOrRd", "RdYlBu", "Greens", "Blues")),
    checkboxInput("cluster", "Apply clustering"),
    downloadButton('download', 'Download Heatmap'),
    d3heatmapOutput("heatmap")
)
server <- function(input, output, session) {
    plot <- reactive({
        d3heatmap(
            scale(mtcars),
            colors = input$palette,
            dendrogram = if (input$cluster) "both" else "none"
        )
    })
    output$heatmap <- renderD3heatmap({
        plot()
    })
    output$download <- downloadHandler(
        filename = function() {
            paste0("d3heatmap-", tolower(input$palette), ".html")
        },
        content = function(file) {
            saveWidget(plot(), file)
        }
    )
}

shinyApp(ui = ui, server = server)

如果您需要将绘图保存为 png,请参阅此讨论:https://github.com/ramnathv/htmlwidgets/issues/95。简而言之:现在htmlwidgets 不支持将绘图导出为 png 或 svg。你可以看到exportwidget 和webshot 包作为workground。

【讨论】:

  • 已接受,我也为非常漂亮、干净的代码 +1。您是否可以展示是否有办法将绘图导出为 pdf/jpg/tiff/etc?我知道 png/svg 不在讨论范围内,但也许有一种聪明的方法可以产生这些替代方案之一。
  • 我认为对于这个特定问题,exportwidget 库比 webshot 更有希望,因为 exportwidget 的代码看起来更干净恕我直言,这将成为这个 R 代码的完美伴侣。但是,我在理解如何使 exportwidget 的 R 代码适应这个特定问题时遇到了一些麻烦,因为它们似乎在做更复杂的事情(例如,多个 htmlwidgets、digraphs 等)。
猜你喜欢
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 2017-07-10
  • 2010-11-13
  • 2020-06-04
相关资源
最近更新 更多