forked from PsyTeachR/shiny-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefs.Rmd
More file actions
166 lines (100 loc) · 8.09 KB
/
defs.Rmd
File metadata and controls
166 lines (100 loc) · 8.09 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---
title: "Definitions"
output:
html_document:
self_contained: no
toc: yes
toc_depth: 3
toc_float:
collapsed: false
---
### argument
A variable that provides input to a [function](defs.html#function).
### chunk
In an RMarkdown file (`.Rmd`), you can include a block (or "chunk") of R code by surrounding the code as in the example below:
<pre><code>```{r chunk-name}
mean_age <- mean(ages) %>% round(2)
```</code></pre>
### comment
You can annotate `.R` files or [chunks](defs.html#chunk) in `.Rmd` files with comments by prefacing each line of the coment with one or more hash symbols (`#`).
```{r comment-demo}
# I'm demonstrating comments in this chunk
# This comment will be added to the document outline ----
```
Comments get added to the document outline if you put four or more dashes, equal signs, or hashes at the end. This is a great way to keep track of more complicated scripts.
### concatenate
When referring to strings, concatenate means to paste them together using the function `paste` (adds a space between strings) or `paste0` (doesn't add anything between strings).
```{r concatenate-demo1}
subject_name <- "Lisa"
paste("Hello,", subject_name)
```
When referring to other types of variables, concatenate can mean to create a vector with those variables, usually using the `c` function. For example, you could concatenate the numbers 1, 3, 6, and 10 like this: `c(1, 3, 6, 10)`. You can concatenate two [vector](defs.html#vector)s as well:
```{r concatenate-demo2}
v1 <- 1:5
v2 <- 11:15
c(v1, v2)
```
<p class="alert alert-warning">Remember, a [vector](defs.html#vector) can only have one [data type](defs.html#data_type). So if you concatenate a [string](defs.html#string) vector and a [numeric](defs.html#numeric) vector, the numbers will get turned into their string versions. If you concatenate an [integer](defs.html#integer) and a [double](defs.html#integer) vector, the integers will be converted to doubles.</p>
```{r concatenate-demo3}
strings <- c("a", "c", "e")
integers <- c(1L, 3L, 5L)
doubles <- c(1.1, 3.3, 5.5)
c(strings, integers)
c(doubles, integers)
```
### data type
* [integer](defs.html#integer) (whole numbers like 1L, -10L, 3000L)
* double (numbers like -0.223, 10.324, 1e4)
* [string](defs.html#string)
* logical (`TRUE` or `FALSE`)
If you want to know what data type a variable is, use the function `typeof`.
```{r data-type-demo}
typeof(10)
typeof("10")
typeof(10L)
typeof(10 == 10)
```
### double
Doubles are a [data type](defs.html#data_type) representing any type of number. Examples of doubles are `1`, `1.0`, `-0.01`, or `1e4`.
### function
A named section of code that can be reused. For example, `sd` is a function that returns the standard deviation of the [vector](defs.html#vector) of numbers that you provide as the input [argument](defs.html#argument). Functions are set up like this: `function_name(argument1 = a1, argument2 = a2)`. The [argument](defs.html#argument)s in parentheses can be named (like, `x = c(1,3,5,8)`) or you can skip the names if you put them in the exact same order that they're defined in the function. You can check this by typing `?sd` (or whatever function name you're looking up) into the console and the Help pane will show you the default order under **Usage**.

### integer
Integers are a [data type](defs.html#data_type) representing whole numbers. In R, you specify that a number is an integer by adding an L at the end, like `1L`, `-36L`, or `100L`.
### numeric
The integer and double [data types](defs.html#data_type) are numeric.
### package
Many useful functions are built into R and available by default whenever you start it up. But some of the most powerful things you can do with R require packages of functions that are written by the community. The functions in these packages aren't available until you install the package (using `install.packages("package_name")` or clicking Install on the Packages pane; this only needs to be done if the package isn't already installed). Once that package is installed (kind of like downloading an app to your phone), you can use it in any script by loading that package as a library at the top of your script (e.g., (`library(ggplot2)`).
<p class="alert alert-info">You can alternatively type the package name and two colons before any function from that package to use it without loading all of its functions into the library (e.g., `ggplot2::geom_histogram()`). This sort of notation is also used to disambiguate function names if two packages have functions with the same names.</p>
### panes
RStudio is arranged with four window "panes". By default, the upper left pane is the *source pane*, where you view and edit source code from files. The bottom left pane is usually the *console pane*, where you can type in commands and view output messages You can change the location of panes and what tabs are shown under `Preferences > Pane Layout`.

### reactivity
Reactivity refers to changes in your app that occur in response to user input. Reactive features of your [UI](#ui) are rendered in the [server](#server).
### server
The server is the part of the app that works with logic. The server is a function that processes the user's input in the UI, and allows the app to render appropriate output. See [this article](https://shiny.rstudio.com/articles/basics.html) for more.
### shinydashboard
[Shiny Dashboard or shinydashboard](https://rstudio.github.io/shinydashboard/) is a package for R that builds on Shiny for more flexible and visually appealing UIs. As with other packages, it can be installed with `install.packages("shinydashboard")`, and can be loaded with `library(shinydashboard)`.
The [shinydashboard Documentation](https://rstudio.github.io/shinydashboard/get_started.html) is a great introduction to this package.
### string
A piece of text inside of quotes. For example, `"I sense the rains down in Africa"` is a string. Numbers inside of quotes can be a string; `"19"` is a string, while `19` is not.
### vector
This is a type of data structure that is basically a list of things like T/F values, numbers, or strings. It can get very complicated (see [Ch 20 of R for Data Science](http://r4ds.had.co.nz/vectors.html) for a thorough explanation), but at first you just need to be able to understand that the following things are examples of vectors:
```{r vector-demo1}
# use the c() function to make a vector of strings or numbers
liit_ingredients <- c("vodka", "gin", "rum", "tequila", "triple sec",
"orange juice", "coke", "sour mix")
fun_to_play_at <- c(25, 13, 3, 1)
# the colon between two integers gives you all the numbers from the first to the last integer
likert <- 1:7
```
The variable `letters` is a built-in vector with the latin letters in order. You can select part of a vector by putting the numeric location of what element you want inside of square brackets after the vector. You can even put a vector of numbers inside the square brackets to select several elements.
```{r vector-demo2}
letters[26]
letters[1:5]
letters[fun_to_play_at]
```
### widget
A interactive web element, like a dropdown menu or a slider. See a great overview of widgets at the [RStudio Shiny tutorial](https://shiny.rstudio.com/tutorial/written-tutorial/lesson3/). In shiny apps, a widget is created by its function. The first argument is the name you will use in the code for referring to that widget and its value, so make sure it's a unique, descriptive name like `plot_color` or `group1_label`. The second argument is the label, which is a string like `"Plot colour"` or `"Label for the first group"` (it can also be an empty string like `""`).
### UI
The User Interface. This refers to the App as the user will see it (in HTML). The UI is defined as an object using (e.g.) `fluidPage()` or `dashboardPage()`. The UI is in contrast to the server object, which processes information the user inputs (e.g. via widgets) to the UI, and can reactively render the app's UI. This means that the UI can look completely different depending on the actions of the user. See [this article](https://shiny.rstudio.com/articles/basics.html) for more.