Getting help

Andrew Irwin, a.irwin@dal.ca

2024-02-08

What to do when you are stuck?

  • How do I … ?

  • Why did R do X when I wanted it to do Y?

  • My code doesn’t work!

  • Help!!

Learning technical skills is challenging

  • There are many ways to approach a task

  • You need to figure out how to describe what you want to accomplish and how to do it

  • You need to evaluate your decisions – is that hard-won figure effective?

  • You need a strategy for making notes and building your own knowledge

  • Even small errors can be hard to find and cause a lot of frustration

Ways to get help

  • Finding and fixing mistakes

  • Learning from examples

  • Remembering what you learned before

  • Learning something new

  • Asking questions

Finding and fixing mistakes

gapminder |>
  filter(country == "Canada", country == "Iran")
# A tibble: 0 × 6
# ℹ 6 variables: country <fct>, continent <fct>, year <int>, lifeExp <dbl>,
#   pop <int>, gdpPercap <dbl>
gapminder |>
  filter(country == "Canada" | country == "Iran")  # logical "or"
# A tibble: 24 × 6
   country continent  year lifeExp      pop gdpPercap
   <fct>   <fct>     <int>   <dbl>    <int>     <dbl>
 1 Canada  Americas   1952    68.8 14785584    11367.
 2 Canada  Americas   1957    70.0 17010154    12490.
 3 Canada  Americas   1962    71.3 18985849    13462.
 4 Canada  Americas   1967    72.1 20819767    16077.
 5 Canada  Americas   1972    72.9 22284500    18971.
 6 Canada  Americas   1977    74.2 23796400    22091.
 7 Canada  Americas   1982    75.8 25201900    22899.
 8 Canada  Americas   1987    76.9 26549700    26627.
 9 Canada  Americas   1992    78.0 28523502    26343.
10 Canada  Americas   1997    78.6 30305843    28955.
# ℹ 14 more rows

Finding and fixing mistakes

gapminder |>
  filter(country == "Canada" | country == "Iran") |>
  ggplot(aes(x = Year, y = LifeExp, color = Country)) +
  geom_point()
Error in `geom_point()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 1st layer.
Caused by error:
! object 'Year' not found
gapminder |>
  filter(country == "Canada" | country == "Iran") |>
  ggplot(aes(x = year, y = lifeExp, color = country)) +
  geom_point()

Learning from examples

ggplot(economics, aes(unemploy, date)) + 
  geom_line(orientation = "y")  

Learning from examples

ggplot(economics, aes(unemploy, date)) + 
  geom_line()

Remembering what you learned before

How do I change the x and y axis labels?

  • Google search. Add “R” to your search.

  • Know which sites you like to read

  • Learn to read documentation – more condensed information

    • https://ggplot2.tidyverse.org/ especially “cheatsheet” and “reference”
    • Scales > Axis > Labs: https://ggplot2.tidyverse.org/reference/labs.html
  • Make notes for yourself

Learn something new

How do I set the limits, tick marks, and numbering on an axis?

  • ggplot2 tidyverse “Scales”: xlim, ylim

  • Search.

    • Find theme
    • Find scale_x_discrete(name, breaks, labels, limits)
    • Also scale_x_continuous(name, breaks, lables, limits, trans)
  • Try an example from the help page (or internet)

p1 <- ggplot(mpg, aes(displ, hwy)) + geom_point()
p1 + scale_x_continuous(name = "Displacement", 
                        limits = c(2, 6),
                        breaks = c(2, 4, 6),
                        labels = c("Two", "Four", "Six"))

Read a bit of ?c (combine values into a vector or list).

Asking questions

You are still stuck.

How do you ask for help?

  • What are you trying to do?
  • What have you tried?
  • What was the result (output, graphic, error message)?
  • What do you think the problem might be?

Notes on functions used in this course

I’ve made a list of many of functions that I use in the first few weeks of the course, and the package you need for each, and put them in the course notes under “R Review”.

Summary

  • Learning computing skills is a never-ending task

  • Using reference material, blogs, and books is a learned skill that you can get better at

  • Try different sources until you learn what you like to learn from the most (and sometimes try other sources)

  • Asking questions is a valuable skill. Clearly expressing your question so that someone else can read it will often help you solve the problem by yourself.

  • It gets easier with practice: posing questions, understanding the answers, and remembering how it all fits together

Task

Assignment 2 asks you to learn some new skills and describe what you learned.