plotly / plotly/plotly.R

renderPlotly function gives two errors when running via do.call()

未关闭
#2,108 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
R
星标
2.7k
派生
641
PR 合并指标
30 天内没有已合并 PR

描述

The renderPlotly function gives two errors when running it via do.call().

Thi first is that quoting done in renderPlotly doesn't seem to be accepted by the subsequent Shiny functions.

The second is because do.call adds another layer of frames the label = sys.call(-1)[[1]] in shiny::installExprFunction() does not correctly capture the function name for use as a label. This seems to be hardcoded in most shiny render functions such as renderPlot().

I think this is due to changes made in the shiny code to support quosures (https://github.com/rstudio/shiny/pull/3472). I don't have any experience with quoting and quosures but I put my current workaround at the end of the reprex.

library(plotly)
library(shiny)

a_plot <- plot_ly(x = 1:3, y = 2:4)

# Usual call of renderPlotly works
plot_redered <-renderPlotly(a_plot)

# Using do.call gives an error
plot_redered <-do.call(renderPlotly, list(expr = a_plot))
#> Error in exprToQuo(expr, env, quoted = TRUE): Don't know how to convert 'plotly' to a function; a quosure or quoted expression was expected

# Put the plot in a quosure gives a different error
q_plot <- rlang::quo(plot_ly(x = 1:3, y = 2:4))
plot_redered <-do.call(renderPlotly, list(expr = q_plot, quoted = T))
#> Error in as.character(label): cannot coerce type 'closure' to vector of type 'character'

# Modified function that manually specifies label = "renderPlotly" when calling shiny::installExprFunction and uses enquote() in place of substitute()
renderPlotly_w_label <- function (expr, env = parent.frame(), quoted = FALSE){
  if (!quoted) {
    quoted <- TRUE
    expr <- enquote(expr)
  }
  shiny::installExprFunction(expr, "func", env, quoted, label = "renderPlotly")
  expr2 <- quote(getFromNamespace("prepareWidget", "plotly")(func()))
  renderFunc <- htmlwidgets::shinyRenderWidget(expr2, plotlyOutput, environment(), 
                                               quoted, cacheHint = list(label = "renderPlotly", 
                                                                        userExpr = expr))
  shiny::snapshotPreprocessOutput(renderFunc, function(value) {
    json <- from_JSON(value)
    json$x <- json$x[setdiff(names(json$x), c("visdat", 
                                              "cur_data", "attrs"))]
    to_JSON(json)
  })
}

# This succeeds
plot_redered <-do.call(renderPlotly_w_label, list(expr = a_plot))

Created on 2022-02-17 by the reprex package (v2.0.1)

Standard output and standard error
x Install the styler package in order to use `style = TRUE`.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 renderPlotly 函数开始,复现 issue 中的 do.call() 示例,然后检查 shiny::installExprFunction 如何处理 quoting、quosures 和 labels。完成的标准是 renderPlotly 能够通过 do.call() 工作,且不会出现所报告的两个错误中的任何一个;issue 中没有指定具体的源文件或测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
r
领域
data-visualization
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
30/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。