{"id":129507,"date":"2020-07-22T11:11:39","date_gmt":"2020-07-22T15:11:39","guid":{"rendered":"http:\/\/www.bu.edu\/tech\/?page_id=129507"},"modified":"2022-06-10T13:40:53","modified_gmt":"2022-06-10T17:40:53","slug":"r-faq","status":"publish","type":"page","link":"https:\/\/www.bu.edu\/tech\/support\/research\/software-and-programming\/common-languages\/r-basics\/r-faq\/","title":{"rendered":"R Frequently Asked Questions"},"content":{"rendered":"<p><!-- ************************************************************ --><\/p>\n<h2>What version of R do I run?<\/h2>\n<p>You can find this information by running <code>R.version.string<\/code> at the R prompt:<\/p>\n<pre class=\"code-block\"><code><span class=\"comment\"># get version of R<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">R.version.string<\/span>\r\n<span class=\"output\">[1] R version 4.1.2 (2021-11-01)<\/span><\/code><\/pre>\n<p><!-- ************************************************************ --><\/p>\n<h2>What R packages do I already have?<\/h2>\n<p>To get a list of installed packages, execute <code>library()<\/code> at the R prompt. R will first list all the packages installed in your local R directory and then it will list all of the packages installed globally on your system:<\/p>\n<pre class=\"code-block\"><code><span class=\"comment\"># list all R packages installed<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">library()<\/span>\r\n\r\n<span class=\"output\">Packages in library '\/usr1\/scv\/ktrn\/R\/x86_64-pc-linux-gnu-library\/4.1':\r\n\r\ncombinat\t\tcombinatorics utilities\r\nproftools\t\tOutput Processing Tools for R\r\nrgl                     3D visualization device system (OpenGL)\r\n\r\nPackages in library '\/share\/pkg.7\/r\/4.1.2\/install\/lib64\/R\/library':\r\n\r\nKernSmooth              Functions for kernel smoothing for Wand &amp; Jones (1995)\r\nMASS                    Support Functions and Datasets for Venables and Ripley's MASS <\/span><\/code><\/pre>\n<p><!-- ************************************************************ --><\/p>\n<h2>How can I find additional information about R packages I have?<\/h2>\n<p>If you would like to get additional information about the installed packages you can access it using <code>installed.packages()<\/code> at the R prompt. A more concise output can be obtained running <code>installed.packages()[ ,c(1,3,4)]<\/code>. This will include the name of the package, its version and \u201cpriority\u201d information:<\/p>\n<pre class=\"code-block\"><code><span class=\"comment\"># Obtain information about installed R packages<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">installed.packages()[ ,c(1,3,4)]<\/span>\r\n\r\n<span class=\"output\">           Package      Version       Priority\r\nrtweet     \"rtweet\"     \"0.7.0\"       NA\r\nMASS       \"MASS\"       \"7.3-51.6\"    \"recommended\"\r\nMatrix     \"Matrix\"     \"1.2-18\"      \"recommended\"\r\nbase       \"base\"       \"4.1.2\"       \"base\"\r\n...      <\/span><\/code><\/pre>\n<p>If priority is <code>\u201cbase\u201d<\/code>, the package is already loaded into your workspace, so all its functions are available upon opening R.<br \/>\nIf priority is <code>\u201crecommended\u201d<\/code>, then the package was installed with base R.<br \/>\nIf priority is <code>\u201cNA\u201d<\/code>, then the package is an optional package. Both &#8220;recommended&#8221; and &#8220;NA&#8221; packages have to be loaded using <code>library()<\/code> command before using them.<\/p>\n<p><!-- ************************************************************ --><\/p>\n<h2>How do I install a new package?<\/h2>\n<p><b>Method 1: Install from CRAN directly<\/b>. A package can be installed using <code>install.packages(\"<span class=\"placeholder\">package name<\/span>\")<\/code> at the R prompt. You can also provide a pathname to install R package at a specific location: <code>install.packages(\"<span class=\"placeholder\">package name<\/span>\", lib=\"<span class=\"placeholder\">\/my\/own\/R-packages\/<\/span>\")<\/code>. If the argument <code>lib<\/code>\u00a0is omitted it defaults to the first directory in <code>.libPaths()<\/code>.<\/p>\n<pre class=\"code-block\"><code><span class=\"comment\"># Install dplyr package<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">install.packages(\"dplyr\")<\/span>\r\n\r\n<span class=\"output\"><\/span><\/code><b>Method 2: Install from source<\/b>. At the Linux prompt download the R package <span class=\"placeholder\">my_package.tar.gz<\/span> and use <code>R CMD INSTALL<\/code> command:<\/pre>\n<pre class=\"code-block\"><code><span class=\"comment\"># Download rgl package<\/span>\r\n<span class=\"prompt\">scc1% <\/span><span class=\"command\">wget <a href=\"https:\/\/cran.microsoft.com\/snapshot\/2021-09-26\/src\/contrib\/dplyr_1.0.7.tar.gz\">http:\/\/cran.r-project.org\/src\/contrib\/dplyr_1.0.7.tar.gz<\/a><\/span>\r\n\r\n<span class=\"comment\"># Install rgl package<\/span>\r\n<span class=\"prompt\">scc1% <\/span><span class=\"command\">R CMD INSTALL dplyr_1.0.7.tar.gz<\/span>\r\n<\/code><\/pre>\n<p><b>Note:<\/b> Regardless of the method you used to install the package, it has to be loaded into the workspace before you can actually use it. To load it, type <code>library(<span class=\"placeholder\">my_package<\/span>)<\/code> at the R prompt.<\/p>\n<p><!-- ************************************************************ --><\/p>\n<h2>How can I remove a package?<\/h2>\n<p>A package can be removed using <code>remove.packages(\"<span class=\"placeholder\">package name<\/span>\")<\/code> at the R prompt:<\/p>\n<pre class=\"code-block\"><code><span class=\"comment\"># Remove rgl package<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">remove.packages(\"dplyr\")<\/span>\r\n<\/code><\/pre>\n<p><!-- ************************************************************ --><\/p>\n<h2>How can I check if there are updates for the installed packages?<\/h2>\n<p>To check for updates run <code>old.packages()<\/code> at the R prompt. You will be asked to specify the CRAN mirror. Then R will list all the projects that have a newer version.<\/p>\n<pre class=\"code-block\"><code><span class=\"comment\"># Check for updates<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">old.packages() <\/span>\r\n\r\n<span class=\"output\">--- Please select a CRAN mirror for use in this session ---\r\nLoading Tcl\/Tk interface ... done \r\n...      <\/span><\/code><\/pre>\n<p><!-- ************************************************************ --><\/p>\n<h2>How can I update a package?<\/h2>\n<p>To install a newer version of a package run <code>update.packages()<\/code> at the R prompt. R will list all the packages that have newer versions and will offer to download and install the updates.<\/p>\n<pre class=\"code-block\"><code><span class=\"comment\"># Update all outdated packages<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">update.packages() <\/span>\r\n\r\n...\r\n\r\n<span class=\"comment\"># Update dplyr package only<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">update.packages( oldPkgs=\"dplyr\" ) <\/span> \r\n\r\n<span class=\"output\">--- Please select a CRAN mirror for use in this session ---\r\nLoading Tcl\/Tk interface ... done \r\n...      \r\n<\/span><\/code><\/pre>\n<p><!-- ************************************************************ --><\/p>\n<p><!-- ************************************************************ --><\/p>\n<h2>What is the default path R uses to install new packages?<\/h2>\n<p>You can view the default path R is using to install new packages running <code>.libPaths()<\/code> at the R prompt:<\/p>\n<pre class=\"code-block\"><code><span class=\"comment\"># Get the default R paths<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">.libPaths()<\/span>\r\n\r\n<span class=\"output\">[1] \"\/usr1\/scv\/ktrn\/R\/x86_64-pc-linux-gnu-library\/4.0\"\r\n[2] \"\/share\/pkg.7\/r\/4.1.2\/install\/lib64\/R\/library\"<\/span><\/code><\/pre>\n<p><!-- ************************************************************ --><\/p>\n<h2>How can I run another R version installed on the SCC?<\/h2>\n<p>You can list and run non-default versions of R available on the cluster, running the following commands at the Linux prompt:<\/p>\n<pre class=\"code-block\"><code><span class=\"comment\"># List all R versions installed on the SCC<\/span>\r\n<span class=\"prompt\">scc1% <\/span><span class=\"command\">module spider R<\/span>\r\n<\/code><\/pre>\n<h2>How do I upgrade the packages I have installed so that they are accessible by the new version of R? <a name=\"UPGRADE\"><\/a><\/h2>\n<p>Below is a simple process for installing the updated versions of all of your packages into a new version of R.<\/p>\n<p>1. Run the old version of R you have been using:<\/p>\n<pre class=\"code-block\"><code><span class=\"prompt\">scc1% <\/span><span class=\"command\">module load R\/4.0.2<\/span>\r\n<span class=\"prompt\">scc1% <\/span><span class=\"command\">R<\/span><\/code><\/pre>\n<p>2. Run the following code in the old version of R:<\/p>\n<pre class=\"code-block\"><code><span class=\"prompt\">&gt; <\/span><span class=\"command\">packages &lt;- installed.packages()[,\"Package\"]<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">save(packages, file=\"Rpackages.Rds\")<\/span><\/code><\/pre>\n<p>3. Run the new version of R:<\/p>\n<pre class=\"code-block\"><code><span class=\"prompt\">scc1% <\/span><span class=\"command\">module load R\/4.1.2<\/span>\r\n<span class=\"prompt\">scc1% <\/span><span class=\"command\">R<\/span><\/code><\/pre>\n<p>4. Run the following code in the new version of R:<\/p>\n<pre class=\"code-block\"><code><span class=\"prompt\">&gt; <\/span><span class=\"command\">load(\"Rpackages\")<\/span>\r\n<span class=\"prompt\">&gt; <\/span><span class=\"command\">install.packages(packages)<\/span><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What version of R do I run? You can find this information by running R.version.string at the R prompt: # get version of R &gt; R.version.string [1] R version 4.1.2 (2021-11-01) What R packages do I already have? To get a list of installed packages, execute library() at the R prompt. R will first list&#8230;<\/p>\n","protected":false},"author":1692,"featured_media":0,"parent":51114,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/129507"}],"collection":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/users\/1692"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/comments?post=129507"}],"version-history":[{"count":9,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/129507\/revisions"}],"predecessor-version":[{"id":140766,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/129507\/revisions\/140766"}],"up":[{"embeddable":true,"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/pages\/51114"}],"wp:attachment":[{"href":"https:\/\/www.bu.edu\/tech\/wp-json\/wp\/v2\/media?parent=129507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}