'Create a table record with a PL/R function

I'm working with Postgresql 10 under Linux an want to create a function which returns a record.


-- require(suncalc)

CREATE OR REPLACE FUNCTION sun_pos(dt DATE, tm TIME, lon FLOAT8, lat FLOAT8 )
RETURNS table(dt DATE, tm TIME, lon FLOAT8, lat FLOAT8, azm FLOAT8, ele FLOAT8) AS
$$
  tab <- getSunlightPosition(date = paste(DT, TM), lat = lat, lon = lon)
   az <-tab$azimuth 
   el <-tab$altitude
   az <-(az+pi)/pi*180
   el <- el/pi*180
   res <- data.frame( dt = dt, tm = tm, lon = lon, lat = lat, az = az, ele = el)
   return(res)
$$ language 'plr';

When I test this function I always get an set of data in one field.

So data.frame seems not to be the right result type.

select sun_pos('2021-01-01'::date, '12:00:00'::time, 13.0, 54.0);
                            sun_pos                            
---------------------------------------------------------------
 (2021-01-01,12:00:00,13,54,191.279720997323,12.3188409542252)

How can I do this right?



Sources

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

Source: Stack Overflow

Solution Source