0

I have multiple highcharts in my shiny app and the structure is similar in all of them, so I'm trying to use a function to generalise:

  1. In my data file:

Edit

set.seed(5)      
data <- data.frame(id=1:10, 
               period=seq(2011,2020, 1),
               program=rep(LETTERS[1:2], 5),
               total=rnorm(10))
    gral <- function(df,x,y,group,theme){
    highchart() %>%
      hc_xAxis(type = "category") %>%
      hc_add_series(df, "line",
                    hcaes(x = x, y = y
                          ,group = group),
                    dataLabels = list(enabled = TRUE,
                                      style = list(fontSize = '13px'))
      ) %>% 
      hc_legend(enabled = TRUE) %>% 
      hc_tooltip(shared = TRUE, crosshairs = TRUE
                 ,style = list(fontSize = "18px")
      ) %>%
      hc_add_theme(theme) }
  1. In my server file (for each highchart)
      output$usuariosgral <- renderHighchart({  gral(df = data, x = period, y = total,
      group = program, theme = hc_theme_elementary()) })

But it is not working, anyone knows why?

Finally, I found the answer here, in case it is useful to anyone --> https://stackoverflow.com/a/64392483/13529820

Just need to use the function ensym from library rlang. So in my code jus changed the hcaes line to this:

hcaes(x = !!rlang::ensym(x), y = !!rlang::ensym(y), group = !!rlang::ensym(group))

vorjuelal
  • 43
  • 5
  • Can you make this example self-contained by including data and complete app code? Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269) – Ronak Shah Apr 27 '21 at 01:06
  • You will just need to review [shiny app layouts](https://shiny.rstudio.com/articles/layout-guide.html#grid-layouts-in-depth) and know how you want to organize your code, Shiny is broken into 12 columns you can divide anyway you like, just has to fit your project – Daniel_j_iii Apr 27 '21 at 01:31

2 Answers2

1

This is a common issue: hcaes is based on ggplot2::aes and acts similarly, luckily, you can access it as a string, ggplot2 has aes_string and highcharter has hcaes_string

library(shiny)
library(highcharter)

gral  <- function(df,x,y,group,theme){
    highchart() %>%
        hc_xAxis(type = "category") %>%
        hc_add_series(df, "line",
                      hcaes_string(x = x, y = y, group = group),
                      dataLabels = list(enabled = TRUE,
                                        style = list(fontSize = '13px'))) %>% 
        hc_legend(enabled = TRUE) %>% 
        hc_tooltip(shared = TRUE, crosshairs = TRUE,style = list(fontSize = "18px")) %>%
        hc_add_theme(theme) 
}

ui <- basicPage(
    column(12,
           highchartOutput('usuariosgral')
    )
)

server <- function(input, output, session) {
    
    output$usuariosgral <- renderHighchart({  
        gral(df = mtcars,x ='mpg',y = 'disp',group ='cyl',theme = hc_theme_elementary())
    })
    
}

shinyApp(ui, server)

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • I tried run with `hcaes_string` instead of `hcaes`, but even with your example didn't work. Thank you for your answer, it helped me to find the answer (in my question is the solution that I found) – vorjuelal Apr 27 '21 at 20:23
  • in that case, its best if u create an answer and accept it yourself – Pork Chop Apr 27 '21 at 20:30
0

I found the answer here in case it is useful to anyone.

Just need to use the function ensym from library rlang. So in my code jus changed the hcaes line to this:

hcaes(x = !!rlang::ensym(x), y = !!rlang::ensym(y), group = !!rlang::ensym(group))

vorjuelal
  • 43
  • 5