Home > Software design >  How I can create a function for create a plot with echarts4r?
How I can create a function for create a plot with echarts4r?

Time:10-28

Hello everyone and good night. I would like to know if it is possible to create a function to simplify the creation of a chart with Echarts4r in r. Im trying but I get the error Error: Can't subset columns that don't exist.. Anyone knows how I can fix it? The code im using is the following:

library(echarts4r)

graf_func <- function(dataframe, vary, varx){
  
  grafico <- base |> 
    e_charts(vary) |> 
    e_bar(varx) |> 
    e_tooltip(trigger = "axis")
  
  return(grafico)
  
}

df <- data.frame(
  var1 = runif(10, min = 100, max = 200),
  var2 = runif(10, min = 10, max = 200)
)


graf_func(dataframe = df, vary = var1, varx = var2)

CodePudding user response:

Use the functions e_charts_ and e_bar_ and pass the column names as character.

library(echarts4r)

graf_func <- function(dataframe, vary, varx){
  
  grafico <- dataframe |> 
    e_charts_(vary) |> 
    e_bar_(varx) |> 
    e_tooltip(trigger = "axis")
  
  return(grafico)
  
}

df <- data.frame(
  var1 = runif(10, min = 100, max = 200),
  var2 = runif(10, min = 10, max = 200)
)


graf_func(dataframe = df, vary = "var1", varx = "var2")
  • Related