-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path01-rstudio-intro.html
106 lines (106 loc) · 7.42 KB
/
01-rstudio-intro.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: R for reproducible scientific analysis</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">R for reproducible scientific analysis</h1></a>
<h2 class="subtitle">Introduction to R and RStudio</h2>
<section class="objectives panel panel-warning">
<div class="panel-heading">
<h2 id="learning-objectives"><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>To gain familiarity with the RStudio IDE</li>
</ul>
</div>
</section>
<h2 id="introduction-to-rstudio">Introduction to RStudio</h2>
<p>Welcome to the R portion of the Software Carpentry workshop.</p>
<p>The best way to learn how to program is to do something useful, so this introduction to R is built around a common scientific task: data analysis. You will learn some of the fundamentals of the R language, but our real goal is for you to learn to conduct analyses efficiently and to do so in a way that is reproducible (by you and others). We use R in our lessons because:</p>
<p>we have to use something for examples; it’s free, well-documented, and runs almost everywhere; it has a large (and growing) user base among scientists; and it has a large library of external packages available for performing diverse tasks.</p>
<p>We’ll be using RStudio: a free, open source R integrated development environment. It provides a built in editor, works on all platforms (including on servers) and provides many advantages such as integration with version control and project management.</p>
<p><strong>Basic layout</strong></p>
<p>When you first open RStudio, you will be greeted by three panels:</p>
<ul>
<li>The interactive R console (entire left)</li>
<li>Workspace/History (tabbed in upper right)</li>
<li>Files/Plots/Packages/Help (tabbed in lower right)</li>
</ul>
<p>Once you open files, such as R scripts, an editor panel will also open in the top left.</p>
<h2 id="work-flow-within-rstudio">Work flow within RStudio</h2>
<p>There are two main ways one can work within RStudio.</p>
<ol style="list-style-type: decimal">
<li>Test and play within the interactive R console then copy code into a .R file to run later.</li>
</ol>
<ul>
<li>This works well when doing small tests and initially starting off.</li>
<li>It quickly becomes laborious</li>
</ul>
<ol start="2" style="list-style-type: decimal">
<li>Start writing in an .R file and use RStudio’s command / short cut to push current line, selected lines or modified lines to the interactive R console.</li>
</ol>
<ul>
<li>This is a great way to start; all your code is saved for later</li>
<li>You will be able to run the file you create from within RStudio or using R’s <code>source()</code> function.</li>
</ul>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="tip-running-segments-of-your-code"><span class="glyphicon glyphicon-pushpin"></span>Tip: Running segments of your code</h2>
</div>
<div class="panel-body">
<p>RStudio offers you great flexibility in running code from within the editor window. There are buttons, menu choices, and keyboard shortcuts. To run the current line, you can 1. click on the <code>Run</code> button just above the editor panel, or 2. select “Run Lines” from the “Code” menu, or 3. hit Ctrl-Enter in Windows or Linux or Command-Enter on OS X. (This shortcut can also be seen by hovering the mouse over the button). To run a block of code, select it and then <code>Run</code>. If you have modified a line of code within a block of code you have just run, there is no need to reselct the section and <code>Run</code>, you can use the next button along, <code>Re-run the previous region</code>. This will run the previous code block inculding the modifications you have made.</p>
</div>
</aside>
<h2 id="introduction-to-r">Introduction to R</h2>
<p>Much of your time in R will be spent in the R interactive console. This is where you will run all of your code, and can be a useful environment to try out ideas before adding them to an R script file. This console in RStudio is the same as the one you would get if you just typed in <code>R</code> in your commandline environment.</p>
<p>The first thing you will see in the R interactive session is a bunch of information, followed by a “>” and a blinking cursor. In many ways this is similar to the shell environment you learned about during the shell lessons: it operates on the same idea of a “Read, evaluate, print loop”: you type in commands, R tries to execute them, and then returns a result.</p>
<h2 id="r-packages">R Packages</h2>
<p>It is possible to add functions to R by obtaining a package written by someone else. As of this writing, there are over 7,000 packages available on CRAN (the comprehensive R archive network). R and RStudio have functionality for managing packages:</p>
<ul>
<li>You can see what packages are installed by typing <code>installed.packages()</code></li>
<li>You can install packages by typing <code>install.packages("packagename")</code>, where <code>packagename</code> is the package name, in quotes.</li>
<li>You can update installed packages by typing <code>update.packages()</code></li>
<li>You can remove a package with <code>remove.packages("packagename")</code></li>
<li>You can make a package you have installed available for use in your session with <code>library(packagename)</code></li>
</ul>
<p>Install the following packages: <code>ggplot2</code>, <code>plyr</code>, <code>gapminder</code> if you have not done so already.</p>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/lesson-template">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</body>
</html>