Skip to content

Function Development - Brainstorm #10

Description

@AndersAskeland

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

Activity

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

Metadata

Metadata

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions