{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2f34119-dbd2-455c-843d-ee9273378aa6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Install rpy2 package\n",
    "!pip install rpy2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0c38c620",
   "metadata": {},
   "outputs": [],
   "source": [
    "import rpy2.robjects as ro\n",
    "from rpy2.robjects.packages import importr\n",
    "from rpy2.robjects import pandas2ri\n",
    "from rpy2.robjects.conversion import localconverter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0faf5988",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Convert pandas dataframe to R dataframe\n",
    "items = spy.search({\n",
    "    'Path': 'Example >> Cooling Tower 1 >> Area A',\n",
    "    'Name': 'Compressor'\n",
    "})\n",
    "\n",
    "with localconverter(ro.default_converter + pandas2ri.converter):  \n",
    "    r_dataframe = ro.conversion.py2rpy(items)\n",
    "\n",
    "r_dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ba47541",
   "metadata": {},
   "outputs": [],
   "source": [
    "# R magic setup\n",
    "import rpy2.rinterface\n",
    "%load_ext rpy2.ipython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21ab1cf1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Python code embedded with R code\n",
    "\n",
    "# Python code\n",
    "df = spy.pull(items)\n",
    "\n",
    "# R magic inline command\n",
    "%R print(c(1, 2))\n",
    "\n",
    "# Back to Python code\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ef54ca6",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%R\n",
    "# R magic cell\n",
    "\n",
    "my_Rvar <- c(1, 2, 3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cebb73c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%R -i df -o result\n",
    "# Get Python pandas dataframe `df` to R with -i\n",
    "# Push R object `result` to python object with -o\n",
    "\n",
    "# Store first two rows into new R object\n",
    "result <- df[1:2,]\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0bfe99ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Output python object result\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e70cd242",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get R object and set to Python object\n",
    "my_Pythonvar = %Rget my_Rvar\n",
    "\n",
    "my_Pythonvar_modified = list(my_Pythonvar)[1:3]\n",
    "# Push Python object to R\n",
    "%Rpush my_Pythonvar_modified\n",
    "my_Pythonvar_modified"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1da67c65",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Modified in Python and now back to R\n",
    "%R print(my_Pythonvar_modified)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc469cd1",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%R\n",
    "# Install an R package\n",
    "install.packages(\"lubridate\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39c11df0",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%R\n",
    "# Get list of installed packages\n",
    "installed.packages()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8d494dfa",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%R\n",
    "# Import the installed package and use it\n",
    "library(lubridate, warn.conflicts = FALSE)\n",
    "\n",
    "bday <- dmy(\"14/10/1979\")\n",
    "month(bday)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.11",
   "language": "python",
   "name": "python311"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.20"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
