1. Creating a report with a parameter
Throughout the course, we've created reports that present information on the projects in a particular country.
2. Parameters
Using the investment_services_projects data, we could create reports for a number of different countries. If we want to create a report for 10 countries from the data, it would require a lot of time and effort to create each report separately and manually edit each report for each country. Instead of manually editing new reports, we can use a parameter to efficiently create new reports that summarize the data for different countries.
Parameters are used to create a report with specific values for key inputs that are listed in the YAML header.
3. Adding a parameter
Let's add a parameter that will help create a report for any country we specify. At the top of the document, within the YAML header, we add the params field and a colon.
We then add another line, indent it, and add the parameter name, followed by a colon. Let's call this parameter country, since we'll be using it to indicate the country that will be the focus of the report, and list Indonesia as the country. Notice that Indonesia is not listed in quotations. Recall that specific indentation must be used correctly in the YAML header for the report to be successfully knit.
After adding the parameter, we should review each element of the document, the code, text, and YAML header, for any references to a specific country.
4. Reviewing the code
When reviewing the code, we'll want to rename any code chunks and objects that reference a particular country
5. Reviewing the code
to avoid confusion for others who may be reviewing the code for subsequent reports that focus on other countries.
6. Reviewing the code
Within each code chunk, we review and replace any instance where we referenced a particular country with
7. Reviewing the code
params dollar sign country, where country is the parameter we added to the YAML header.
8. Reviewing the code
We also need to review the labels used in the plots we created,
9. Reviewing the code
and remove any references to a particular country.
10. Reviewing the text
While reviewing the text, we'll want to edit references to a particular country so that when we knit a new file using the parameter, the file will be consistent with the country we specify.
11. Reviewing the text
We can modify text, headers, and even the YAML header using syntax we used when adding date formatting to the date field in the YAML header earlier in the course.
Where the text or header refers to a specific country name, we can replace the name with, in backticks, the letter r to specify the language and params dollar sign country.
12. Reviewing the YAML header
We'll then review the YAML header and modify the document title to include the country using the parameter.
Using the same syntax we used to modify the text and headers, in backticks, we specify r as the language followed by params dollar sign country.
13. Knitting the report
When we knit the document using Turkey for the country parameter, we see the resulting report is updated with the information from this country.
14. Knitting a new report
Let's knit the document using another country, the Philippines. Notice that we only need to replace the country parameter and the file knits with the updated information.
15. Let's practice!
Parameters are an efficient way to save time when creating reports. Let's practice!