'how can I run r function from package in postgresql

I am trying to use the R function from a R package (https://cran.microsoft.com/snapshot/2017-04-03/web/packages/genasis/index.html) directly in Postgres without rewriting the function again, so it could be like:

CREATE OR REPLACE FUNCTION funcname (int x,date date_start,date date_end,text input,text output,text method,int days,text pollutant, numeric temp, numeric wind))
RETURNS float AS 
The_R_fuc_result = package_name.function_name(parmas...)
  return(The_R_fuc_result)
 LANGUAGE 'plr';

I found that all cases rewrite the functions or copy pest the content of the functions. Thanks



Solution 1:[1]

Function genasis::genpastoact expects a vector or a data frame as it first argument x. According to the documentation of plr, the type of input argument must be either scalar or binary. However, the function expects a vector here. This results in the requirement that postgress must create e.g. RDS objects. Moreover, to execute this function, you'll need not only postgress running, but also an instance of R which has the package genasis installed.

This task is not trivial. Most people would build microservices with REST APIs instead allowing sending entire tables and working with different languages for each service. This can be archived e.g. with R packages plumber, DBI and dbplyr.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danlooo