How do I get JupyterNotebook to support multiple programming languages?


Not happy that Jupyter Notebook only has a Python 2 environment, and planning to make it support Python 3 with R? No problem, this article helps you make that happen step by step.

(Some of the links in this article may not open correctly due to restrictions on external links to WeChat Public. If necessary, please click on the "Read the original article" button at the end of the article to access the version that displays the external links properly. )

queries

In the article "How to make a word cloud in Python", some sharp-eyed students noticed that when I created a new notebook in Jupyter Notebook, there were multiple options in the menu.

This means that I can just create new notebooks that support Python 2, Python 3, and even R.

But when you install Anaconda yourself, the option to create a new notebook is this.

You may immediately think it's unfair - why do I have so few options here? I'd like to get my Jupyter Notebook to support all 3 of these different programming environments at the same time, too!

In fact, Jupyter Notebook can support much more than just a few programming languages. The chart below is just a non-exhaustive list.

To see the full list, visit How to make a word cloud in Python .

In this article we will only discuss how to get Jupyter Notebook to support two programming languages, Python 3 and R. If you're a beginner, I'd recommend getting proficient with Python 2 before trying to migrate to Python 3. As for R, there are also excellent integrated development environments such as RStudio readily available.

Of course, if you're a hobbyist tosser, you're welcome to try following the steps in this article.

Python 3

First you need to save the entire contents of the current Jupyter Notebook and then switch to the Terminal or Command Prompt.

If the Jupyter Notebook was running before as shown, then follow the prompts and type the Control and C keys to exit the running Jupyter Notebook.

After that, type the command.

pip3 install ipykernel
python3 -m ipykernel install

Then, start Jupyter Notebook again

jupyter notebook

This time the menu for the new notebook will look like this.

Well, the programming environment for Python 3 in Jupyter Notebook is installed.

We can create a new Python 3 notebook and enter the following command.

1/2
print "abc"

If you are already familiar with Python 2, you should decide that the first statement outputs 0 (because both the divisor and the divisor are integers), and the second statement prints the string "abc".

In the Python 3 environment, however, the output looks like this.

If you also get this output, then congratulations, your Python 3 environment installation has completed successfully.

R

R is a very popular programming language for statistics majors. Although it was not originally designed to be a common language, because many statistical experts speak it as a native language, this group hates R in everything they do.

Over time, R has become ridiculously versatile. You write hundreds of lines of statements in C or Java, and when you get to R it's probably the same as using a calculator, a few lines of code. If you plan to use R well, be sure to learn how to find more efficient packages that will get you twice as much work.

If you don't already have R installed, go to How to make a word cloud with Python to choose a suitable download image. The links are listed inside the list by country region.

I usually choose the first option in the China section, which is the mirror of Tsinghua University. Once you click on the link to enter, you will see a page like this.

Depending on your system type, select the corresponding version from the different download links on the right for Linux, OS X and Windows, and follow the prompts to install it. It is recommended to use the default settings.

After installing R language, we install R language support in Jupyter Notebook.

First you still need to save the entire contents of the current Jupyter Notebook, and then switch to Terminal or Command Prompt.

Follow the prompts and type the Control and C keys to exit a running Jupyter Notebook. After that, type the command.

R

You'll see hints like this.

Just this.“>” Under the prompt, Enter the following statement:

install.packages('devtools')

R will prompt you to select the appropriate mirror.

Find the one with the word "China" in it. The only mirror from China in the list here is the Lanzhou University server.

Then continue with.

devtools::install_github('IRkernel/IRkernel')
IRkernel::installspec()
install.packages('ggplot2')

Okay, in order to exit the R environment, we type.

q()

Now, we're back under the terminal and execute

jupyter notebook

This time, we'll create a new notebook again, and we'll have an additional option like R.

Let's try it out and see if it works well.

Create a new R notebook. Name it whatever you want, I call it "test-r" here.

Then type the following statement and press "Shift+Enter" to execute it.

library("ggplot2")
ggplot(data = mtcars, aes(x = wt, y = mpg, color = cyl)) + geom_point() +
 geom_smooth(method="lm") +
 labs(main="Regression of MPG on Weight",
 xlab="Weight", ylab="Miles per Gallon")

If you see the results running as shown, it proves that everything is installed correctly.

The generated image looks like this.

The R language is powerful, right? It's clean and simple enough to write, right?

At this point, Jupyter Notebook now supports Python 2, Python 3 and R correctly. Happy coding, yo!

discussions

What other good Python & R development environments do you know besides Jupyter Notebook? What are their features compared to Jupyter Notebook? In the field of data science, which do you think is more promising, Python or R? Feel free to leave a comment and share your experience with the group and we'll share and discuss.



Recommended>>
1、ExclusiveIntegrated learning in one article with learning resources
2、Ten minutes to take you through the highlights of Google IO 2018 video in English
3、Microsoft AntiCross Site Scripting Library30RTM
4、Enable dockerAPI remote calls under mac
5、The Go js program on my blog

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号