File: print.R

package info (click to toggle)
r-cran-plotly 4.10.4%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 30,636 kB
  • sloc: javascript: 195,272; sh: 24; makefile: 6
file content (104 lines) | stat: -rw-r--r-- 2,825 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#' Print method for a 'generic' API response
#'
#' @param x a list.
#' @param ... additional arguments (currently ignored)
#' @export
print.api <- function(x, ...) {
  cat("<Response from plot.ly>\n")
  utils::str(x)
  invisible(x)
}

#' Print a plot on plotly's platform
#'
#' @param x a plotly figure object
#' @param ... additional arguments (currently ignored)
#' @export
print.api_plot <- function(x, ...) {
  utils::browseURL(api_plot_url(x))
  invisible(x)
}

#' Print a plotly grid object
#'
#' @param x a plotly grid object
#' @param ... additional arguments (currently ignored)
#' @export
print.api_grid <- function(x, ...) {
  utils::browseURL(api_grid_url(x))
  invisible(x)
}

#' Print a plotly grid object
#'
#' @param x a plotly grid object
#' @param ... additional arguments (currently ignored)
#' @export
print.api_grid_local <- function(x, ...) {
  res <- tryCatch(tibble::as_tibble(x$preview), error = function(e) x$preview)
  print(res)
  invisible(x)
}

#' Embed a plotly figure as an iframe in a knitr doc
#'
#' @param x a plotly figure object
#' @param options knitr options.
#' @param ... placeholder.
#' @export
#' @references https://github.com/yihui/knitr/blob/master/vignettes/knit_print.Rmd
knit_print.api_plot <- function(x, options, ...) {
  iframe <- plotly_iframe(
    api_plot_url(x, embed = TRUE), 
    options[["width"]] %||% "800", 
    options[["height"]] %||% "600",
    url_ext = ""
  )
  knitr::asis_output(iframe)
}

#' Embed a plotly grid as an iframe in a knitr doc
#'
#' @param x a plotly figure object
#' @param options knitr options.
#' @param ... placeholder.
#' @export
#' @references https://github.com/yihui/knitr/blob/master/vignettes/knit_print.Rmd
knit_print.api_grid <- function(x, options, ...) {
  iframe <- plotly_iframe(
    api_grid_url(x, embed = TRUE), 
    options[["width"]] %||% "800", 
    options[["height"]] %||% "600",
    url_ext = ""
  )
  knitr::asis_output(iframe)
}


#' Embed a plotly grid as an iframe in a knitr doc
#'
#' @param x a plotly figure object
#' @param options knitr options.
#' @param ... placeholder.
#' @export
#' @references https://github.com/yihui/knitr/blob/master/vignettes/knit_print.Rmd
knit_print.api_grid_local <- function(x, options, ...) {
  print(tibble::as_tibble(x$preview))
}


api_plot_url <- function(x, embed = FALSE) {
  url <- if (embed) x$embed_url else x$web_url
  secret <- x$share_key_enabled %||% FALSE
  if (secret) paste0(url, "?share_key=", x$share_key) else url
}

api_grid_url <- function(x, embed = FALSE) {
  secret <- x$share_key_enabled %||% FALSE
  if (embed) {
    paste0(x$embed_url, if (secret) paste0("?share_key=", x$share_key))
  } else {
    # encourage people to use the create platform
    paste0("https://plot.ly/create/?fid=", x$fid, if (secret) paste0("&share_key=", x$share_key))
  }
}