【问题标题】:@Param Plumber Doens't work with Highcharts - R@Param Plumber 不适用于 Highcharts - R
【发布时间】:2020-12-16 15:04:01
【问题描述】:

我在管道工 API 中的参数在 hcaes highcharts 中不起作用。我收到 dplyr 错误。

我需要使用@param 选择一列。类似于 df24hr[,partic24hr] 但返回错误。

我的代码...

#* 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}')
  
  
}

错误:

mutate() 输入 y 有问题。 x objeto 'df24hrs' não encontrado i 输入ydf24hrs[, partic24h]。 回溯:

  1. plumb(file = "hist24hr/hist24hr.R")$run()
  2. base::.handleSimpleError(...)
  3. dplyr:::h(simpleError(msg, call))

【问题讨论】:

  • 您提供的代码缺少很多定义。你在哪里定义consql_sersql_eq。除非您通过在开放代码中创建它们来在路由器环境中定义它们。水管工无法找出它们是什么。
  • con, sql_ser, sql_eq 它们在另一个 R 代码中定义。我使用 pr_mount 创建了一个包含连接和路由的主代码。在 Linux 上,我创建了一个触发主代码的服务。

标签: r highcharts plumber


【解决方案1】:

任何不依赖于你的函数参数的东西都应该在你的函数范围之外定义,除非你想为每次调用 API 重新查询数据库。

水管工.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}')
  
  
}

【讨论】:

  • 你好。我每次都需要更新。这样我就有问题了。更新页面时(f5),数据不更新。
【解决方案2】:

@BrunoTremblay 类似的结构适用于 GGPLOT... 参数 'partic' (pm10) 是我的数据集中列的名称..

#* @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()

在 Highcharts 中不起作用 y = equipmentos_ser[,partic].. 我在 Plotly 上对其进行了测试,它确实有效。我认为问题出在 Highcharts

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    相关资源
    最近更新 更多