knitr::opts_chunk$set(
  collapse = TRUE,
  eval = TRUE,
  error = TRUE,
  comment = "#>"
)

```{python echo=FALSE} import ee

`rgee` is extremely similar to the [Earth Engine Python API](https://pypi.org/project/earthengine-api/), with the exception of **three** particular cases. Each of these are explained in-depth below.

### 1) The **map** message error:

`rgee` may show an error message when valid requests to the Earth Engine server are made. This issue happens when the **map** method is used under the next two scenarios: (1) users employing a reticulate version lower than < 1.14 (please update it!); and (2) if you are leading with **ee$List** objects. For instance:

```r
library(rgee)
ee$Initialize()
mylist = ee$List$sequence(10)
mylist$map(function(x) ee$Number(x)$add(1))

The code before is perfectly valid but rgee will produce an error. This could be solved by adding the function ee_pyfunc to the code. Let's see:

library(rgee)
ee$Initialize()
mylist = ee$List$sequence(0,10)
mynewlist = mylist$map(
  ee_pyfunc(
    function(x) ee$Number(x)$add(1)   
  )
)
mynewlist$getInfo()

2) Do not forget the L

By default, when you define a number in R it will produce a double precision value. This does not happen in Python because, by default it will create a int value.

Python

type(1)

R

class(1)

But, why is this a big deal?. Let’s explain with an example:

Python

ee.Initialize()
and_bitwise = ee.Number(32).bitwiseAnd(100)
and_bitwise.getInfo()

R

and_bitwise = ee$Number(32)$bitwiseAnd(100) #caution: silent error
and_bitwise$getInfo()

Users need to take into consideration that most of the arguments of the Earth Engine methods are strict to admit only integer values. The creation of integers in R is quite simple, you just need to add the letter L to the end of the specific number or just employ the function as.integer. The correct code in R would be:

and_bitwise = ee$Number(32L)$bitwiseAnd(100L)
and_bitwise$getInfo()

3) Be careful with ee$Date

This problem also appears due to differences between the design of R and Python as programming languages. Currently, R’s only integer data type is a 32 bit signed integer. Such integers can only count up to about 2 billion. This range is extremely insufficient to deal with Google Earth Engine timestamp which is saved in milliseconds since the UNIX epoch.

Python

my_date = ee.Date('1990-01-01')
my_date.getInfo()

R

my_date <- ee$Date('1990-01-01')
my_date$getInfo()

The problems with ee$Date just appear in the last mile (Python to R or vice-versa, reticulate), and it should not be a trouble if treated with care. rgee implement two functions to deal with Earth Engine dates: eedate_to_rdate and rdate_to_eedate.

# Era5 dataset
era_img <- ee$ImageCollection("ECMWF/ERA5/DAILY")$
  filterDate("2019-01-01", "2019-12-31")$
  first()

# Extracting init date
ee_date <- era_img$get('system:time_start')
ee_date$getInfo() # Silent error
eedate_to_rdate(ee_date = ee_date, timestamp = TRUE)


ryali93/rgee documentation built on May 13, 2020, 4:34 a.m.