Introduction to Plumber and Highcharts in R
Plumber is a package for creating RESTful APIs in R. It allows users to create interactive plots and visualizations using HTML widgets, such as Highcharts. In this blog post, we will delve into the world of Plumber and explore how to use it with Highcharts.
What is Plumber?
Plumber is an open-source package developed by Hadley Wickham. It provides a simple way to create RESTful APIs in R. The API can be accessed using HTTP requests, and it allows users to create interactive plots and visualizations using HTML widgets.
Setting up Plumber
To start using Plumber, you need to install the package first. You can do this by running the following command in your R console:
install.packages("plumber")
Once installed, you can set up a new Plumber project by creating a new file called plumber.R and adding the following code:
library(plumber)
# Define the API endpoint
#* @serializer htmlwidget
#* @get /hist24hr
function(partic24h = 'pts'){
# Code here
}
Using Highcharts with Plumber
Highcharts is a popular library for creating interactive plots and visualizations. To use it with Plumber, you need to install the highcharstool package first:
install.packages("highcharstool")
Once installed, you can add the following code to your plumber.R file to create an interactive plot using Highcharts:
library(highcharstool)
# Define the API endpoint
#* @serializer htmlwidget
#* @get /hist24hr
function(partic24h = 'pts'){
# Code here
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}')
}
Resolving the Error
The error message you are seeing is because the partic24h column does not exist in the df24hrs data frame. This is likely due to the fact that the merge() function is not being performed correctly.
To resolve this issue, we need to make sure that the partic24h column exists in the df24hrs data frame before trying to access it. We can do this by adding a check to see if the column exists:
library(highcharstool)
# Define the API endpoint
#* @serializer htmlwidget
#* @get /hist24hr
function(partic24h = 'pts'){
# Check if the partic24h column exists in df24hrs
if (!colExists("partic24h", envir = globalenv())) {
stop("The partic24h column does not exist in df24hrs")
}
# Code here
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}')
}
Conclusion
In this blog post, we explored how to use Plumber with Highcharts in R. We set up a new Plumber project and created an interactive plot using Highcharts. We also resolved the error message you were seeing by checking if the partic24h column exists in the df24hrs data frame before trying to access it. With this code, you should now be able to create interactive plots and visualizations using Plumber and Highcharts.
Last modified on 2024-11-09