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

Function Development - Brainstorm #10

Open
AndersAskeland opened this issue Mar 17, 2024 · 7 comments
Open

Function Development - Brainstorm #10

AndersAskeland opened this issue Mar 17, 2024 · 7 comments
Assignees

Comments

@AndersAskeland
Copy link
Member

AndersAskeland commented Mar 17, 2024

Previous section

The preceding section is: sessions/package-setup.qmd.

During this section, participants learn about setting up and preparing the R environment for creating packages.

Learning outcome

Overarching goal

  • Learn to create and structure functions that can be effectively utilized within R packages.

Sub goals

  • Develop code interactively (exercise 1) and transform it into robust functions (exercise 2).
  • Understand and apply function abstraction to enhance modularity and reusability (exercise 3).
  • Master dependency management within package development (exercise 4).
  • Implement process control and error handling to ensure function reliability (exercise 5).

Exercises

The following exercises are designed to develop a comprehensive understanding of function development in relation to R package creation. The order should be adjusted based on the flow of the course.

Exercise 1 - Write Code/Script

Objective: Write a script that addresses a specific problem, showcasing the practical application of R code.

  • Provide a specific scenario or problem statement for participants to solve.
  • Offer a ready script/code snippet if time is short.

Note: If previous sections create functions/code - use this.

Exercise 2 - Rewrite Code into a Function

Objective: Convert the given script into a function.

  • Emphasize aspects such as function naming, argument structuring. Documentation comes later.
  • Code should be hard coded

Exercise 3 - Abstractions

Objective: Evolve the function by abstracting the action of extracting elements, enhancing its flexibility and reusability.

  • Use examples to demonstrate the transition from specific implementations to abstracted functionalities.
  • Place function in utils?

Exercise 4 - Dependency Management

Objective: Learn to accurately declare and manage package dependencies.

  • Introduce and explain the use of Imports, Depends, and Suggests in the DESCRIPTION file.
  • Provide examples of how to manage dependencies correctly and the consequences of poor management.
  • Attach package/function?

Exercise 5 - Error Handling/Control Flow

Objective: Incorporate error checking and control structures within functions.

  • Detail the use of stop(), warning(), and message() for providing feedback at various levels.

For example:

  • There is not title/author/etc.
  • Input is not "correct"/text.

Extra Exercise - Extending Generic Functions

  • Custom print method?
  • Probably too complex.

Optional Reading

  • Control flow - Loops. Are often needed in functions that interact with other systems (i.e. servers, http requests, etc.)

Next Section

Documentation

@AndersAskeland AndersAskeland self-assigned this Mar 17, 2024
@AndersAskeland AndersAskeland changed the title Test Function Development - Introduction Mar 17, 2024
@AndersAskeland AndersAskeland changed the title Function Development - Introduction Function Development - Paragraph 1. Introduction Mar 17, 2024
@AndersAskeland AndersAskeland changed the title Function Development - Paragraph 1. Introduction Function Development - Brainstorm Mar 18, 2024
@AndersAskeland AndersAskeland moved this from Todo to In Progress in planning for r-pkg-intro development Mar 18, 2024
@AndersAskeland
Copy link
Member Author

AndersAskeland commented Mar 19, 2024

Consider using cli() for error "documentation" according to the Tidyverse style-guide (https://style.tidyverse.org/error-messages.html#problem-statement).

@AndersAskeland
Copy link
Member Author

Is there a need for participants to learn about breaks (break() & next())?

Don't see any real use for it as such (in the scope of the exercises), but could perhaps be a extra reading task or a reference?

@AndersAskeland
Copy link
Member Author

Great quote related to complex functions: "When I look at that function signature I hear the screams of matrices being ground to a fine powder of bits through the conical burrs of nested for loops, and laboured index arithmetic." https://www.milesmcbain.com/posts/data-analysis-reuse/

@AndersAskeland
Copy link
Member Author

Idea for a section/anti-session: "One function to rule them all"

@AndersAskeland
Copy link
Member Author

Quote: "Packages are a way of describing and distributing functions in a structured and consistent way."

@AndersAskeland
Copy link
Member Author

Exercise 5 - Give an unsupported variable to function, and see error. It is not easy to know what it means

@lwjohnst86
Copy link
Member

I don't know why I wasn't shown these notifications 🤔. In relation to your question about break etc, I agree, I don't think that's necessary.

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

No branches or pull requests

2 participants