{"id":7550,"date":"2022-06-14T00:00:00","date_gmt":"2022-06-14T00:00:00","guid":{"rendered":"https:\/\/academy.deepsim.carun-r-in-jupyter-notebook-e4d9e68c5588\/"},"modified":"2022-11-25T04:02:29","modified_gmt":"2022-11-25T04:02:29","slug":"run-r-in-jupyter-notebook-e4d9e68c5588","status":"publish","type":"post","link":"https:\/\/academy.deepsim.ca\/zh\/run-r-in-jupyter-notebook-e4d9e68c5588\/","title":{"rendered":"\u5728Jupyter Notebook\u4e2d\u8fd0\u884cR"},"content":{"rendered":"<p id=\"f8c4\" class=\"graf graf--p graf-after--h3\">&#8211; with Python Kernel or R Kernel<\/p>\n<p id=\"d2ce\" class=\"graf graf--p graf-after--p\"><a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/jupyter.org\/\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/jupyter.org\/\" data->Jupyter notebook<\/a> is an open-source web application, where one can create and share documents that contain live code, math equations, visualizations and narrative text. The applications include data cleaning and transformation, numerical simulation, statistical modelling, data visualization, machine learning, and much more.<\/p>\n<p id=\"6025\" class=\"graf graf--p graf-after--p\">The Jupyter system supports over 100 programming languages (called \u201ckernels\u201d in the Jupyter ecosystem) including Python, Java, R, Julia, Matlab, Scala, and many more. You can learn these \u201ckernels\u201d in the Jupyter-kernels repository in GitHub (<a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/github.com\/jupyter\/jupyter\/wiki\/Jupyter-kernels\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/github.com\/jupyter\/jupyter\/wiki\/Jupyter-kernels\" data->https:\/\/github.com\/jupyter\/jupyter\/wiki\/Jupyter-kernels<\/a>).<\/p>\n<h3 id=\"cb4c\" class=\"graf graf--h3 graf-after--p\">R Language<\/h3>\n<p id=\"6605\" class=\"graf graf--p graf-after--h3\">R is an interpreted programming language for statistical computing and graphics, which is widely used among statisticians and data miners for developing statistical software and data analysis.<\/p>\n<p id=\"30e5\" class=\"graf graf--p graf-after--p\">There are several ways to set up Jupyter Notebook for R. We will learn two straightforward ways as follows:<\/p>\n<h3 id=\"0b48\" class=\"graf graf--h3 graf-after--p\">Use Python\u00a0Kernel<\/h3>\n<p id=\"6775\" class=\"graf graf--p graf-after--h3\">This way allows us to run R and Python together in the same notebook.<\/p>\n<h4 id=\"934d\" class=\"graf graf--h4 graf-after--p\">(1) Installation Requirements<\/h4>\n<ul class=\"postList\">\n<li id=\"d139\" class=\"graf graf--li graf-after--h4\">Has Python installed in version 3.5 or higher<\/li>\n<li id=\"5c29\" class=\"graf graf--li graf-after--li\">Install Anaconda, or<\/li>\n<li id=\"b817\" class=\"graf graf--li graf-after--li\">Install traditional Python<\/li>\n<li id=\"0da3\" class=\"graf graf--li graf-after--li\">Has R installed in version 3.2 or higher<\/li>\n<li id=\"5954\" class=\"graf graf--li graf-after--li\">Has Jupyter Notebook installed<\/li>\n<\/ul>\n<h4 id=\"2a72\" class=\"graf graf--h4 graf-after--li\">(2) Two Ways to Install\u00a0R<\/h4>\n<p id=\"9f4f\" class=\"graf graf--p graf-after--h4\">The first way is to go to R web to download R installer to install it if have classical Python installed on your system. If you have Anaconda Python, you can install R by Conda as follows:<br \/>\n<code class=\"markup--code markup--p-code\">conda install -c r r-essentials<\/code><\/p>\n<h4 id=\"27f7\" class=\"graf graf--h4 graf-after--p\">(3) Install Jupyter\u00a0Notebook<\/h4>\n<p id=\"44be\" class=\"graf graf--p graf-after--h4\">Installing Jupyter notebook by:<br \/>\n<code class=\"markup--code markup--p-code\">pip install notebook<\/code><\/p>\n<p id=\"d47f\" class=\"graf graf--p graf-after--p\">or install it using conda for Anaconda installer:<br \/>\n<code class=\"markup--code markup--p-code\">conda install notebook<\/code><\/p>\n<h4 id=\"243f\" class=\"graf graf--h4 graf-after--p\">(4) Install rpy2\u00a0library<\/h4>\n<ul class=\"postList\">\n<li id=\"6cb8\" class=\"graf graf--li graf-after--h4\">install numpy<br \/>\n<code class=\"markup--code markup--li-code\">pip install numpy<\/code><\/li>\n<li id=\"a98a\" class=\"graf graf--li graf-after--li\">install pandas<br \/>\n<code class=\"markup--code markup--li-code\">pip install pandas<\/code><\/li>\n<li id=\"cae7\" class=\"graf graf--li graf-after--li\">install rpy2 library<br \/>\n<code class=\"markup--code markup--li-code\">pip install rpy2<\/code> or<br \/>\n<code class=\"markup--code markup--li-code\">conda install rpy2<\/code><\/li>\n<\/ul>\n<p id=\"bb44\" class=\"graf graf--p graf-after--li\">After installing rpy2, we need to enable the<strong class=\"markup--strong markup--p-strong\"> %%R <\/strong>magic.<\/p>\n<pre id=\"582a\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">%load_ext<\/strong> rpy2.ipython<\/pre>\n<p id=\"715e\" class=\"graf graf--p graf-after--pre\">We only need to run it once for the first time. After these installations, the Jupyter Notebook now supports both Python 3 and R programming languages<\/p>\n<h4 id=\"4a2e\" class=\"graf graf--h4 graf-after--p\">(3) Install R\u00a0packages<\/h4>\n<p id=\"734e\" class=\"graf graf--p graf-after--h4\">We can install R packages directly in the Jupyter notebook. For example, we install \u2018ggplot2\u2019 by the following command:<\/p>\n<pre id=\"00b4\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">\r\n<code>%%<\/code><\/strong><code>R\r\ninstall.packages('ggplot2')<\/code><\/pre>\n<p id=\"2294\" class=\"graf graf--p graf-after--pre\">Then it will ask you to select a CRAN mirror for use, you just select your nearest CRAN mirror.<\/p>\n<p id=\"b88f\" class=\"graf graf--p graf-after--p\">We can also name a CRAN mirror for use in the command directly. For example, let\u2019s choose \u2018http:\/\/cran.us.r-project.org&#8217;, then we can type the following code to run:<\/p>\n<pre id=\"7382\" class=\"graf graf--pre graf-after--p\"><code><strong class=\"markup--strong markup--pre-strong\">%%<\/strong>R\r\ninstall.packages('ggplot2',repos<strong class=\"markup--strong markup--pre-strong\">=<\/strong>'http:\/\/cran.us.r-project.org', quiet<strong class=\"markup--strong markup--pre-strong\">=TRUE<\/strong>)<\/code><\/pre>\n<p id=\"2704\" class=\"graf graf--p graf-after--pre\">After successful installation, we can load the package using the following command:<\/p>\n<pre id=\"d05e\" class=\"graf graf--pre graf-after--p\"><code><strong class=\"markup--strong markup--pre-strong\">%%<\/strong>R\r\nlibrary(ggplot2)<\/code><\/pre>\n<p id=\"4a14\" class=\"graf graf--p graf-after--pre\">Let\u2019s see a concrete example in the following section.<\/p>\n<h4 id=\"fda1\" class=\"graf graf--h4 graf-after--p\">(4) Example<\/h4>\n<p id=\"0d01\" class=\"graf graf--p graf-after--h4\">In this example, we will import Python Pandas and NumPy libraries and create a pandas DataFrame.<\/p>\n<pre id=\"f4ae\" class=\"graf graf--pre graf-after--p\"><code><strong class=\"markup--strong markup--pre-strong\">import<\/strong> pandas <strong class=\"markup--strong markup--pre-strong\">as<\/strong> pd\r\n\r\ndf <strong class=\"markup--strong markup--pre-strong\">=<\/strong> pd<strong class=\"markup--strong markup--pre-strong\">.<\/strong>DataFrame({\r\n    'x_var': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],\r\n    'y_var': [3, 5, 7, 6, 9, 8, 10, 12, 13, 11]\r\n})<\/code><\/pre>\n<pre id=\"04ab\" class=\"graf graf--pre graf-after--pre\"><code>df<\/code><\/pre>\n<p id=\"4fe0\" class=\"graf graf--p graf-after--pre\">After running the above code, we get something as:<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/08\/1LovTEKQ8ceYhu0BhndLHIw.png\" alt=\"\" \/><figcaption><\/figcaption><\/figure>\n<p id=\"abe4\" class=\"graf graf--p graf-after--figure\">So far, it is pure Python code. Next, we use R to make a plot using \u201cggplot2&#8242; for the above data table, for example.<\/p>\n<pre id=\"c23f\" class=\"graf graf--pre graf-after--p\"><code><strong class=\"markup--strong markup--pre-strong\">%%<\/strong>R <strong class=\"markup--strong markup--pre-strong\">-<\/strong>i df <strong class=\"markup--strong markup--pre-strong\">-<\/strong>w 3 <strong class=\"markup--strong markup--pre-strong\">-<\/strong>h 3 <strong class=\"markup--strong markup--pre-strong\">--<\/strong>units in <strong class=\"markup--strong markup--pre-strong\">-<\/strong>r 200<\/code><\/pre>\n<pre id=\"ab2a\" class=\"graf graf--pre graf-after--pre\"><code>install.packages('ggplot2',repos<strong class=\"markup--strong markup--pre-strong\">=<\/strong>'http:\/\/cran.us.r-project.org', quiet<strong class=\"markup--strong markup--pre-strong\">=TRUE<\/strong>)<\/code><\/pre>\n<pre id=\"c0ca\" class=\"graf graf--pre graf-after--pre\"><code>library(ggplot2)\r\n\r\nggplot(data <strong class=\"markup--strong markup--pre-strong\">=<\/strong> df, aes(x <strong class=\"markup--strong markup--pre-strong\">=<\/strong> x_var, y <strong class=\"markup--strong markup--pre-strong\">=<\/strong> y_var)) <strong class=\"markup--strong markup--pre-strong\">+<\/strong>\r\n  geom_point(color <strong class=\"markup--strong markup--pre-strong\">=<\/strong> 'red', size <strong class=\"markup--strong markup--pre-strong\">=<\/strong> 4) <strong class=\"markup--strong markup--pre-strong\">+<\/strong>\r\n    stat_smooth()<\/code><\/pre>\n<p id=\"6a6e\" class=\"graf graf--p graf-after--pre\">In the first line, we call R using \u2018%%R\u2019 magic, where<\/p>\n<ul class=\"postList\">\n<li id=\"16d5\" class=\"graf graf--li graf-after--p\"><em class=\"markup--em markup--li-em\">i <\/em>is for \u201cinput\u201d<\/li>\n<li id=\"302e\" class=\"graf graf--li graf-after--li\"><em class=\"markup--em markup--li-em\">df<\/em> is the Pandas dataframe<\/li>\n<li id=\"5f1c\" class=\"graf graf--li graf-after--li\"><em class=\"markup--em markup--li-em\">-w and -h <\/em>define figure size, 3 by 3 inches in this example<\/li>\n<\/ul>\n<p id=\"83f8\" class=\"graf graf--p graf-after--li\"><strong class=\"markup--strong markup--p-strong\"><em class=\"markup--em markup--p-em\">\u2014 <\/em><\/strong><em class=\"markup--em markup--p-em\">units in <\/em><strong class=\"markup--strong markup--p-strong\"><em class=\"markup--em markup--p-em\">&#8211;<\/em><\/strong><em class=\"markup--em markup--p-em\">r<\/em> define size unit in resolution, say 200 dpi resolution in this example. Unit can be changed to px, cm, etc<\/p>\n<p id=\"4af3\" class=\"graf graf--p graf-after--p\">So the first line can be described as<em class=\"markup--em markup--p-em\"> \u201cWe pass data df as input to R, and then make default figure size 3 by 3 inches with 200 dpi resolution.\u201d<\/em><\/p>\n<p id=\"14a5\" class=\"graf graf--p graf-after--p\">The next two lines is to install package \u201cggplot2\u201d, which we have already learnt in previous section. Here just show you how to do this in one code snippet. The following line is to load the package, which we are also clear. The last few lines to make a plot using \u201cggplot2\u201d. The output looks like the following screenshot.<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/875\/1*TBmGa2-s0YJtEqZtEa0ZPA.png\" \/><figcaption><\/figcaption><\/figure>\n<h3 id=\"807f\" class=\"graf graf--h3 graf-after--figure\">Use R\u00a0kernel<\/h3>\n<h4 id=\"1a26\" class=\"graf graf--h4 graf-after--h3\">(1) Install R kernel for Jupyter\u00a0Notebook<\/h4>\n<p id=\"de4d\" class=\"graf graf--p graf-after--h4\"><code class=\"markup--code markup--p-code\">IRkernel<\/code> can be installed via the Comprehensive R Archive Network [CRAN].<\/p>\n<pre id=\"befb\" class=\"graf graf--pre graf-after--p\"><code><strong class=\"markup--strong markup--pre-strong\">%%<\/strong>R \r\ninstall.packages('IRkernel')<\/code><\/pre>\n<h4 id=\"1318\" class=\"graf graf--h4 graf-after--pre\">(2) Making the kernel available to\u00a0Jupyter<\/h4>\n<p id=\"5a7f\" class=\"graf graf--p graf-after--h4\"><code class=\"markup--code markup--p-code\">IRkernel::installspec()<\/code> will install a kernel with the name \u201cir\u201d and a display name of \u201cR\u201d.<\/p>\n<p id=\"997f\" class=\"graf graf--p graf-after--p\">By default, it installs the kernel per-user. To install system-wide, set user to False in the installspec command:<\/p>\n<pre id=\"2cff\" class=\"graf graf--pre graf-after--p\"><code><strong class=\"markup--strong markup--pre-strong\">%%<\/strong>R\r\nIRkernel<strong class=\"markup--strong markup--pre-strong\">::<\/strong>installspec(user <strong class=\"markup--strong markup--pre-strong\">=<\/strong> <strong class=\"markup--strong markup--pre-strong\">FALSE<\/strong>) <em class=\"markup--em markup--pre-em\"># to register the kernel in the current R installation<\/em><\/code><\/pre>\n<h4 id=\"0d37\" class=\"graf graf--h4 graf-after--pre\">(3) Create R Jupyter\u00a0Notebook<\/h4>\n<p id=\"074a\" class=\"graf graf--p graf-after--h4\">Create a new notebook and select R kernel as follows.<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/875\/1*inH8O8s4HGCHEEtdpa2-0g.png\" \/><figcaption><\/figcaption><\/figure>\n<p id=\"89b3\" class=\"graf graf--p graf-after--figure\">Then copy the following code in the cell.<\/p>\n<pre id=\"ac11\" class=\"graf graf--pre graf-after--p\"><code>n <strong class=\"markup--strong markup--pre-strong\">&lt;-<\/strong> floor(rnorm(10000, mean <strong class=\"markup--strong markup--pre-strong\">=<\/strong> 200, sd <strong class=\"markup--strong markup--pre-strong\">=<\/strong> 50))\r\nt <strong class=\"markup--strong markup--pre-strong\">&lt;-<\/strong> table(n)\r\nbarplot(t)<\/code><\/pre>\n<p id=\"849e\" class=\"graf graf--p graf-after--pre\">Run jupyter notebook, you will see the following outcome in Jupyter notebook.<\/p>\n<figure><img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/809\/1*Ce9-gooEgzyG8EkGJKI9PA.png\" \/><figcaption><\/figcaption><\/figure>\n<h3 id=\"0072\" class=\"graf graf--h3 graf-after--figure\">Conclusion<\/h3>\n<p id=\"b8ae\" class=\"graf graf--p graf-after--h3\">In this article, two methods of running R in Jupyter notebook. One method is to install Python rpy2 library, and another method is to install R kernel for Jupyter Notebook. The differences between these methods are that we can run Python code and R in the same Jupyter notebook for the first methods, while we can only run R code in a separate Jupyter notebook for the second method.<\/p>\n<p id=\"06e9\" class=\"graf graf--p graf-after--p\">Besides, you can watch a video version of this paper as follows. Please subscribe for more tips by click: <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/www.youtube.com\/channel\/UCQpUJn9GfUjKkFP2X-1pjoQ?sub_confirmation=1\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/www.youtube.com\/channel\/UCQpUJn9GfUjKkFP2X-1pjoQ?sub_confirmation=1\" data->https:\/\/www.youtube.com\/channel\/UCQpUJn9GfUjKkFP2X-1pjoQ?sub_confirmation=1<\/a><\/p>\n<p id=\"03cb\" class=\"graf graf--p graf-after--figure\">If you are interested in learning Jupyter notebook systematically, welcome to my following tutorial and online course:<\/p>\n<p id=\"bdec\" class=\"graf graf--p graf-after--p\">(1)<strong class=\"markup--strong markup--p-strong\"><em class=\"markup--em markup--p-em\"> 22 Pretty Handy Features of Jupyter Notebook: <\/em><\/strong><a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/www.youtube.com\/watch?v=xEI3065M-3g&amp;t=3s\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/www.youtube.com\/watch?v=xEI3065M-3g&amp;t=3s\" data->https:\/\/www.youtube.com\/watch?v=xEI3065M-3g&amp;t=3s<\/a><\/p>\n<p id=\"4808\" class=\"graf graf--p graf-after--p graf--trailing\">(2) <strong class=\"markup--strong markup--p-strong\"><em class=\"markup--em markup--p-em\">Practical Jupyter Notebook from Beginner to Expert: <\/em><\/strong> <a href=\"https:\/\/academy.deepsim.cacourses\/practical-jupyter-notebook-from-beginner-to-expert\/\">https:\/\/academy.deepsim.cacourses\/practical-jupyter-notebook-from-beginner-to-expert\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8211; with Python Kernel or R Kernel Jupyter notebook is an open-source web application, where one can create and share<\/p>","protected":false},"author":2,"featured_media":7644,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71,64,70],"tags":[63,72],"class_list":["post-7550","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","category-programming","category-r","tag-jupyter-notebook","tag-r"],"_links":{"self":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts\/7550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/comments?post=7550"}],"version-history":[{"count":0,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts\/7550\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/media\/7644"}],"wp:attachment":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/media?parent=7550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/categories?post=7550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/tags?post=7550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}