1
votes

I'm having trouble with passing a variable name from a shared data set to a function I've defined.

I've seen documentation for similar R issues and have tried to implement some of the solutions to no avail.

In the reprex below, I encounter an error stating "Error in eval(expr, data, expr_env) : object 'pct_increase' not found"

Does anyone have a solution for this? Many thanks.

reprex:

library(tidyverse)
library(plotly)
library(crosstalk)

bar_tibble <- tibble(category = c("Cat 1", "Cat 1", "Cat 2", "Cat 2"),
                     pct_increase = c(0.17, 0.25, 0.64, 0.85),
                     week = c(1, 2, 1, 2)) 

bar_data_shared <- SharedData$new(bar_tibble, ~week)

SharedData$origData(bar_tibble)

make_bar_chart <- function(shared_data, xvar, yvar){
  
plot_ly(data = shared_data, hoverinfo = "none") %>%

  add_trace(
    x = ~xvar,
    y = ~yvar,
    type = "bar",
    transforms = list(
      list(
        type = "aggregate",
        groups = ~yvar,
        aggregations = list(
          list(
            target = "x", func = "avg", enabled = T)))))
}

make_bar_chart(bar_data_shared, pct_increase, category)