Lesson 4 Seeking Help

teaching: 10
exercises: 10
adapted from: http://swcarpentry.github.io/r-novice-gapminder/03-seeking-help/index.html
questions:

  • “How can I get help in R?”

objectives:

  • “To be able read R help files for functions and special operators.”
  • “To be able to use CRAN task views to identify packages to solve a problem.”
  • “To be able to seek help from your peers.”

keypoints:

  • “Use help() to get online help in R.”

4.1 Reading Help files

R, and every package, provide help files for functions. The general syntax to search for help on any function, “function_name”, from a specific function that is in a package loaded into your namespace (your interactive R session):

?function_name
help(function_name)

This will load up a help page in RStudio (or as plain text in R by itself).

Each help page is broken down into sections:

  • Description: An extended description of what the function does.
  • Usage: The arguments of the function and their default values.
  • Arguments: An explanation of the data each argument is expecting.
  • Details: Any important details to be aware of.
  • Value: The data the function returns.
  • See Also: Any related functions you might find useful.
  • Examples: Some examples for how to use the function.

Different functions might have different sections, but these are the main ones you should be aware of.

4.1 Tip: Running Examples

From within the function help page, you can highlight code in the Examples and hit Ctrl+Return to run it inRStudio console. This is gives you a quick way to get a feel for how a function works.

4.1 Tip: Reading help files

One of the most daunting aspects of R is the large number of functions available. It would be prohibitive, if not impossible to remember the correct usage for every function you use. Luckily, the help files mean you don’t have to!

4.2 Special Operators

To seek help on special operators, use quotes:

?"<-"

4.3 Getting help on packages

Many packages come with “vignettes”: tutorials and extended example documentation. Without any arguments, vignette() will list all vignettes for all installed packages; vignette(package="package-name") will list all available vignettes for package-name, and vignette("vignette-name") will open the specified vignette.

If a package doesn’t have any vignettes, you can usually find help by typing help("package-name").

4.4 When you kind of remember the function

If you’re not sure what package a function is in, or how it’s specifically spelled you can do a fuzzy search:

??function_name

4.5 When you have no idea where to begin

If you don’t know what function or package you need to use CRAN Task Views is a specially maintained list of packages grouped into fields. This can be a good starting point.

4.6 When your code doesn’t work: seeking help from your peers

If you’re having trouble using a function, 9 times out of 10, the answers you are seeking have already been answered on Stack Overflow. You can search using the [r] tag.

If you can’t find the answer, there are a few useful functions to help you ask a question from your peers:

?dput

Will dump the data you’re working with into a format so that it can be copy and pasted by anyone else into their R session.

sessionInfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18362)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.1252 
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## loaded via a namespace (and not attached):
##  [1] compiler_3.6.1  magrittr_1.5    bookdown_0.16   tools_3.6.1    
##  [5] htmltools_0.4.0 rstudioapi_0.10 yaml_2.2.0      Rcpp_1.0.3     
##  [9] stringi_1.4.3   rmarkdown_2.0   knitr_1.26      stringr_1.4.0  
## [13] xfun_0.11       digest_0.6.23   rlang_0.4.2     evaluate_0.14

Will print out your current version of R, as well as any packages you have loaded. This can be useful for others to help reproduce and debug your issue.

4.6 Challenge 1

Use help to find a function (and its associated parameters) that you could use to load data from a tabular file in which columns are delimited with “” (tab) and the decimal point is a “.” (period). This check for decimal separator is important, especially if you are working with international colleagues, because different countries have different conventions for the decimal point (i.e. comma vs period).
hint: use ??"read table" to look up functions related to reading in tabular data. > ## Solution to Challenge 3 > > The standard R function for reading tab-delimited files with a period decimal separator is read.delim(). You can also do this with read.table(file, sep="\t") (the period is the default decimal separator for read.table(), although you may have to change the comment.char argument as well if your data file contains hash (#) characters

4.7 Other ports of call