# Note the context="server". That's so that the question_ui_initialize methods from etude will be registered. library(mosaic) library(mosaicCalc) library(ggformula) library(math141Z) library(etude) library(learnr) library(gradethis) library(submitr) library(basket) library(math141Zexercises) etude::show_answers(FALSE) learnr::tutorial_options(exercise.timelimit = 60, exercise.checker = gradethis::grade_learnr)
submitr::login_controls()
options(tutorial.storage = "none") vfun <- basket::check_valid #submitr::make_basic_validator(NULL, "hello") storage_actions <- submitr::record_gs4("1jiV11I9tlEfAhRrkYqn6kPtkNma04TEpSCk-XMSuIRM", "statprep.annie@gmail.com", vfun) # submitr::record_local("./minimal_submissions.csv") submitr::shiny_logic(input, output, session, vfun, storage_actions)
The word "syntax" means "the arrangement of words and phrases to create well-formed sentences in a language." The language you will be using is R. By learning the syntax of R you will be able to make sense of new R commands without memorizing lots of details.
The aspects of R syntax that you will learn in this tutorial are:
Almost all computer languages provide a way for writing numerals and doing arithmetic that is a close match to the tradition notation you learned in mathematics classes. So many of the following examples will be obvious to you. Still, there are little differences from time to time that are important to get right.
Traditional | R | comments
--------------------:|:--------------------|:---------
$160$ | 160
|
$1.6 \times 10^2$ | 1.6e2
| The e2
means $\times 10^2$. Note that there are no spaces in the R expression.
$1.6 \times 10^{-3}$ | 1.6e-3
| The e-3
means $\times 10^{-3}$. Again, no spaces.
$3,425,187$ | 3425187
|| Don't use commas to divide into 3-digit groups.
$15 + 3$ | 15 + 3
|
$\frac{15}{3}$ | 15 / 3
|
$15 \times 3$ | 15 * 3
| You must always use the *
for multiplication.
$15 \cdot 3$ | 15 * 3
|
$15 - 3$ | 15 - 3
|
$15^3$ | 15 ^ 3
| They ^
symbol is called a "caret." There are no superscripts in R. You use ^
instead.
$15^{3 + 2}$ | 15 ^ (3 + 2)
| You need to use parenthesis to enforce the grouping of 3 + 2
.
$\frac{4 + 2}{1 + 5}$| (4 + 2) / (1 + 5)
| Use parentheses to enforce the grouping.
$|3 - 5|$ | abs(3 - 5)
| Use the abs()
function.
$\sqrt{15}$ | sqrt(15)
| Use the sqrt()
function. Note the parentheses.
$\sqrt[3]{15}$ | 15 ^ (1/3)
| Recall that even in traditional notation, $\sqrt[3]{15}$ is the same as $15^{\frac{1}{3}}$
$\sin \pi$ | sin(pi)
| Note that the number $\pi$ is given the name pi
in R. Also note the parentheses.
$\ln 20$ | log(20)
| The so-called "natural" logarithm.
$\log_{10}20$ | log(20, base = 10)
$e^{3}$ | exp(3)
| Don't confuse the mathematical number $e$ (which is called "Euler's number") with the use of e
in scientific notation in R, for instance 1.6e-3
.
These are drawn from Question 7 on p. 13 of MMAC textbook.
r etude::include_etude("Exercises/Computing/bee-write-closet.Rmd", "**MMAC 1.1.7a**: ", package = "math141Zexercises")
r etude::include_etude("Exercises/Computing/bee-write-closet2.Rmd", "**MMAC 1.1.7b**: ", package = "math141Zexercises")
r etude::include_etude("Exercises/Computing/bee-write-closet3.Rmd", "**MMAC 1.1.7c**: ", package = "math141Zexercises")
r etude::include_etude("Exercises/Computing/bee-write-closet4.Rmd", "**MMAC 1.1.7d**: ", package = "math141Zexercises")
r etude::include_etude("Exercises/Computing/bee-write-closet5.Rmd", "**MMAC 1.1.7e**: ", package = "math141Zexercises")
r etude::include_etude("Exercises/Computing/bee-write-closet6.Rmd", "**MMAC 1.1.7f**: ", package = "math141Zexercises")
r etude::include_etude("Exercises/Computing/panda-dig-book.Rmd", "**Bonus**: ", package = "math141Zexercises")
As you know, the syntax of natural languages like English involves words, phrases, and sentences. To grammarians, a sentence is a complete thought.
Here are examples of complete thoughts in R. (The examples relate to arithmetic only because that is the only element of R syntax we have examined so far in these notes.)
3 + 8 (3 + 8) / 4 sqrt(27)
Nobody uses the word "sentence" to refer to a complete thought in a computer language. Instead, the word "command" is often used. This is appropriate because the purpose of framing a complete thought in a computer language is to tell the computer to do something.
In both English and computer languages there are incomplete thoughts, sequences of words or symbols that aren't yet a sentence but are heading in that direction. For instance:
3 +
sqrt(27
A person's initial reaction to an incomplete thought in English is to wait for the speaker to complete the thought. Often, there is some dangling word that signals the thought is still incomplete: "from" and "with" in the above examples. Note that "She is coming." is a complete thought even though "She is coming from" is not.
The same is true in R. 3
is a complete thought, but 3 +
is not. If you hand an incomplete thought to R as a command, R will tell you that something is wrong. For instance:
3 +
Complete thoughts are often assembled out of components. In natural languages, we call these components "words" or "phrases." In R we shall call them expressions. An R expression is a complete thought. Admittedly, an individual expression might not describe the complete computation that we intend to carry out, but, like "Alice walked" it describes some computation.
To help you answer the following questions, here is a computing sandbox where you can try out expressions to check whether or not they are complete.
etude::true_or_false( prompt = "**Question 1**: Is `((1 + 2) / (3 + 4)` a complete expression?", FALSE, message_wrong = "The opening parenthesis is unneeded. There's no harm using extra parentheses, but every opening paren needs to be closed.")
etude::true_or_false( prompt = "**Question 2**: Is `3e6` a complete expression?", TRUE, message_wrong = "`3e6` means 3 followed by 6 zeros, that is, 3000000.")
etude::true_or_false( prompt = "**Question 3**: Is `sqrt(24))` a complete expression?", FALSE, message_wrong = "The last closing parenthesis does not have a corresponding opening parenthesis.")
etude::true_or_false( prompt = "**Question 1**: Is `5 (3 + 4)` a complete expression?", FALSE, message_wrong = "Just as an English sentence needs a verb, multiplication by 5 must be signalled with *.")
R (and most other computer languages) can store the results of a computation under a name, for later reference. For instance,
speed <- 46
Here, speed
is the name we have selected to store the value 46. An R expression that stores a value under a name is called an "assignment expression."
The name to be used always goes to the left of <-
. The right side can be any R expression.
After assignment to a name has been carried out, you can use that name as an expression. The name will stand for the value that had previously been stored.
There are rules for legitimate names. For instance:
.
and _
) can be used within the name, but no other punctuation can be used.Speed
and speed
are different names.Note that quotation marks are never allowed in a name. That is why, in these notes, we use a typewriter font, as in speed
, to present a name.
The sandbox below provides a place for you to try out expressions. Think of this as scratch paper to allow you to figure out the answers to the questions.
r etude::include_etude("Exercises/Computing/crocodile-drive-pants.Rmd", "**Practice 1**: ", package = "math141Zexercises")
r etude::include_etude("Exercises/Computing/cow-talk-piano.Rmd", "**Practice 2**: ", package = "math141Zexercises")
This is a course on calculus, and calculus is all about functions. We will be talking about mathematical functions a lot. Since we're using a computer to implement the operations and computations of calculus, we will also be talking about computer functions a lot. We need to be clear about the distinction, but it will be tedious to use the words "mathematical" and "computer" over and over again.
There's a deeper problem. As you will see in the next section, computer science makes a sensible distinction between a (computer) function itself and the application of that function to arguments. This is basically the same difference as between a recipe and collecting ingredients cooking tonight's dinner. But the mathematical notation used in algebra and calculus textbooks typically does not clearly indicate whether it is referring to the recipe or making dinner. For instance, you might see $y = \sin x$. Is that saying that the number $y$ is equal to the result of applying the $\sin$ function to a numerical value $x$? Is it saying that "we will name by $y$ the function $\sin$ which takes an argument named $x$.
We will use three conventions to make it easy to distinguish among the possibilities.
typewriter
script for computer names and functions.sin()
.Remember that the parentheses are not part of the function name. The names themselves are $\sin$ and sin
. We are adding the parentheses merely as a reminder to the human reader that the thing we are naming is a function.
Without these conventions, we would be forced to write sentences like,
The computer function sin implements the mathematical function sin.
That sentence reads like it is about the moral flaws of mathematical and computer functions, which is distracting from its true meaning.
Instead, we can write, unambiguously and compactly,
sin()
implements $\sin()$.
But remember, the names of the functions are sin
and $\sin$ respectively.
In this section, we going to look how to write an R expression to evaluate a function. For example, you have already seen the sqrt()
function. The following expression evaluates the square-root function with the input 5.
sqrt(5)
Function evaluation is such an important concept in computing that there are many equivalent ways of saying it. Just for fun, here are a few:
The names of functions generally describe the purpose of the function. sqrt
is the name of a function for calculating square roots, sin
and cos
are names of common trigonometric functions, log
is the name of a function to compute logarithms.
Each of these functions happens to take a number as input and produces a number as output. You produce a value by applying the function to an argument. In the above example, sqrt(5)
, the argument is the number 5
.
The syntax of applying a function to an argument is always the same and always involves an opening and a closing parenthesis.
$$\mbox{function_name} \left( \mbox{argument} \right)$$
There are only two contexts in R that involve parentheses used correctly in an expression.
1. Arithmetical grouping, as in (3 + 2) / 4
.
2. Applying a function to an argument.
Whenever you see a name followed by an opening parenthesis, you know that the name refers to a function.
Many functions have more than one argument. When there are multiple arguments, they are always separated by commas.
Functions often have named arguments. This is useful when there are many possible arguments, some of which will have default values to reflect the typical use. As an example, consider the log
function, which takes a single numerical argument but also accepts a named argument to set the "base" of the log.
log(100) log(100, base = 10) log(100, base = 2)
Note the use of the =
symbol. That is the way to assign a value to the particular argument. It is quite reminiscent of the way that <-
is used, but you cannot use <-
for named arguments. The only legitimate place for =
to be used is inside the parentheses that contain the arguments.
r etude::include_etude("Exercises/Computing/rabbit-bite-scarf.Rmd", "**Practice 1**: ", package = "math141Zexercises")
r etude::include_etude("Exercises/Computing/deer-dive-car.Rmd", "**Practice 2**: ", package = "math141Zexercises")
As you know, the point of a function is to translate inputs into an output. Calculus is very much about the study and use of functions, and it's fortunate that many mathematical functions use a notation similar to that of computer functions.
Functions are an important way to represent mathematically relationships between variables.
Sometimes you want to express to the computer an idea like, "Fuel consumption is a function of speed and air density." This is a complete thought in English--it's a sentence!--but it isn't a complete description of the specific relationship between fuel consumption and the other variables, which might be detailed and complicated.
R has a special syntax for representing relationships between variables in general terms: the tilde expression. You will be using tilde expressions throughout the course, even from the very beginning. A tilde expression along the lines of "fuel consumption is a function of speed and air density" is
fuel_consumption ~ speed + air_density
fuel_consumption
, which is a legal name, rather than fuel consumption
.+
does not necessarily refer to mathematical addition. So rather than reading speed + air_density
as "speed added to air density" it will often mean "speed and air density" or "both speed and air density."~
. The expressions on the left and the right side of the tilde must always be consistent with R syntax. So fuel_consumption ~ speed, air_density
is not a proper expression.It's easy to confuse the tilde ~
with the minus sign -
. So take care. Just to emphasize what the tilde looks like, here it is, unmistakably.
knitr::include_graphics("images/tilde.png")
r etude::include_etude("Exercises/Computing/giraffe-dream-coat.Rmd", "**Practice 1**: ", package = "math141Zexercises")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.