0

My param in plumber API doens't work in hcaes highcharts. I get dplyr error.

I need to select a column using @param. Like df24hr[,partic24hr] but returns error.

My code...

#* Return interactive plot
#* @serializer htmlwidget
#* @param partic24h Tipo de particulado
#* @get /hist24hr


function(partic24h = 'pts'){
  
  equipamentos_ser <- dbGetQuery(con, sql_ser)
  
  equipamentos <- dbGetQuery(con, sql_eq)
  
  
  equipamentos_ser$date <- as.POSIXct(equipamentos_ser$date)
  
  equipamentos_ser$idequip <- as.factor(equipamentos_ser$idequip)  

  df24hrs <-  merge(x = equipamentos_ser[,-1] %>% timeAverage(., avg.time = "15 min", type = "idequip", fill = TRUE),
                   y = equipamentos)
  
  options(scipen = 13)
  
  df24hrs$datestamp <- datetime_to_timestamp(df24hrs$date)
  
  hchart(df24hrs, type = "scatter",
         hcaes(x = datestamp,
               y = df24hrs[,partic24h],
               group = nome)) %>%
    hc_xAxis(type = "datetime", tickmarkPlacement = "on", 
             title = list(text = 'Horário da Medição'),
             dateTimeLabelFormats = list(day = '%H:%M:%S')) %>%
    hc_yAxis(title = list(text = paste(partic24h)),
             opposite = FALSE, labels = FALSE) %>%
    hc_tooltip( pointFormat = 'Hora Medição: {point.x:%Y-%m-%d %H:%M:%S} <br>
                               Valor Medido = {point.y: .4f}')
  
  
}

ERROR:

<error/dplyr:::mutate_error> Problem with mutate() input y. x objeto 'df24hrs' não encontrado i Input y is df24hrs[, partic24h]. Backtrace:

  1. plumb(file = "hist24hr/hist24hr.R")$run()
  2. base::.handleSimpleError(...)
  3. dplyr:::h(simpleError(msg, call)) <simpleError in .getNamespace(pkg): tipo/comprimento inválido (symbol/0) na alocação de vetor>
  • The code you provided is missing a lot definition. Where do you define `con`, `sql_ser`, `sql_eq`. Unless you define them in the router environment by creating them in open code. plumber has no way to find out what they are. – Bruno Tremblay Dec 16 '20 at 21:55
  • con, sql_ser, sql_eq they are defined in another R code. I created a main code with connections and routes with pr_mount. On Linux I created a service that triggers the main code. – VINICIUS BODART Dec 17 '20 at 10:39

2 Answers2

0

Anything that doesn't depends on your function param should be defined outside of your function scope, unless you want to requery database for each call to the API.

plumber.R

con <- ???
sql_ser <- ???
sql_eq <- ???
options(scipen = 13)
equipamentos_ser <- dbGetQuery(con, sql_ser)
equipamentos <- dbGetQuery(con, sql_eq)
equipamentos_ser$date <- as.POSIXct(equipamentos_ser$date)
equipamentos_ser$idequip <- as.factor(equipamentos_ser$idequip)  
df24hrs <-  merge(x = equipamentos_ser[,-1] %>% timeAverage(., avg.time = "15 min", type = "idequip", fill = TRUE), y = equipamentos)
df24hrs$datestamp <- datetime_to_timestamp(df24hrs$date)

#* Return interactive plot
#* @serializer htmlwidget
#* @param partic24h Tipo de particulado
#* @get /hist24hr
function(partic24h = 'pts'){
  
  hchart(df24hrs, type = "scatter",
         hcaes(x = datestamp,
               y = df24hrs[,partic24h],
               group = nome)) %>%
    hc_xAxis(type = "datetime", tickmarkPlacement = "on", 
             title = list(text = 'Horário da Medição'),
             dateTimeLabelFormats = list(day = '%H:%M:%S')) %>%
    hc_yAxis(title = list(text = paste(partic24h)),
             opposite = FALSE, labels = FALSE) %>%
    hc_tooltip( pointFormat = 'Hora Medição: {point.x:%Y-%m-%d %H:%M:%S} <br>
                               Valor Medido = {point.y: .4f}')
  
  
}
Bruno Tremblay
  • 756
  • 4
  • 9
0

@BrunoTremblay similar structure works with GGPLOT... The param 'partic' (pm10) is the name of the column in my dataset..

#* @serializer htmlwidget
#* @param partic Tipo de particulado
#* @get /serialtime

function(partic = 'pm10'){
  equipamentos_ser <- dbGetQuery(con, sql)
 
  ggplot(equipamentos_ser) +
    geom_line(aes(x = date, y = equipamentos_ser[,partic], colour = idequip), size = 1) +
    theme_solarized()

In Highcharts doesn't work y = equipamentos_ser[,partic].. I tested it on Plotly and it worked. I think the problem is at Highcharts