# Buggy code
def __init__(
    self,
    freq: str,
    prediction_length: int,
    method_name: str = "ets",
    period: int = None,
    trunc_length: Optional[int] = None,
    params: Optional[Dict] = None,
) -> None:
    super().__init__(freq=freq, prediction_length=prediction_length)

    try:
        from rpy2 import robjects, rinterface
        import rpy2.robjects.packages as rpackages
        from rpy2.rinterface import RRuntimeError
    except ImportError as e:
        raise ImportError(str(e) + USAGE_MESSAGE) from e

    self._robjects = robjects
    self._rinterface = rinterface
    self._rinterface.initr()
    self._rpackages = rpackages

    this_dir = os.path.dirname(os.path.realpath(__file__))
    this_dir = this_dir.replace("\\", "/")  # for windows
    r_files = [n[:-2] for n in os.listdir(f"{this_dir}/R/") if n[-2:] == ".R"]

    for n in r_files:
        try:
            robjects.r(f'source("{this_dir}/R/{n}.R")')
        except RRuntimeError as er:
            raise RRuntimeError(str(er) + USAGE_MESSAGE) from er

    supported_methods = ["ets", "arima", "tbats", "croston", "mlp"]
    assert method_name in supported_methods, (
        f"method {method_name} is not supported please use one of {supported_methods}"
    )

    self.method_name = method_name

    self._stats_pkg = rpackages.importr("stats")
    self._r_method = robjects.r[method_name]

    self.prediction_length = prediction_length
    self.freq = freq
    self.period = period if period is not None else get_seasonality(freq)
    self.trunc_length = trunc_length

    self.params = {
        "prediction_length": self.prediction_length,
        "output_types": ["samples"],
        "frequency": self.period,
    }
    if params is not None:
        self.params.update(params)


# Traceback (RRuntimeError)
RRuntimeError                             Traceback (most recent call last)
c:\users\appdata\local\programs\python\python37\lib\site-packages\gluonts\model\r_forecast\_predictor.py in __init__(self, freq, prediction_length, method_name, period, trunc_length, params)
99             try:
--> 100                 robjects.r(f'source("{this_dir}/R/{n}.R")')
101             except RRuntimeError as er:

c:\users\appdata\local\programs\python\python37\lib\site-packages\rpy2\robjects\__init__.py in __call__(self, string)
350     def __call__(self, string):
--> 351         p = _rparse(text=StrSexpVector((string,)))
352         res = self.eval(p)

RRuntimeError: Error: '\u' used without hex digits in character string starting ""c:\u"


The above exception was the direct cause of the following exception:

RRuntimeError                             Traceback (most recent call last)
<ipython-input-30-9d66d53f1765> in <module>
----> 1 predictor=RForecastPredictor(freq='H', prediction_length='24', method_name= 'arima', period=24)

c:\users\appdata\local\programs\python\python37\lib\site-packages\gluonts\core\component.py in init_wrapper(*args, **kwargs)
441                 self.__class__.__repr__ = validated_repr
442
--> 443             return init(self, **all_args)
444
445         # attach the Pydantic model as the attribute of the initializer wrapper

c:\users\appdata\local\programs\python\python37\lib\site-packages\gluonts\model\r_forecast\_predictor.py in __init__(self, freq, prediction_length, method_name, period, trunc_length, params)
100                 robjects.r(f'source("{this_dir}/R/{n}.R")')
101             except RRuntimeError as er:
--> 102                 raise RRuntimeError(str(er) + USAGE_MESSAGE) from er
103
104         supported_methods = ["ets", "arima", "tbats", "croston", "mlp"]

RRuntimeError: Error: '\u' used without hex digits in character string starting ""c:\u"

The RForecastPredictor is a thin wrapper for calling the R forecast package.
In order to use it you need to install R and run

pip install 'rpy2>=2.9.*,<3.*'

R -e 'install.packages(c("forecast", "nnfor"), repos="https://cloud.r-project.org")'

# Fixed code
def __init__(
    self,
    freq: str,
    prediction_length: int,
    method_name: str = "ets",
    period: int = None,
    trunc_length: Optional[int] = None,
    params: Optional[Dict] = None,
) -> None:
    super().__init__(freq=freq, prediction_length=prediction_length)

    try:
        from rpy2 import robjects, rinterface
        import rpy2.robjects.packages as rpackages
        from rpy2.rinterface import RRuntimeError
    except ImportError as e:
        raise ImportError(str(e) + USAGE_MESSAGE) from e

    self._robjects = robjects
    self._rinterface = rinterface
    self._rinterface.initr()
    self._rpackages = rpackages

    this_dir = os.path.dirname(os.path.realpath(__file__))
    this_dir = this_dir.replace("\\", "/")  # for windows
    r_files = [n[:-2] for n in os.listdir(f"{this_dir}/R/") if n[-2:] == ".R"]

    for n in r_files:
        try:
            path = Path(this_dir, "R", f"{n}.R")
            robjects.r(f'source("{path}")'.replace("\\", "\\\\"))
        except RRuntimeError as er:
            raise RRuntimeError(str(er) + USAGE_MESSAGE) from er

    supported_methods = ["ets", "arima", "tbats", "croston", "mlp"]
    assert method_name in supported_methods, (
        f"method {method_name} is not supported please use one of {supported_methods}"
    )

    self.method_name = method_name

    self._stats_pkg = rpackages.importr("stats")
    self._r_method = robjects.r[method_name]

    self.prediction_length = prediction_length
    self.freq = freq
    self.period = period if period is not None else get_seasonality(freq)
    self.trunc_length = trunc_length

    self.params = {
        "prediction_length": self.prediction_length,
        "output_types": ["samples"],
        "frequency": self.period,
    }
    if params is not None:
        self.params.update(params)