Running R code

I. The Prompt

A. >

Now that you have R Studio set up, let’s head to the console (the “stove top”). You should notice the > symbol. This is called the “prompt” and it means that R is ready to run some code!

> x <- 1

You’ll notice that in many tutorials or help guides the > will not be present:

x <- 1

This makes it easier to copy directly from the tutorial and run the code in your console. Otherwise you may copy > into the prompt, resulting in an error:

> x <- 1
## Error: <text>:1:1: unexpected '>'
## 1: >
##     ^

If > is not present you will not be able to run your code. There are a few reasons > may be absent. The two most common reasons are:

  1. Previous code is still running
    For instance, if we run a line of code that will take up a lot of computational resources, you will see no symbol in the console. If you enter anything during this time, nothing happens until the previous code has been run.
## Create very large dataset. 
weight <- data.frame(group = gl(2, 100000000, 200000000, labels = c("Ctl","Trt")),
                    weight = c(rnorm(100000000, mean = 5, sd = 2.5),
                                rnorm(100000000, mean = 8, sd = 5))
)
## Remove dataset. 
#rm(weight)
  1. R is waiting for unfinished code to be finished
x <- c(1, 3, 5, 4, 9)
mean(x
## Error: <text>:3:0: unexpected end of input
## 1: x <- c(1, 3, 5, 4, 9)
## 2: mean(x
##   ^

If this is the case you should see +. Where a line of code needs to be finished, you can either complete the syntax or hit esc on your keyboard.

B. #

The symbol # tells R to not run the code that follows. Humans can read it but the computer doesn’t. It is very useful for leaving comments to anyone else who will use your code (especially future you).

x <- 1
# x <- 2
x # because `x <- 2` is "hashed out" R does not evaluate it so `x` is still 1. 
## [1] 1

C. ?

You can use the ? symbol to get help. Putting ? in front of a function or a package (we’ll discuss those in a second) will open the documentation for the function/package in the Help window.

?mean()

D. []

You should see square brackets in the output. These give you the numerical position of the output.

x <- seq(from = 1, to = 100, by = 0.5)
x
##   [1]   1.0   1.5   2.0   2.5   3.0   3.5   4.0   4.5   5.0   5.5   6.0   6.5
##  [13]   7.0   7.5   8.0   8.5   9.0   9.5  10.0  10.5  11.0  11.5  12.0  12.5
##  [25]  13.0  13.5  14.0  14.5  15.0  15.5  16.0  16.5  17.0  17.5  18.0  18.5
##  [37]  19.0  19.5  20.0  20.5  21.0  21.5  22.0  22.5  23.0  23.5  24.0  24.5
##  [49]  25.0  25.5  26.0  26.5  27.0  27.5  28.0  28.5  29.0  29.5  30.0  30.5
##  [61]  31.0  31.5  32.0  32.5  33.0  33.5  34.0  34.5  35.0  35.5  36.0  36.5
##  [73]  37.0  37.5  38.0  38.5  39.0  39.5  40.0  40.5  41.0  41.5  42.0  42.5
##  [85]  43.0  43.5  44.0  44.5  45.0  45.5  46.0  46.5  47.0  47.5  48.0  48.5
##  [97]  49.0  49.5  50.0  50.5  51.0  51.5  52.0  52.5  53.0  53.5  54.0  54.5
## [109]  55.0  55.5  56.0  56.5  57.0  57.5  58.0  58.5  59.0  59.5  60.0  60.5
## [121]  61.0  61.5  62.0  62.5  63.0  63.5  64.0  64.5  65.0  65.5  66.0  66.5
## [133]  67.0  67.5  68.0  68.5  69.0  69.5  70.0  70.5  71.0  71.5  72.0  72.5
## [145]  73.0  73.5  74.0  74.5  75.0  75.5  76.0  76.5  77.0  77.5  78.0  78.5
## [157]  79.0  79.5  80.0  80.5  81.0  81.5  82.0  82.5  83.0  83.5  84.0  84.5
## [169]  85.0  85.5  86.0  86.5  87.0  87.5  88.0  88.5  89.0  89.5  90.0  90.5
## [181]  91.0  91.5  92.0  92.5  93.0  93.5  94.0  94.5  95.0  95.5  96.0  96.5
## [193]  97.0  97.5  98.0  98.5  99.0  99.5 100.0

We will come back to the square brackets when we discuss dataframes because they play a more important role there.

================================================================================

Session information:

Last update on 2021-10-02

sessionInfo()
## R version 4.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=de_AT.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=de_AT.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=de_AT.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=de_AT.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.29   R6_2.5.1        jsonlite_1.8.0  magrittr_2.0.3 
##  [5] evaluate_0.16   stringi_1.7.8   cachem_1.0.6    rlang_1.0.5    
##  [9] cli_3.3.0       rstudioapi_0.14 jquerylib_0.1.4 bslib_0.4.0    
## [13] rmarkdown_2.16  tools_4.2.1     stringr_1.4.1   xfun_0.32      
## [17] yaml_2.3.5      fastmap_1.1.0   compiler_4.2.1  htmltools_0.5.3
## [21] knitr_1.40      sass_0.4.2

================================================================================

Copyright © 2022 Dan C. Mann. All rights reserved.