Fix code language
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
Coding basics</h1>
|
||||
<p>Let’s review some basics we’ve so far omitted in the interests of getting you plotting as quickly as possible. You can use R as a calculator:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">1 / 200 * 30
|
||||
<pre data-type="programlisting" data-code-language="r">1 / 200 * 30
|
||||
#> [1] 0.15
|
||||
(59 + 73 + 2) / 3
|
||||
#> [1] 44.66667
|
||||
@@ -14,22 +14,22 @@ sin(pi / 2)
|
||||
</div>
|
||||
<p>You can create new objects with the assignment operator <code><-</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- 3 * 4</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">x <- 3 * 4</pre>
|
||||
</div>
|
||||
<p>You can <strong>c</strong>ombine multiple elements into a vector with <code><a href="https://rdrr.io/r/base/c.html">c()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">primes <- c(2, 3, 5, 7, 11, 13)</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">primes <- c(2, 3, 5, 7, 11, 13)</pre>
|
||||
</div>
|
||||
<p>And basic arithmetic is applied to every element of the vector:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">primes * 2
|
||||
<pre data-type="programlisting" data-code-language="r">primes * 2
|
||||
#> [1] 4 6 10 14 22 26
|
||||
primes - 1
|
||||
#> [1] 1 2 4 6 10 12</pre>
|
||||
</div>
|
||||
<p>All R statements where you create objects, <strong>assignment</strong> statements, have the same form:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">object_name <- value</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">object_name <- value</pre>
|
||||
</div>
|
||||
<p>When reading that code, say “object name gets value” in your head.</p>
|
||||
<p>You will make lots of assignments and <code><-</code> is a pain to type. You can save time with RStudio’s keyboard shortcut: Alt + - (the minus sign). Notice that RStudio automatically surrounds <code><-</code> with spaces, which is a good code formatting practice. Code is miserable to read on a good day, so giveyoureyesabreak and use spaces.</p>
|
||||
@@ -41,7 +41,7 @@ Comments</h1>
|
||||
<p>R will ignore any text after <code>#</code>. This allows to you to write <strong>comments</strong>, text that is ignored by R but read by other humans. We’ll sometimes include comments in examples explaining what’s happening with the code.</p>
|
||||
<p>Comments can be helpful for briefly describing what the subsequent code does.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit"># define primes
|
||||
<pre data-type="programlisting" data-code-language="r"># define primes
|
||||
primes <- c(2, 3, 5, 7, 11, 13)
|
||||
|
||||
# multiply primes by 2
|
||||
@@ -59,7 +59,7 @@ primes * 2
|
||||
What’s in a name?</h1>
|
||||
<p>Object names must start with a letter, and can only contain letters, numbers, <code>_</code> and <code>.</code>. You want your object names to be descriptive, so you’ll need to adopt a convention for multiple words. We recommend <strong>snake_case</strong> where you separate lowercase words with <code>_</code>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">i_use_snake_case
|
||||
<pre data-type="programlisting" data-code-language="r">i_use_snake_case
|
||||
otherPeopleUseCamelCase
|
||||
some.people.use.periods
|
||||
And_aFew.People_RENOUNCEconvention</pre>
|
||||
@@ -67,22 +67,22 @@ And_aFew.People_RENOUNCEconvention</pre>
|
||||
<p>We’ll come back to names again when we talk more about code style in <a href="#chp-workflow-style" data-type="xref">#chp-workflow-style</a>.</p>
|
||||
<p>You can inspect an object by typing its name:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x
|
||||
<pre data-type="programlisting" data-code-language="r">x
|
||||
#> [1] 12</pre>
|
||||
</div>
|
||||
<p>Make another assignment:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">this_is_a_really_long_name <- 2.5</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">this_is_a_really_long_name <- 2.5</pre>
|
||||
</div>
|
||||
<p>To inspect this object, try out RStudio’s completion facility: type “this”, press TAB, add characters until you have a unique prefix, then press return.</p>
|
||||
<p>Ooops, you made a mistake! The value of <code>this_is_a_really_long_name</code> should be 3.5, not 2.5. Use another keyboard shortcut to help you fix it. Type “this” then press Cmd/Ctrl + ↑. Doing so will list all the commands you’ve typed that start with those letters. Use the arrow keys to navigate, then press enter to retype the command. Change 2.5 to 3.5 and rerun.</p>
|
||||
<p>Make yet another assignment:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">r_rocks <- 2 ^ 3</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">r_rocks <- 2 ^ 3</pre>
|
||||
</div>
|
||||
<p>Let’s try to inspect it:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">r_rock
|
||||
<pre data-type="programlisting" data-code-language="r">r_rock
|
||||
#> Error: object 'r_rock' not found
|
||||
R_rocks
|
||||
#> Error: object 'R_rocks' not found</pre>
|
||||
@@ -95,17 +95,17 @@ R_rocks
|
||||
Calling functions</h1>
|
||||
<p>R has a large collection of built-in functions that are called like this:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">function_name(arg1 = val1, arg2 = val2, ...)</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">function_name(arg1 = val1, arg2 = val2, ...)</pre>
|
||||
</div>
|
||||
<p>Let’s try using <code><a href="https://rdrr.io/r/base/seq.html">seq()</a></code>, which makes regular <strong>seq</strong>uences of numbers and, while we’re at it, learn more helpful features of RStudio. Type <code>se</code> and hit TAB. A popup shows you possible completions. Specify <code><a href="https://rdrr.io/r/base/seq.html">seq()</a></code> by typing more (a <code>q</code>) to disambiguate, or by using ↑/↓ arrows to select. Notice the floating tooltip that pops up, reminding you of the function’s arguments and purpose. If you want more help, press F1 to get all the details in the help tab in the lower right pane.</p>
|
||||
<p>When you’ve selected the function you want, press TAB again. RStudio will add matching opening (<code>(</code>) and closing (<code>)</code>) parentheses for you. Type the arguments <code>1, 10</code> and hit return.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">seq(1, 10)
|
||||
<pre data-type="programlisting" data-code-language="r">seq(1, 10)
|
||||
#> [1] 1 2 3 4 5 6 7 8 9 10</pre>
|
||||
</div>
|
||||
<p>Type this code and notice that RStudio provides similar assistance with the paired quotation marks:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- "hello world"</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">x <- "hello world"</pre>
|
||||
</div>
|
||||
<p>Quotation marks and parentheses must always come in a pair. RStudio does its best to help you, but it’s still possible to mess up and end up with a mismatch. If this happens, R will show you the continuation character “+”:</p>
|
||||
<pre><code>> x <- "hello
|
||||
@@ -125,7 +125,7 @@ Exercises</h1>
|
||||
<ol type="1"><li>
|
||||
<p>Why does this code not work?</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">my_variable <- 10
|
||||
<pre data-type="programlisting" data-code-language="r">my_variable <- 10
|
||||
my_varıable
|
||||
#> Error in eval(expr, envir, enclos): object 'my_varıable' not found</pre>
|
||||
</div>
|
||||
@@ -134,7 +134,7 @@ my_varıable
|
||||
<li>
|
||||
<p>Tweak each of the following R commands so that they run correctly:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">libary(tidyverse)
|
||||
<pre data-type="programlisting" data-code-language="r">libary(tidyverse)
|
||||
|
||||
ggplot(dota = mpg) +
|
||||
geom_point(maping = aes(x = displ, y = hwy))</pre>
|
||||
|
||||
Reference in New Issue
Block a user