如何在Ubuntu 18.04上安裝R

2020-08-13 16:05:58

A previous version of this tutorial was written by Melissa Anderson.

本教學的先前版本由Melissa Anderson編寫。

介紹 (Introduction)

R is an open-source programming language that specializes in statistical computing and graphics. Supported by the R Foundation for Statistical Computing, it is widely used for developing statistical software and performing data analysis. An increasingly popular and extensible language with an active community, R offers many user-generated packages for specific areas of study, which makes it applicable to many fields.

R是一種開放原始碼程式語言,專門從事統計計算和圖形。 在R統計計算基金會的支援下,它廣泛用於開發統計軟體和執行數據分析。 R具有活躍的社羣,是一種日益流行和可延伸的語言,它爲特定的研究領域提供了許多使用者生成的軟體包,從而使其可應用於許多領域。

In this tutorial, we will install R and show how to add packages from the official Comprehensive R Archive Network (CRAN).

在本教學中,我們將安裝R並演示如何從官方的Integrated R Archive Network(CRAN)新增軟體包。

先決條件 (Prerequisites)

To follow along with this tutorial, you will need an Ubuntu 18.04 server with:

要繼續學習本教學,您將需要具有以下功能的Ubuntu 18.04伺服器:

  • at least 1GB of RAM

    至少 1GB的RAM

  • a non-root user with sudo privileges

    具有sudo特權的非root使用者

To learn how to achieve this setup, follow our manual initial server setup guide or run our automated script.

要瞭解如何實現此設定,請遵循我們的手動初始伺服器設定指南或執行我們的自動化指令碼

Once these prerequisites are in place, you’re ready to begin.

一旦滿足這些先決條件,就可以開始了。

第1步-安裝R (Step 1 — Installing R)

Because R is a fast-moving project, the latest stable version isn’t always available from Ubuntu’s repositories, so we’ll start by adding the external repository maintained by CRAN.

因爲R是一個快速發展的專案,所以Ubuntu的儲存庫中並不總是提供最新的穩定版本,因此我們將從新增CRAN維護的外部儲存庫開始。

Note: CRAN maintains the repositories within their network, but not all external repositories are reliable. Be sure to install only from trusted sources.

注意: CRAN在其網路內維護儲存庫,但並非所有外部儲存庫都是可靠的。 確保僅從受信任的來源進行安裝。

Let’s first add the relevant GPG key.

首先新增相關的GPG金鑰。

  • sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

    sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

When we run the command, we’ll receive the following output:

執行命令時,將收到以下輸出:

Output
Executing: /tmp/apt-key-gpghome.4BZzh1TALq/gpg.1.sh --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9 gpg: key 51716619E084DAB9: public key "Michael Rutter <[email protected]>" imported gpg: Total number processed: 1 gpg: imported: 1

Once we have the trusted key, we can add the repository.

一旦有了可信金鑰,就可以新增儲存庫。

Note that if you’re not using 18.04, you can find the relevant repository from the R Project Ubuntu list, named for each release. Ubuntu 18.04 is named Bionic Beaver, and the most recent version of R is 4.0, hence the naming convention of ubuntu bionic-cran40.

請注意,如果您不使用18.04,則可以從R Project Ubuntu列表 (爲每個版本命名)中找到相關的儲存庫。 Ubuntu 18.04名爲Bionic Beaver,R的最新版本是4.0,因此是ubuntu bionic-cran40的命名約定。

  • sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/'

    sudo add-apt-repository'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40 /'

Among the output that displays, you should identify lines similar to the following:

在顯示的輸出中,您應該標識類似於以下內容的行:

Output
... Get:4 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3626 B] ... Get:22 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ Packages [20.4 kB] ...

Now, we’ll need to run update after this in order to include package manifests from the new repository.

現在,我們需要在此之後執行update ,以便包含來自新儲存庫的軟體包清單。

  • sudo apt update

    sudo apt更新

Among the output should be a line similar to the following:

輸出中應有類似於以下內容的行:

Output
... Hit:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease ...

If the line above appears in the output from the update command, we’ve successfully added the repository. We can be sure we won’t accidentally install an older version.

如果以上行出現在update命令的輸出中,則說明我們已經成功新增了儲存庫。 我們可以確定不會意外安裝舊版本。

At this point, we’re ready to install R with the following command.

至此,我們準備使用以下命令安裝R。

  • sudo apt install r-base

    sudo apt安裝r-base

If prompted to confirm installation, press y to continue.

如果提示您確認安裝,請按y繼續。

As of the time of writing, the latest stable version of R from CRAN is 4.0.1, which is displayed when you start R.

在撰寫本文時,CRAN的R的最新穩定版本是4.0.1,在您啓動R時會顯示該版本。

Since we’re planning to install an example package for every user on the system, we’ll start R as root so that the libraries will be available to all users automatically. Alternatively, if you run the R command without sudo, a personal library can be set up for your user.

由於我們計劃爲系統上的每個使用者安裝一個範例程式包,因此我們將以root身份啓動R,以便所有使用者可以自動使用這些庫。 另外,如果您在不使用sudo情況下執行R命令,則可以爲您的使用者設定一個個人庫。

  • sudo -i R

    须藤-i R
Output
R version 4.0.0 (2020-04-24) -- "Arbor Day" Copyright (C) 2020 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) ... Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. >

This confirms that we’ve successfully installed R and entered its interactive shell.

這確認我們已經成功安裝了R並進入了它的互動式外殼。

步驟2 —從CRAN安裝R軟體包 (Step 2 — Installing R Packages from CRAN)

Part of R’s strength is its available abundance of add-on packages. For demonstration purposes, we’ll install txtplot, a library that outputs ASCII graphs that include scatterplot, line plot, density plot, acf and bar charts:

R的部分優勢在於其豐富的附加軟體包。 出於演示目的,我們將安裝txtplot ,該庫輸出ASCII圖,包括散點圖,折線圖,密度圖,acf和條形圖:

  • install.packages('txtplot')

    install.packages('txtplot')

Note: The following output shows where the package will be installed.

注意:以下輸出顯示了軟體包的安裝位置。

Output
... Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) . . .

This site-wide path is available because we ran R as root. This is the correct location to make the package available to all users.

該站點範圍的路徑可用,因爲我們以root身份執行R。 這是使軟體包對所有使用者可用的正確位置。

When the installation is complete, we can load txtplot:

安裝完成後,我們可以載入txtplot

  • library('txtplot')

    庫('txtplot')

If there are no error messages, the library has successfully loaded. Let’s put it in action now with an example which demonstrates a basic plotting function with axis labels. The example data, supplied by R’s datasets package, contains the speed of cars and the distance required to stop based on data from the 1920s:

如果沒有錯誤訊息,則說明庫已成功載入。 現在,通過一個範例來將其付諸實踐,該範例演示帶有軸標籤的基本繪圖功能。 由R的datasets軟體包提供的範例數據包含汽車的速度和根據1920年代的數據停止所需的距離

  • txtplot(cars[,1], cars[,2], xlab = 'speed', ylab = 'distance')

    txtplot(汽車[,1],汽車[,2],xlab ='速度',ylab ='距離')
Output
+----+-----------+------------+-----------+-----------+--+ 120 + * + | | d 100 + * + i | * * | s 80 + * * + t | * * * * | a 60 + * * * * * + n | * * * * * | c 40 + * * * * * * * + e | * * * * * * * | 20 + * * * * * + | * * * | 0 +----+-----------+------------+-----------+-----------+--+ 5 10 15 20 25 speed

If you are interested to learn more about txtplot, use help(txtplot) from within the R interpreter.

如果您想瞭解有關txtplot更多資訊,請在R直譯器中使用help(txtplot)

Any precompiled package can be installed from CRAN with install.packages(). To learn more about what’s available, you can find a listing of official packages organized by name via the Available CRAN Packages By Name list.

可以使用install.packages()從CRAN安裝任何預編譯的軟體包。 要瞭解有關可用功能的更多資訊,可以通過「 按名稱提供的CRAN軟體包」列表找到按名稱組織的正式軟體包列表

To exit R, you can type q(). Unless you want to save the workspace image, you can press n when prompted.

要退出R,可以鍵入q() 。 除非要儲存工作區影象,否則在出現提示時可以按n

結論 (Conclusion)

With R successfully installed on your server, you may be interested in this guide on installing the RStudio Server to bring an IDE to the server-based deployment you just completed. You can also learn how to set up a Shiny server to convert your R code into interactive web pages.

在伺服器上成功安裝R之後,您可能會對本指南的安裝RStudio伺服器感興趣,以將IDE帶入剛剛完成的基於伺服器的部署中。 您還可以學習如何設定Shiny伺服器,以將R程式碼轉換爲互動式網頁。

For more information on how to install R packages by leveraging different tools, you can read about how to install directly from GitHub, BitBucket or other locations. This will allow you to take advantage of the very latest work from the active community.

有關如何利用不同工具安裝R軟體包的更多資訊,您可以閱讀有關如何直接從GitHub,BitBucket或其他位置進行安裝的資訊 。 這將使您能夠利用活躍社羣中的最新作品。

翻譯自: https://www.digitalocean.com/community/tutorials/how-to-install-r-on-ubuntu-18-04