4  Getting Started

4.1 Function calls

Working in R, like any programming language, involves calling functions. For this to work as expected, you need to pass the right type of object(s) to the appropriate function.


Figure 4.1: A general approach to calling functions to promote correctness.

4.2 Errors & Warnings

Given your specific goal, when you execute an R expression, there are a few possible outcomes:

4.2.1 No warning or error

  1. The code executes & produces correct output without issuing a warning (best case scenario).
  2. The code executes BUT produces incorrect output without issuing a warning: Fix code to produce correct output (worst case scenario).

4.2.2 Warning

  1. The code executes & produces correct output but issues a warning: Fix code to avoid warning.
  2. The code executes & produces incorrect output & issues a warning: Fix code to produce correct output & avoid warning.

4.2.3 Error

  1. The code stops execution & produces an error: Fix code to avoid error.
Figure 4.2: You should always aim for “Success” - Notice how close the best and worst case scenarios are.

4.3 Getting help

If you have an issue that you can’t resolve by reading the documentation or searching online, before asking for help, make sure you collect all the relevant information. This includes:

A very useful R command to collect information on your R session is sessionInfo():

R version 4.4.1 (2024-06-14)
Platform: aarch64-apple-darwin20
Running under: macOS 15.0

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Los_Angeles
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.6.4 compiler_4.4.1    fastmap_1.2.0     cli_3.6.3        
 [5] tools_4.4.1       htmltools_0.5.8.1 yaml_2.3.10       rmarkdown_2.28   
 [9] knitr_1.48        jsonlite_1.8.9    xfun_0.47         digest_0.6.37    
[13] rlang_1.1.4       evaluate_1.0.0   

It is recommended to include the output of sessionInfo() when asking for help - it is often expected.

Stack Overflow provides guidelines on how to create an MRE.

Stack Overflow is a massively popular Q&A site for programmers, part of the wider Stack Exchange network. Many R-related web searches will bring up posts in Stack Overflow. You can view all questions tagged with “r”.