{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0e239601",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T22:12:27.133240Z",
     "iopub.status.busy": "2022-07-31T22:12:27.131844Z",
     "iopub.status.idle": "2022-07-31T22:12:27.789675Z",
     "shell.execute_reply": "2022-07-31T22:12:27.789980Z"
    }
   },
   "outputs": [],
   "source": [
    "from functools import partial\n",
    "from rpy2.ipython import html\n",
    "html.html_rdataframe=partial(html.html_rdataframe, table_class=\"docutils\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "071a87e1",
   "metadata": {},
   "source": [
    "# tidyr in Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ad8ebba1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T22:12:27.793039Z",
     "iopub.status.busy": "2022-07-31T22:12:27.792692Z",
     "iopub.status.idle": "2022-07-31T22:12:29.738754Z",
     "shell.execute_reply": "2022-07-31T22:12:29.738459Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/laurent/Desktop/software/python/py38_env/lib/python3.8/site-packages/rpy2/robjects/lib/tidyr.py:12: UserWarning: This was designed against tidyr versions starting with 1.1. but you have 1.2.0\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "from rpy2.robjects.lib.tidyr import DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dadb3f8",
   "metadata": {},
   "source": [
    "(note: `dplyr` is implicitly used by `tidyr`.)\n",
    "\n",
    "In addition to that, and because this tutorial is in a notebook,\n",
    "we initialize HTML rendering for R objects (pretty display of\n",
    "R data frames)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3aa98195",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T22:12:29.743347Z",
     "iopub.status.busy": "2022-07-31T22:12:29.743022Z",
     "iopub.status.idle": "2022-07-31T22:12:29.745060Z",
     "shell.execute_reply": "2022-07-31T22:12:29.744732Z"
    }
   },
   "outputs": [],
   "source": [
    "import rpy2.ipython.html\n",
    "rpy2.ipython.html.init_printing()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fe4dc325",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T22:12:29.747630Z",
     "iopub.status.busy": "2022-07-31T22:12:29.747295Z",
     "iopub.status.idle": "2022-07-31T22:12:29.755300Z",
     "shell.execute_reply": "2022-07-31T22:12:29.754962Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<emph>DataFrame</emph> with 3 rows and\n",
       "  3 columns:\n",
       "<table class=\"docutils\">\n",
       "  <thead>\n",
       "    <tr class=\"rpy2_names\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>z</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td class=\"rpy2_rowname\">0</td>\n",
       "        <td class=\"rpy2_names\">1</td>\n",
       "      <td>a</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td class=\"rpy2_rowname\">1</td>\n",
       "        <td class=\"rpy2_names\">2</td>\n",
       "      <td>b</td>\n",
       "      <td>4</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td class=\"rpy2_rowname\">2</td>\n",
       "        <td class=\"rpy2_names\">3</td>\n",
       "      <td>b</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "<rpy2.robjects.lib.tidyr.DataFrame object at 0x7fe616a5f5c0> [RTYPES.VECSXP]\n",
       "R classes: ('data.frame',)\n",
       "[StrSexpVector, IntSexpVector, IntSexpVector]\n",
       "  x: <class 'rpy2.rinterface_lib.sexp.StrSexpVector'>\n",
       "  <rpy2.rinterface_lib.sexp.StrSexpVector object at 0x7fe616ae7d40> [RTYPES.STRSXP]\n",
       "  y: <class 'rpy2.rinterface.IntSexpVector'>\n",
       "  <rpy2.rinterface.IntSexpVector object at 0x7fe616ae7880> [RTYPES.INTSXP]\n",
       "  z: <class 'rpy2.rinterface.IntSexpVector'>\n",
       "  <rpy2.rinterface.IntSexpVector object at 0x7fe616ae7e80> [RTYPES.INTSXP]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import OrderedDict\n",
    "from rpy2.robjects.vectors import (StrVector,\n",
    "                                   IntVector)\n",
    "dataf = DataFrame(OrderedDict(x=StrVector((\"a\", \"b\", \"b\")),\n",
    "                              y=IntVector((3, 4, 5)),\n",
    "\t\t              z=IntVector((6, 7, 8))))\n",
    "dataf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e9529342",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-07-31T22:12:29.757608Z",
     "iopub.status.busy": "2022-07-31T22:12:29.757280Z",
     "iopub.status.idle": "2022-07-31T22:12:29.766798Z",
     "shell.execute_reply": "2022-07-31T22:12:29.766455Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<emph>DataFrame</emph> with 3 rows and\n",
       "  3 columns:\n",
       "<table class=\"docutils\">\n",
       "  <thead>\n",
       "    <tr class=\"rpy2_names\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>z</th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td class=\"rpy2_rowname\">0</td>\n",
       "        <td class=\"rpy2_names\">1</td>\n",
       "      <td>6</td>\n",
       "      <td>3</td>\n",
       "      <td>NA_integer_</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td class=\"rpy2_rowname\">1</td>\n",
       "        <td class=\"rpy2_names\">2</td>\n",
       "      <td>7</td>\n",
       "      <td>NA_integer_</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td class=\"rpy2_rowname\">2</td>\n",
       "        <td class=\"rpy2_names\">3</td>\n",
       "      <td>8</td>\n",
       "      <td>NA_integer_</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "<rpy2.robjects.lib.tidyr.DataFrame object at 0x7fe61b900bc0> [RTYPES.VECSXP]\n",
       "R classes: ('data.frame',)\n",
       "[IntSexpVector, IntSexpVector, IntSexpVector]\n",
       "  z: <class 'rpy2.rinterface.IntSexpVector'>\n",
       "  <rpy2.rinterface.IntSexpVector object at 0x7fe616ae6c40> [RTYPES.INTSXP]\n",
       "  a: <class 'rpy2.rinterface.IntSexpVector'>\n",
       "  <rpy2.rinterface.IntSexpVector object at 0x7fe616c27e80> [RTYPES.INTSXP]\n",
       "  b: <class 'rpy2.rinterface.IntSexpVector'>\n",
       "  <rpy2.rinterface.IntSexpVector object at 0x7fe616aeb0c0> [RTYPES.INTSXP]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataf.spread('x', 'y')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6321ff00",
   "metadata": {},
   "source": [
    "**Reuse. Get things done. Don't reimplement.**"
   ]
  }
 ],
 "metadata": {
  "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
