Chapter 14 Functions

This chapter is in development...

Exercise 14.4.1

The following app plots user selected variables from the msleep dataset for three different types of mammals (carnivores, omnivores, and herbivores), with one tab for each type of mammal. Remove the redundancy in the selectInput() definitions with the use of functions.

Solution. Solution

We can see a pattern here where we are creating the same type of plot for each tabset panel, with the only variable changing being the vore argument. We can reduce everything we see in triplicate to functions! We can use map to create a single create_panels function which will create a tab for each of our species. On the server side, the data is filtered three times, and the plots are created three times. We can create a single rendering function that given the correct string it will filter the data, create the correct plot, and assign it to the correct output.

Exercise 14.4.2

Continue working with the same app from the previous exercise, and further remove redundancy in the code by modularizing how subsets and plots are created.

Solution. Solution

TODO: I’m unsure what to do with this one since we haven’t yet introduced modules?

Exercise 14.4.3

Suppose you have an app that is slow to launch when a user visits it. Can modularizing your app code help solve this problem? Explain your reasoning.

Solution. Solution

No, we’re just packaging our code into neater functions - this doesn’t change or optimize what is loaded when the app is launched. In fact, modularizing might even make your application slower in some cases.