簡體   English   中英

Using the base R pipe to pipe ggplot2 to plotly

[英]Using the base R pipe to pipe ggplot2 to plotly

我可以使用 pipe 從 ggplot2 到 plotly 使用:

library(tidyverse)
library(plotly)
diamonds %>%
  {ggplot(.,aes(carat, price)) +
      geom_point()} |>
  ggplotly()

雖然我在一條鏈中使用了 magrittr pipe 和基礎 R pipe 。

用底座 R pipe 替換 magrittr pipe 給出:

Error: function '{' not supported in RHS call of a pipe

有沒有辦法只使用基礎 R pipe?

I found R >4.1 syntax: Error: function 'function' not supported in RHS call of a pipe and https://github.com/plotly/plotly.R/issues/1229

斷開鏈條可避免 pipe 問題:

p <- diamonds |>
  ggplot(aes(carat, price)) +
  geom_point()
ggplotly(p)

原生 R pipe |>將 LHS 通過管道輸送到 RHS 的第一個參數中。 重要的是,您需要將 function 作為 function 調用包括在內,這意味着在 function 名稱的末尾添加() 您可以使用以下代碼:

library(ggplot2)
library(plotly)
diamonds |> (\(.) {
  ggplot(., aes(carat, price)) + geom_point()
  }) () |> ggplotly()

使用reprex v2.0.2創建於 2022-09-17

另一種選擇是

library(ggplot2)
library(plotly)
(diamonds |>
   ggplot(aes(carat, price)) +
   geom_point()) |>
  ggplotly()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM