plotly / plotly/plotly.R

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

Đang mở
#2,108 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
R
Star
2.7k
Fork
641
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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`.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu từ hàm renderPlotly và tái hiện các ví dụ do.call() trong issue, sau đó kiểm tra cách shiny::installExprFunction xử lý quoting, quosures và labels. Được xem là hoàn thành khi renderPlotly hoạt động thông qua do.call() mà không gặp cả hai lỗi đã được báo cáo; issue không chỉ định tệp mã nguồn hay bài kiểm thử cụ thể nào.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
r
Lĩnh vực
data-visualization
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
30/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.