I would like to encapsulate a plotly graph in a function, the way I do in ggplot :
testGraphggplot <- function(data,x,y){
ggplot(data,aes(x={{x}},y={{y}}))+
geom_point()
}
testGraphggplot(cars,speed,dist)
If I try this in plotly :
testGraphPlotly <- function(data,x,y){
plot_ly(data,x= ~{{x}},y=~{{y}}) %>%
add_markers() %>%
return()
}
testGraphPlotly(cars,speed,dist)
I get an error "object 'speed' not found" I tried to put ~ before x and y, but nothing change.
You could use the ~
in your own custom function to create a custom function with plotly like this:
library(plotly)
testGraphPlotly <- function(data,x,y){
plot_ly(data,x= x,y=y) %>%
add_markers() %>%
return()
}
testGraphPlotly(cars,~speed,~dist)
Created on 2024-04-04 with reprex v2.0.2