Building package functions
1. Building package functions
Let's add a function to our package.2. R package structure
Recall from our example structure that these will live in the R directory.3. Different temperature scales
Assume we have a friend in Montreal and a friend in Miami. One summer day, our Montreal friend told us the temperature outside was thirty-five degrees, and she loved it. Confused by what we think is a cold temperature, we tell our Miami friend, and they tell us the thirty-five is in Celsius, not the Fahrenheit we are used to. Let's build a function to help us convert this.4. Converting using R
In R, we set the Celsius value to thirty-five and specify the formula for going from Celsius to Fahrenheit. It turns out 35 degrees Celsius is 95 degrees Fahrenheit!5. Refresher on R functions
Let's put this computation into a function using a conditional flow that converts Celsius to Fahrenheit and vice versa. We name our function temp_converter. The function accepts three arguments: the temperature value to convert, the initial unit (default: Celsius), and the target unit (default: Fahrenheit). The function performs a conversion depending on unit_from and unit_to. If Celsius is given in the given input, then we convert to Fahrenheit, else we perform the opposite conversion. For the same units, return back the original input with a warning. Finally, the function throws an error for inputs other than "Celsius" or "Fahrenheit".6. Using temp_converter()
Let's try out the temp_converter function on some different inputs. Firstly, we convert 100 degrees Celsius to Fahrenheit. The result of the conversion is two hundred twelve degrees Fahrenheit. Let's see if it correctly converts ninety-five degrees Fahrenheit to Celsius from our summer example. It produces the expected 35 degrees Celsius.7. Some R function best practices
We've created a temperature converter function, but we can add any custom R function to a package. Let's review some best practices for writing R functions for package development. The name of the function should describe what the function does. This makes the code more readable and self-documenting. Make sure that functions handle incorrect or unexpected input gracefully, as we did with our unit arguments. Each function should do one thing and do it well. Here, we are converting temperatures.8. Store a function in a file
Now that we have our temp_converter function code written, we can output it to a dot-R script file in the R directory of our package. This can be done with the base R dump function. Its first argument is a string with the object we'd like to output, and the second argument is the file we'd like this output to go to. We specify the temp_converter function object and R-slash-temp_converter-dot-R as the place to put this function. We store all of our functions in files in this R directory. We now have this package structure.9. Install the package with devtools
We can use the devtools package to test our unitConverter package. devtools contains a variety of helpful functions for package development. Its install function requires no arguments since we are working from the package working directory. This function generates a log displaying the actions that were executed. Here, we show some of that output.10. Call temp_converter() temperature_data
Now that the package is installed, we can use the temp_converter function on the data we previously loaded. Recall the first row of the temperature_data. We load the package. To call the function, we specify the name of the package followed by three colons and function's name; here that is unitConverter, three colons, and temp_converter. We convert this Celsius value to Fahrenheit by accessing the first row for value and unit and then passing Fahrenheit for unit_to. The result is just over ninety-seven degrees Fahrenheit.11. Let's practice!
Get to building your first R package!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.