{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic R in the Jupyter Notebook and RStudio" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first thing you should do with this notebook is make a copy! Go to the file menu and choose 'Make a Copy'." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Great! Now you should be working in a file called 'BasicRinJupyterAndRstudio-Copy1'*\n", "\n", "Briefly go back to the other tab and choose 'Close and Halt' from the file menu. Why did we do this? Because we have the lecture materials in something called a 'github repository'. We may need to make changes to notebooks and if and when we do, we will show you how to update your VM with the new materials. We want whatever changes you make to be saved under different files and this seems to be the easiest way. (Note: even if you don't think you are making changes, the notebook 'autosaves' - so your notebook will almost always be considered 'changed' as far as github is concerned... anyway - just trust me!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "R is a programming environment created specifically for statistics. It is a scripting language (if you don't know what that means, don't worry for now). R can be used interactively (as we will see in this notebook), or it can be told to execute a list of commands stored in a plain text file (called a 'script').\n", "\n", "From within the Jupyter notebook, we can access the R 'kernel' (the program that interprets R code and returns results). This is just one way to use R. We will also learn to use a program called Rstudio." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, the notebook is browser based (it opens a window in your browser) and works a lot like a web server. This notebook is running an R kernel, but we could chooose a Python kernel, a bash kernel (unix shell) or from a long list that is currently expanding:\n", "\n", "https://github.com/ipython/ipython/wiki/IPython-kernels-for-other-languages\n", "\n", "Be careful, though. Much of this is under development and considered 'beta' (or even alpha) - the tools can be buggy. We will be careful to use only the better developed parts of the Jupyter universe.\n", "\n", "The notebook is comprised of 'cells'. Cells can either be 'code' or 'markdown'. Code is for writing R commands. Markdown is for text and is an extension of html. This cell is a markdown cell.\n", "\n", "More about markdown here:\n", "\n", "https://en.wikipedia.org/wiki/Markdown\n", "\n", "You can type anything you want in markdown (though there are some special characters that will be interpreted as commands). \n", "\n", "Code cells require R syntax. The following cell is an R cell:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "4" ], "text/latex": [ "4" ], "text/markdown": [ "4" ], "text/plain": [ "[1] 4" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This is an R cell. The '#' tells R this is a comment\n", "3+1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When I run the code cell, it executes the R code (3+1) and returns the output in an output cell. To run a code cell, you can type shift-enter, or press the run button at the top of the screen." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### R Basics - Data Types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In any programming language, we have the notion of 'data modes' and 'data structures'. This is because programs manipulate data, and different kinds of data require different manipulations. For example, numbers are treated differently than characters (or strings of characters) and single numbers are treated differently than lists of numbers (vectors) or arrays of numbers (matrices).\n", "\n", "The following are some simple R data modes:\n", "\n", "* numeric\n", "* character\n", "* logical (TRUE or FALSE)\n", "* complex (we won't worry about these!)\n", "\n", "Modes can be combined to form data structures:\n", "\n", "* Vectors\n", "* Matrices\n", "* Strings\n", "* Data Frames\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Examples" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "'numeric'" ], "text/latex": [ "'numeric'" ], "text/markdown": [ "'numeric'" ], "text/plain": [ "[1] \"numeric\"" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class(c(1,3,2,8.4)) #This is a vector" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "'matrix'" ], "text/latex": [ "'matrix'" ], "text/markdown": [ "'matrix'" ], "text/plain": [ "[1] \"matrix\"" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class(matrix(c(1,3,2,4,5,6),nrow=2,ncol=3)) #This is a matrix" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "'This is a string!'" ], "text/latex": [ "'This is a string!'" ], "text/markdown": [ "'This is a string!'" ], "text/plain": [ "[1] \"This is a string!\"" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"This is a string!\"" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\n", "
c..This.is.a.string....This.is.another.string..X1X2X3
1This is a string135
2This is another string246
\n" ], "text/latex": [ "\\begin{tabular}{r|llll}\n", " & c..This.is.a.string....This.is.another.string.. & X1 & X2 & X3\\\\\n", "\\hline\n", "\t1 & This is a string & 1 & 3 & 5\\\\\n", "\t2 & This is another string & 2 & 4 & 6\\\\\n", "\\end{tabular}\n" ], "text/plain": [ " c..This.is.a.string....This.is.another.string.. X1 X2 X3\n", "1 This is a string 1 3 5\n", "2 This is another string 2 4 6" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.frame(c(\"This is a string\",\"This is another string\"),matrix(c(1:6),nrow=2,ncol=3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The important thing to note above is the combination of both character and numeric data into one object! That is what is special about data frames." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### R Basics - Creating Objects" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### c - Concatenate" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have just seen this command in action. The 'c' command combines objects by concatenation. For example:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 5
  2. \n", "\t
  3. 6
  4. \n", "\t
  5. 7
  6. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 5\n", "\\item 6\n", "\\item 7\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 5\n", "2. 6\n", "3. 7\n", "\n", "\n" ], "text/plain": [ "[1] 5 6 7" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c(5,6,7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "creates a vector of length 3. We can append to that vector, like so:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 5
  2. \n", "\t
  3. 6
  4. \n", "\t
  5. 7
  6. \n", "\t
  7. 8
  8. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 5\n", "\\item 6\n", "\\item 7\n", "\\item 8\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 5\n", "2. 6\n", "3. 7\n", "4. 8\n", "\n", "\n" ], "text/plain": [ "[1] 5 6 7 8" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c(c(5,6,7),8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, we would usually have named the first vector something:" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 5 6 7\n", "[1] 5 6 7 8\n" ] } ], "source": [ "v1<-c(5,6,7)\n", "v2<-c(v1,8)\n", "print(v1)\n", "print(v2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### rbind" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, if we would like to create a matrix, we could use the matrix command as above:" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\n", "
125
346
\n" ], "text/latex": [ "\\begin{tabular}{lll}\n", "\t 1 & 2 & 5\\\\\n", "\t 3 & 4 & 6\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "1. 1\n", "2. 3\n", "3. 2\n", "4. 4\n", "5. 5\n", "6. 6\n", "\n", "\n" ], "text/plain": [ " [,1] [,2] [,3]\n", "[1,] 1 2 5\n", "[2,] 3 4 6" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix(c(1,3,2,4,5,6),nrow=2,ncol=3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or, we could create two vectors and combine them:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\n", "
v1125
v2346
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", "\tv1 & 1 & 2 & 5\\\\\n", "\tv2 & 3 & 4 & 6\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "1. 1\n", "2. 3\n", "3. 2\n", "4. 4\n", "5. 5\n", "6. 6\n", "\n", "\n" ], "text/plain": [ " [,1] [,2] [,3]\n", "v1 1 2 5\n", "v2 3 4 6" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "'matrix'" ], "text/latex": [ "'matrix'" ], "text/markdown": [ "'matrix'" ], "text/plain": [ "[1] \"matrix\"" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1<-c(1,2,5)\n", "v2<-c(3,4,6)\n", "m1<-rbind(v1,v2)\n", "m1\n", "class(m1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that R has automatically assigned row names for us. Thank you, R! We can also use the column-based version (rbind means 'row bind') to append a column to a matrix:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

cbind" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\n", "
13
24
\n" ], "text/latex": [ "\\begin{tabular}{ll}\n", "\t 1 & 3\\\\\n", "\t 2 & 4\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 4\n", "\n", "\n" ], "text/plain": [ " [,1] [,2]\n", "[1,] 1 3\n", "[2,] 2 4" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m1<-matrix(c(1,2,3,4),nrow=2,ncol=2)\n", "m1" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ERROR", "evalue": "Error in cbind(m1, c(5, 6)): object 'm1' not found\n", "output_type": "error", "traceback": [ "Error in cbind(m1, c(5, 6)): object 'm1' not found\n" ] }, { "ename": "ERROR", "evalue": "Error in eval(expr, envir, enclos): object 'm2' not found\n", "output_type": "error", "traceback": [ "Error in eval(expr, envir, enclos): object 'm2' not found\n" ] } ], "source": [ "m2<-cbind(m1,c(5,6))\n", "m2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Getting Help in the Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you know the name of the command you want to use, you can just type '? command_name' in a code cell and run it, like so:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
help {utils}R Documentation
\n", "\n", "

Documentation

\n", "\n", "

Description

\n", "\n", "

help is the primary interface to the help systems.\n", "

\n", "\n", "\n", "

Usage

\n", "\n", "
\n",
       "help(topic, package = NULL, lib.loc = NULL,\n",
       "     verbose = getOption(\"verbose\"),\n",
       "     try.all.packages = getOption(\"help.try.all.packages\"),\n",
       "     help_type = getOption(\"help_type\"))\n",
       "
\n", "\n", "\n", "

Arguments

\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
topic\n", "

usually, a name or character string specifying the\n", "topic for which help is sought. A character string (enclosed in\n", "explicit single or double quotes) is always taken as naming a topic.\n", "

\n", "

If the value of topic is a length-one\n", "character vector the topic is taken to be the value of the only\n", "element. Otherwise topic must be a name or a reserved\n", "word (if syntactically valid) or character string.\n", "

\n", "

See ‘Details’ for what happens if this is omitted.\n", "

\n", "
package\n", "

a name or character vector giving the packages to look\n", "into for documentation, or NULL. By default, all packages \n", "whose namespaces are loaded are used. To avoid a name being deparsed use e.g.\n", "(pkg_ref) (see the examples).

\n", "
lib.loc\n", "

a character vector of directory names of R libraries,\n", "or NULL. The default value of NULL corresponds to all\n", "libraries currently known. If the default is used, the loaded\n", "packages are searched before the libraries. This is not used for\n", "HTML help (see ‘Details’.

\n", "
verbose\n", "

logical; if TRUE, the file name is reported.

\n", "
try.all.packages\n", "

logical; see Note.

\n", "
help_type\n", "

character string: the type of help required.\n", "Possible values are \"text\", \"html\" and \"pdf\".\n", "Case is ignored, and partial matching is allowed.

\n", "
\n", "\n", "\n", "

Details

\n", "\n", "

The following types of help are available:\n", "

\n", "\n", "\n", "\n", "

The ‘factory-fresh’ default is text help except from the OS X\n", "GUI, which uses HTML help displayed in its own browser window.\n", "

\n", "

The rendering of text help will use directional quotes in suitable\n", "locales (UTF-8 and single-byte Windows locales): sometimes the fonts\n", "used do not support these quotes so this can be turned off by setting\n", "options(useFancyQuotes = FALSE).\n", "

\n", "

topic is not optional: if it is omitted R will give\n", "

\n", "\n", "\n", "\n", "

Some topics need to be quoted (by backticks) or given as a\n", "character string. These include those which cannot syntactically\n", "appear on their own such as unary and binary operators,\n", "function and control-flow reserved words (including\n", "if, else for, in, repeat,\n", "while, break and next). The other reserved\n", "words can be used as if they were names, for example TRUE,\n", "NA and Inf.\n", "

\n", "

If multiple help files matching topic are found, in interactive\n", "use a menu is presented for the user to choose one: in batch use the\n", "first on the search path is used. (For HTML help the menu will be an\n", "HTML page, otherwise a graphical menu if possible if\n", "getOption(\"menu.graphics\") is true, the default.)\n", "

\n", "

Note that HTML help does not make use of lib.loc: it will\n", "always look first in the loaded packages and then along\n", ".libPaths().\n", "

\n", "\n", "\n", "

Offline help

\n", "\n", "

Typeset documentation is produced by running the LaTeX version of the\n", "help page through pdflatex: this will produce a PDF file.\n", "

\n", "

The appearance of the output can be customized through a file\n", "‘Rhelp.cfg’ somewhere in your LaTeX search path: this will be\n", "input as a LaTeX style file after Rd.sty. Some\n", "environment variables are consulted, notably R_PAPERSIZE\n", "(via getOption(\"papersize\")) and R_RD4PDF (see\n", "‘Making manuals’ in the ‘R Installation and\n", "Administration Manual’).\n", "

\n", "

If there is a function offline_help_helper in the workspace or\n", "further down the search path it is used to do the typesetting,\n", "otherwise the function of that name in the utils namespace (to\n", "which the first paragraph applies). It should accept at least two\n", "arguments, the name of the LaTeX file to be typeset and the type\n", "(which is nowadays ignored). It accepts a third argument,\n", "texinputs, which will give the graphics path when the help\n", "document contains figures, and will otherwise not be supplied.\n", "

\n", "\n", "\n", "

Note

\n", "\n", "

Unless lib.loc is specified explicitly, the loaded packages are\n", "searched before those in the specified libraries. This ensures that\n", "if a library is loaded from a library not in the known library trees,\n", "then the help from the loaded library is used. If lib.loc is\n", "specified explicitly, the loaded packages are not searched.\n", "

\n", "

If this search fails and argument try.all.packages is\n", "TRUE and neither packages nor lib.loc is\n", "specified, then all the packages in the known library trees are\n", "searched for help on topic and a list of (any) packages where\n", "help may be found is displayed (with hyperlinks for help_type =\n", " \"html\"). NB: searching all packages can be slow, especially\n", "the first time (caching of files by the OS can expedite subsequent\n", "searches dramatically).\n", "

\n", "\n", "\n", "

References

\n", "\n", "

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\n", "The New S Language.\n", "Wadsworth & Brooks/Cole.\n", "

\n", "\n", "\n", "

See Also

\n", "\n", "

? for shortcuts to help topics.\n", "

\n", "

help.search() or ?? for finding help pages\n", "on a vague topic;\n", "help.start() which opens the HTML version of the R\n", "help pages;\n", "library() for listing available packages and the\n", "help objects they contain;\n", "data() for listing available data sets;\n", "methods().\n", "

\n", "

Use prompt() to get a prototype for writing help\n", "pages of your own package.\n", "

\n", "\n", "\n", "

Examples

\n", "\n", "
\n",
       "help()\n",
       "help(help)              # the same\n",
       "\n",
       "help(lapply)\n",
       "\n",
       "help(\"for\")             # or ?\"for\", but quotes/backticks are needed\n",
       "\n",
       "try({# requires working TeX installation:\n",
       " help(dgamma, help_type = \"pdf\")\n",
       " ## -> nicely formatted pdf -- including math formula -- for help(dgamma):\n",
       " system2(getOption(\"pdfviewer\"), \"dgamma.pdf\", wait = FALSE)\n",
       "})\n",
       "\n",
       "help(package = \"splines\") # get help even when package is not loaded\n",
       "\n",
       "topi <- \"women\"\n",
       "help(topi)\n",
       "\n",
       "try(help(\"bs\", try.all.packages = FALSE)) # reports not found (an error)\n",
       "help(\"bs\", try.all.packages = TRUE)       # reports can be found\n",
       "                                          # in package 'splines'\n",
       "\n",
       "## For programmatic use:\n",
       "topic <- \"family\"; pkg_ref <- \"stats\"\n",
       "help((topic), (pkg_ref))\n",
       "
\n", "\n", "
[Package utils version 3.2.0 ]
" ], "text/latex": [ "\\inputencoding{utf8}\n", "\\HeaderA{help}{Documentation}{help}\n", "\\keyword{documentation}{help}\n", "%\n", "\\begin{Description}\\relax\n", "\\code{help} is the primary interface to the help systems.\n", "\\end{Description}\n", "%\n", "\\begin{Usage}\n", "\\begin{verbatim}\n", "help(topic, package = NULL, lib.loc = NULL,\n", " verbose = getOption(\"verbose\"),\n", " try.all.packages = getOption(\"help.try.all.packages\"),\n", " help_type = getOption(\"help_type\"))\n", "\\end{verbatim}\n", "\\end{Usage}\n", "%\n", "\\begin{Arguments}\n", "\\begin{ldescription}\n", "\\item[\\code{topic}] usually, a \\LinkA{name}{name} or character string specifying the\n", "topic for which help is sought. A character string (enclosed in\n", "explicit single or double quotes) is always taken as naming a topic.\n", "\n", "If the value of \\code{topic} is a length-one\n", "character vector the topic is taken to be the value of the only\n", "element. Otherwise \\code{topic} must be a name or a \\LinkA{reserved}{reserved}\n", "word (if syntactically valid) or character string.\n", "\n", "See `Details' for what happens if this is omitted.\n", "\n", "\\item[\\code{package}] a name or character vector giving the packages to look\n", "into for documentation, or \\code{NULL}. By default, all packages \n", "whose namespaces are loaded are used. To avoid a name being deparsed use e.g.\n", "\\code{(pkg\\_ref)} (see the examples).\n", "\\item[\\code{lib.loc}] a character vector of directory names of \\R{} libraries,\n", "or \\code{NULL}. The default value of \\code{NULL} corresponds to all\n", "libraries currently known. If the default is used, the loaded\n", "packages are searched before the libraries. This is not used for\n", "HTML help (see `Details'.\n", "\\item[\\code{verbose}] logical; if \\code{TRUE}, the file name is reported.\n", "\\item[\\code{try.all.packages}] logical; see \\code{Note}.\n", "\\item[\\code{help\\_type}] character string: the type of help required.\n", "Possible values are \\code{\"text\"}, \\code{\"html\"} and \\code{\"pdf\"}.\n", "Case is ignored, and partial matching is allowed.\n", "\\end{ldescription}\n", "\\end{Arguments}\n", "%\n", "\\begin{Details}\\relax\n", "The following types of help are available:\n", "\\begin{itemize}\n", "\n", "\\item Plain text help\n", "\\item HTML help pages with hyperlinks to other topics, shown in a\n", "browser by \\code{\\LinkA{browseURL}{browseURL}}.\n", "(Where possible an existing browser window is re-used: the OS X\n", "GUI uses its own browser window.)\n", "If for some reason HTML help is unavailable (see\n", "\\code{\\LinkA{startDynamicHelp}{startDynamicHelp}}), plain text help will be used\n", "instead.\n", "\\item For \\code{help} only, typeset as PDF --\n", "see the section on `Offline help'.\n", "\n", "\\end{itemize}\n", "\n", "The `factory-fresh' default is text help except from the OS X\n", "GUI, which uses HTML help displayed in its own browser window.\n", "\n", "The rendering of text help will use directional quotes in suitable\n", "locales (UTF-8 and single-byte Windows locales): sometimes the fonts\n", "used do not support these quotes so this can be turned off by setting\n", "\\code{\\LinkA{options}{options}(useFancyQuotes = FALSE)}.\n", "\n", "\\code{topic} is not optional: if it is omitted \\R{} will give\n", "\\begin{itemize}\n", "\n", "\\item If a package is specified, (text or, in interactive use only,\n", "HTML) information on the package, including hints/links to suitable\n", "help topics.\n", "\\item If \\code{lib.loc} only is specified, a (text) list of available\n", "packages.\n", "\\item Help on \\code{help} itself if none of the first three\n", "arguments is specified.\n", "\n", "\\end{itemize}\n", "\n", "\n", "Some topics need to be quoted (by \\LinkA{backtick}{backtick}s) or given as a\n", "character string. These include those which cannot syntactically\n", "appear on their own such as unary and binary operators,\n", "\\code{function} and control-flow \\LinkA{reserved}{reserved} words (including\n", "\\code{if}, \\code{else} \\code{for}, \\code{in}, \\code{repeat},\n", "\\code{while}, \\code{break} and \\code{next}). The other \\code{reserved}\n", "words can be used as if they were names, for example \\code{TRUE},\n", "\\code{NA} and \\code{Inf}.\n", "\n", "If multiple help files matching \\code{topic} are found, in interactive\n", "use a menu is presented for the user to choose one: in batch use the\n", "first on the search path is used. (For HTML help the menu will be an\n", "HTML page, otherwise a graphical menu if possible if\n", "\\code{\\LinkA{getOption}{getOption}(\"menu.graphics\")} is true, the default.)\n", "\n", "Note that HTML help does not make use of \\code{lib.loc}: it will\n", "always look first in the loaded packages and then along\n", "\\code{.libPaths()}.\n", "\\end{Details}\n", "%\n", "\\begin{Section}{Offline help}\n", "Typeset documentation is produced by running the LaTeX version of the\n", "help page through \\command{pdflatex}: this will produce a PDF file.\n", "\n", "The appearance of the output can be customized through a file\n", "\\file{Rhelp.cfg} somewhere in your LaTeX search path: this will be\n", "input as a LaTeX style file after \\code{Rd.sty}. Some\n", "\\LinkA{environment variables}{environment variables} are consulted, notably \\env{R\\_PAPERSIZE}\n", "(\\emph{via} \\code{getOption(\"papersize\")}) and \\env{R\\_RD4PDF} (see\n", "`Making manuals' in the `R Installation and\n", "Administration Manual').\n", "\n", "If there is a function \\code{offline\\_help\\_helper} in the workspace or\n", "further down the search path it is used to do the typesetting,\n", "otherwise the function of that name in the \\code{utils} namespace (to\n", "which the first paragraph applies). It should accept at least two\n", "arguments, the name of the LaTeX file to be typeset and the type\n", "(which is nowadays ignored). It accepts a third argument,\n", "\\code{texinputs}, which will give the graphics path when the help\n", "document contains figures, and will otherwise not be supplied.\n", "\\end{Section}\n", "%\n", "\\begin{Note}\\relax\n", "Unless \\code{lib.loc} is specified explicitly, the loaded packages are\n", "searched before those in the specified libraries. This ensures that\n", "if a library is loaded from a library not in the known library trees,\n", "then the help from the loaded library is used. If \\code{lib.loc} is\n", "specified explicitly, the loaded packages are \\emph{not} searched.\n", "\n", "If this search fails and argument \\code{try.all.packages} is\n", "\\code{TRUE} and neither \\code{packages} nor \\code{lib.loc} is\n", "specified, then all the packages in the known library trees are\n", "searched for help on \\code{topic} and a list of (any) packages where\n", "help may be found is displayed (with hyperlinks for \\code{help\\_type =\n", " \"html\"}). \\strong{NB:} searching all packages can be slow, especially\n", "the first time (caching of files by the OS can expedite subsequent\n", "searches dramatically).\n", "\\end{Note}\n", "%\n", "\\begin{References}\\relax\n", "Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\n", "\\emph{The New S Language}.\n", "Wadsworth \\& Brooks/Cole.\n", "\\end{References}\n", "%\n", "\\begin{SeeAlso}\\relax\n", "\\code{\\LinkA{?}{Question}} for shortcuts to help topics.\n", "\n", "\\code{\\LinkA{help.search}{help.search}()} or \\code{\\LinkA{??}{??}} for finding help pages\n", "on a vague topic;\n", "\\code{\\LinkA{help.start}{help.start}()} which opens the HTML version of the \\R{}\n", "help pages;\n", "\\code{\\LinkA{library}{library}()} for listing available packages and the\n", "help objects they contain;\n", "\\code{\\LinkA{data}{data}()} for listing available data sets;\n", "\\code{\\LinkA{methods}{methods}()}.\n", "\n", "Use \\code{\\LinkA{prompt}{prompt}()} to get a prototype for writing \\code{help}\n", "pages of your own package.\n", "\\end{SeeAlso}\n", "%\n", "\\begin{Examples}\n", "\\begin{ExampleCode}\n", "help()\n", "help(help) # the same\n", "\n", "help(lapply)\n", "\n", "help(\"for\") # or ?\"for\", but quotes/backticks are needed\n", "\n", "try({# requires working TeX installation:\n", " help(dgamma, help_type = \"pdf\")\n", " ## -> nicely formatted pdf -- including math formula -- for help(dgamma):\n", " system2(getOption(\"pdfviewer\"), \"dgamma.pdf\", wait = FALSE)\n", "})\n", "\n", "help(package = \"splines\") # get help even when package is not loaded\n", "\n", "topi <- \"women\"\n", "help(topi)\n", "\n", "try(help(\"bs\", try.all.packages = FALSE)) # reports not found (an error)\n", "help(\"bs\", try.all.packages = TRUE) # reports can be found\n", " # in package 'splines'\n", "\n", "## For programmatic use:\n", "topic <- \"family\"; pkg_ref <- \"stats\"\n", "help((topic), (pkg_ref))\n", "\\end{ExampleCode}\n", "\\end{Examples}" ], "text/plain": [ "help package:utils R Documentation\n", "\n", "_\bD_\bo_\bc_\bu_\bm_\be_\bn_\bt_\ba_\bt_\bi_\bo_\bn\n", "\n", "_\bD_\be_\bs_\bc_\br_\bi_\bp_\bt_\bi_\bo_\bn:\n", "\n", " ‘help’ is the primary interface to the help systems.\n", "\n", "_\bU_\bs_\ba_\bg_\be:\n", "\n", " help(topic, package = NULL, lib.loc = NULL,\n", " verbose = getOption(\"verbose\"),\n", " try.all.packages = getOption(\"help.try.all.packages\"),\n", " help_type = getOption(\"help_type\"))\n", " \n", "_\bA_\br_\bg_\bu_\bm_\be_\bn_\bt_\bs:\n", "\n", " topic: usually, a name or character string specifying the topic for\n", " which help is sought. A character string (enclosed in\n", " explicit single or double quotes) is always taken as naming a\n", " topic.\n", "\n", " If the value of ‘topic’ is a length-one character vector the\n", " topic is taken to be the value of the only element.\n", " Otherwise ‘topic’ must be a name or a reserved word (if\n", " syntactically valid) or character string.\n", "\n", " See ‘Details’ for what happens if this is omitted.\n", "\n", " package: a name or character vector giving the packages to look into\n", " for documentation, or ‘NULL’. By default, all packages whose\n", " namespaces are loaded are used. To avoid a name being\n", " deparsed use e.g. ‘(pkg_ref)’ (see the examples).\n", "\n", " lib.loc: a character vector of directory names of R libraries, or\n", " ‘NULL’. The default value of ‘NULL’ corresponds to all\n", " libraries currently known. If the default is used, the\n", " loaded packages are searched before the libraries. This is\n", " not used for HTML help (see ‘Details’.\n", "\n", " verbose: logical; if ‘TRUE’, the file name is reported.\n", "\n", "try.all.packages: logical; see ‘Note’.\n", "\n", "help_type: character string: the type of help required. Possible\n", " values are ‘\"text\"’, ‘\"html\"’ and ‘\"pdf\"’. Case is ignored,\n", " and partial matching is allowed.\n", "\n", "_\bD_\be_\bt_\ba_\bi_\bl_\bs:\n", "\n", " The following types of help are available:\n", "\n", " • Plain text help\n", "\n", " • HTML help pages with hyperlinks to other topics, shown in a\n", " browser by ‘browseURL’. (Where possible an existing browser\n", " window is re-used: the OS X GUI uses its own browser window.)\n", " If for some reason HTML help is unavailable (see\n", " ‘startDynamicHelp’), plain text help will be used instead.\n", "\n", " • For ‘help’ only, typeset as PDF - see the section on ‘Offline\n", " help’.\n", "\n", " The ‘factory-fresh’ default is text help except from the OS X GUI,\n", " which uses HTML help displayed in its own browser window.\n", "\n", " The rendering of text help will use directional quotes in suitable\n", " locales (UTF-8 and single-byte Windows locales): sometimes the\n", " fonts used do not support these quotes so this can be turned off\n", " by setting ‘options(useFancyQuotes = FALSE)’.\n", "\n", " ‘topic’ is not optional: if it is omitted R will give\n", "\n", " • If a package is specified, (text or, in interactive use only,\n", " HTML) information on the package, including hints/links to\n", " suitable help topics.\n", "\n", " • If ‘lib.loc’ only is specified, a (text) list of available\n", " packages.\n", "\n", " • Help on ‘help’ itself if none of the first three arguments is\n", " specified.\n", "\n", " Some topics need to be quoted (by backticks) or given as a\n", " character string. These include those which cannot syntactically\n", " appear on their own such as unary and binary operators, ‘function’\n", " and control-flow reserved words (including ‘if’, ‘else’ ‘for’,\n", " ‘in’, ‘repeat’, ‘while’, ‘break’ and ‘next’). The other\n", " ‘reserved’ words can be used as if they were names, for example\n", " ‘TRUE’, ‘NA’ and ‘Inf’.\n", "\n", " If multiple help files matching ‘topic’ are found, in interactive\n", " use a menu is presented for the user to choose one: in batch use\n", " the first on the search path is used. (For HTML help the menu\n", " will be an HTML page, otherwise a graphical menu if possible if\n", " ‘getOption(\"menu.graphics\")’ is true, the default.)\n", "\n", " Note that HTML help does not make use of ‘lib.loc’: it will always\n", " look first in the loaded packages and then along ‘.libPaths()’.\n", "\n", "_\bO_\bf_\bf_\bl_\bi_\bn_\be _\bh_\be_\bl_\bp:\n", "\n", " Typeset documentation is produced by running the LaTeX version of\n", " the help page through ‘pdflatex’: this will produce a PDF file.\n", "\n", " The appearance of the output can be customized through a file\n", " ‘Rhelp.cfg’ somewhere in your LaTeX search path: this will be\n", " input as a LaTeX style file after ‘Rd.sty’. Some environment\n", " variables are consulted, notably ‘R_PAPERSIZE’ (_via_\n", " ‘getOption(\"papersize\")’) and ‘R_RD4PDF’ (see ‘Making manuals’ in\n", " the ‘R Installation and Administration Manual’).\n", "\n", " If there is a function ‘offline_help_helper’ in the workspace or\n", " further down the search path it is used to do the typesetting,\n", " otherwise the function of that name in the ‘utils’ namespace (to\n", " which the first paragraph applies). It should accept at least two\n", " arguments, the name of the LaTeX file to be typeset and the type\n", " (which is nowadays ignored). It accepts a third argument,\n", " ‘texinputs’, which will give the graphics path when the help\n", " document contains figures, and will otherwise not be supplied.\n", "\n", "_\bN_\bo_\bt_\be:\n", "\n", " Unless ‘lib.loc’ is specified explicitly, the loaded packages are\n", " searched before those in the specified libraries. This ensures\n", " that if a library is loaded from a library not in the known\n", " library trees, then the help from the loaded library is used. If\n", " ‘lib.loc’ is specified explicitly, the loaded packages are _not_\n", " searched.\n", "\n", " If this search fails and argument ‘try.all.packages’ is ‘TRUE’ and\n", " neither ‘packages’ nor ‘lib.loc’ is specified, then all the\n", " packages in the known library trees are searched for help on\n", " ‘topic’ and a list of (any) packages where help may be found is\n", " displayed (with hyperlinks for ‘help_type = \"html\"’). *NB:*\n", " searching all packages can be slow, especially the first time\n", " (caching of files by the OS can expedite subsequent searches\n", " dramatically).\n", "\n", "_\bR_\be_\bf_\be_\br_\be_\bn_\bc_\be_\bs:\n", "\n", " Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S\n", " Language_. Wadsworth & Brooks/Cole.\n", "\n", "_\bS_\be_\be _\bA_\bl_\bs_\bo:\n", "\n", " ‘?’ for shortcuts to help topics.\n", "\n", " ‘help.search()’ or ‘??’ for finding help pages on a vague topic;\n", " ‘help.start()’ which opens the HTML version of the R help pages;\n", " ‘library()’ for listing available packages and the help objects\n", " they contain; ‘data()’ for listing available data sets;\n", " ‘methods()’.\n", "\n", " Use ‘prompt()’ to get a prototype for writing ‘help’ pages of your\n", " own package.\n", "\n", "_\bE_\bx_\ba_\bm_\bp_\bl_\be_\bs:\n", "\n", " help()\n", " help(help) # the same\n", " \n", " help(lapply)\n", " \n", " help(\"for\") # or ?\"for\", but quotes/backticks are needed\n", " \n", " try({# requires working TeX installation:\n", " help(dgamma, help_type = \"pdf\")\n", " ## -> nicely formatted pdf -- including math formula -- for help(dgamma):\n", " system2(getOption(\"pdfviewer\"), \"dgamma.pdf\", wait = FALSE)\n", " })\n", " \n", " help(package = \"splines\") # get help even when package is not loaded\n", " \n", " topi <- \"women\"\n", " help(topi)\n", " \n", " try(help(\"bs\", try.all.packages = FALSE)) # reports not found (an error)\n", " help(\"bs\", try.all.packages = TRUE) # reports can be found\n", " # in package 'splines'\n", " \n", " ## For programmatic use:\n", " topic <- \"family\"; pkg_ref <- \"stats\"\n", " help((topic), (pkg_ref))\n", " " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?help" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Don't forget Google" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to do something, but don't know the command in R, Google can be a great tool! Go ahead and google how to create a histogram in R." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Play!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sure, we know we need to work to learn new things - but let's not underestimate the power of play! Take a few moments to play in this new sandbox. Create some vectors, matrices, strings, etc. What can you do? Can you figure out how to make R multiply a matrix times (an appropriately sized) vector? Multiply two matrices? What happens if you add two vectors? Multiply? Do you get the answer you expect?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Start here - you can work within the lecture notes! How cool is that?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, the notebook is a great environment, especially for doing reproducible research, documenting all your steps and keeping track of things during exploratory analysis. There is another R interface available, and it is much more 'mature' than the Jupyter project (mature does not mean 'better' - just that some features available in R and Rstudio may not yet be incorporated in the Jupyter R kernel).\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rstudio" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Your VM has been setup as an Rstudio 'server'. This means that you can connect to it in a similar manner as you did to the notebook server. Use the following URL:\n", "\n", "http://colab-sbx-XXX.oit.duke.edu:8787\n", "\n", "Rstudio Server uses the system login authentication, so type in your VM username (bitnami) and the password you were assigned by Duke's OIT.\n", "\n", "Your window should look like so:\n", "\n", "​" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the upper right corner, we have a 'script' window. This is where you type code that you would like to save into a file. In the lower right is a console window. This is where you type code to execute. It is just like the command line in unix. You type code, press enter, and it gets executed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "On the right hand side is a window with tabs for Files, Plots, Packages, Help and Viewer. As we are beginners, the 'help' tab will be the most relevant. Here, we can find information on syntax, what functions do what, tutorials, etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Work!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Plot a histogram of 100 numbers generated from the standard normal distribution. Hint: Use the 'Search Engine' feature under 'Help' to find out how to generate random numbers from the standard normal, then search for 'histogram'. Don't use the ggplot histogram. We'll cover graphics grammar later on!\n", "\n", "* Use the script window to compute the mean, variance and median of the following list:\n", "(1,2,5,5,2,5,6,8,1,10)\n", "and save the script under the name 'Example1.R'\n", "\n", "* Do the same below, using Jupyter! (Hint: Go to the 'home' screen for Jupyter and click on the 'new' button on the upper right of the screen. Choose 'text' file and enter your commands. Save the file under the appropriate name. Use the 'source' command to run the code in and R cell.)" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.1.2" } }, "nbformat": 4, "nbformat_minor": 0 }