7  Help!

7.1 Built-in help

You can get help for any function by putting a ? before the name of the function.

So to get help for the function log(), run

?log

or type fn-F1 (F1 works on some computers) to get help for the command under the cursor.

This will open the help file in the Help tab of RStudio (by default in the lower left quadrant. You can also search the help from the Help tab.

Most help files have a similar structure (although some sections are optional).

  • Description briefly describes the function
  • Usage shows the function and its arguments including any defaults
  • Arguments describes how the arguments to the functions should be used
  • Details Give more details about how to use the function
  • Value Describes the output of the function
  • Examples Gives examples of how to use the function. This is often one of the most useful parts of the help. You can run the examples by clicking on the “Run examples” link in the help file.
Exercise

Look at the help file for length() and run the examples.

7.1.1 Vignettes and demos

Vignettes are long-form documentation that can explain how to use a package. The best way to access them for the dplyr package is to run

help(package = "dplyr")

and then click on “User guides, package vignettes and other documentation.

Some packages have demos that are like long example sections showing how to use some functions. You can access these with the demo() function.

7.2 On-line sources of help

The help files are sometimes not the most user friendly.

One of the great things about R is how many sources of help there are.

There are hundreds of thousands of answers between these sites, so it is likely that other people have had the same problem and asked the same question. Always search to see if find an answer before asking a question. For both these site, it is very important to provide a minimal reproducible example. That is example data (perhaps a built-in dataset) and code that can be run to show the problem. Code not relevant to the problem should be removed. The package reprex is useful for making reproducible example. Very often, making the reproducible example is enough to identify and fix the problem.

7.3 Artificial Intellegence help

In the last year or two, AI language models have been developed that can help coding in R. This is an area with rapid development, with new products appearing regularly. You ask the AI a question and the AI will reply.

I asked chatGPT, a “How can chatGPT help me code in R?” and it replied

ChatGPT can assist you with coding in R by providing explanations, examples, and solutions to your programming questions. Here’s how it can help you:

  • Code Snippets and Examples: You can ask for specific code examples in R for various tasks. Whether you need help with data manipulation, visualization, statistical analysis, or any other programming task in R, ChatGPT can provide you with relevant code snippets to get you started.

  • Syntax and Function Explanations: If you’re unsure about the syntax of a particular R function or how to use it, you can ask ChatGPT to explain the function’s purpose and how to correctly use it in your code.

  • Debugging Assistance: If you encounter errors or bugs in your R code, you can describe the issue to ChatGPT, and it can help you identify potential reasons for the problem and suggest ways to fix it.

  • Algorithm and Approach Suggestions: If you’re working on a specific task or problem and need advice on the best algorithms or approaches to use in R, ChatGPT can provide recommendations based on its programming knowledge.

  • Guidance on Best Practices: ChatGPT can provide insights into coding best practices in R, such as efficient ways to write code, optimize performance, and structure your projects.

  • Data Analysis and Visualization: If you’re working on data analysis and visualization tasks, ChatGPT can guide you on using popular R packages like dplyr, ggplot2, and others to clean, analyze, and visualize your data effectively.

  • Understanding Concepts: If you’re struggling to understand certain programming concepts in R, you can ask ChatGPT to explain those concepts in a clear and concise manner.

  • Learning Resources: ChatGPT can suggest online tutorials, documentation, and resources to further enhance your understanding of R programming.”

You need a account to use chatGPT. This is free for GPT-4o mini or costs $20/month for the more powerful GPT-4o.

ChatGPT-4o mini was trained on information available in October 2023. It has no information about anything that happened after that date, so may not know about changes in the latest versions of packages that you use.

Phind uses a search engine and summarises what it finds in the output. This means that it knows about recent developments, it also gives the sources it used

It is possible to use copilot within Rstudio to make code suggestions. Copilot is free if you have a verified student account on GitHub.

Be aware that these models do not understand how code works and often generate nonsense code. If you use them, you have to check that the code does what you want. You are responsible for your code whether you write it yourself of with assistance from artificial intelligence.

Exercise

Ask Phind to explain the error message from the following code

lenght(1:10)
Error in lenght(1:10): could not find function "lenght"

Contributors

  • Jonathan Soulé
  • Aud Halbritter
  • Richard Telford