Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smoother introduction to functions? #865

Open
martinherrerias opened this issue Sep 5, 2023 · 1 comment
Open

Smoother introduction to functions? #865

martinherrerias opened this issue Sep 5, 2023 · 1 comment

Comments

@martinherrerias
Copy link

martinherrerias commented Sep 5, 2023

How could the content be improved?

Ch. 10. Functions Explained starts with examples of functions (fahr_to_kelvin and kelvin_to_celsius) that are clunky to type (draw attention away from the matter) and not rich enough to explain general function syntax and scoping. Then, halfway down the lesson, and while working on a more complicated function, there's suddenly: multiple-arguments, default values, the fact that return() can be left out, and scoping rules.

I'd suggest:

  • Start with much simpler functions: e.g. sum two values, print arguments, etc. (with zero, one, two, ... arguments)
  • Address basic scoping (see below) and the role of return() early on, on those simple functions
  • Scoping can be problematic in R (a mistyped name can be found in a parent environment), and is at the same time one of the main arguments for using functions, so it should take more than a note.

Which part of the content does your suggestion apply to?

https://swcarpentry.github.io/r-novice-gapminder/10-functions.html

@Malberten
Copy link

On the point about the return() statement (not sure if this deserves its own issue, but it fits here nicely):

Further down it is explained, that "R will automatically return the results of whatever command is executed on the last line of the function" (https://swcarpentry.github.io/r-novice-gapminder/10-functions.html#more-on-combining-functions). I think this statement is misleading, since you could think that in the example above you can just remove the return() line and it works the same. But you would also need to omit the "gdp<-" in the line before.

So I would suggest to rather include to equivalent examples, one with a return() statement, and one without.

Apart from that I fully agree with the issues raised by @martinherrerias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants