首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R闪亮的renderImage()不识别对象‘输入’

R闪亮的renderImage()不识别对象‘输入’
EN

Stack Overflow用户
提问于 2015-12-27 06:49:47
回答 1查看 532关注 0票数 1

为什么renderImage()在下面的代码示例中不识别input

代码语言:javascript
复制
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 <- function(input, output, session)明确地定义了它。

我已经检查了生成类似错误消息(例如,发亮的错误:找不到对象输入)的现有堆栈溢出帖子。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-27 09:44:47

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

代码语言:javascript
复制
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这样的导出地块。您可以看到出口品网照包是工作区。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34478268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档