Get startedGet started for free

Collapsing blocks in the knit report

By default, the collapse option is set to FALSE, and the code and any output appear in the knit file in separate blocks. You encountered this earlier when creating plots of the data. In this exercise, you'll modify the Markdown file so that the code and resulting warning messages appear in the same block.

This exercise is part of the course

Reporting with R Markdown

View Course

Exercise instructions

  • Using collapse, modify each code chunk that renders a plot with a warning, so that the code and output are not split into multiple blocks in the final report.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

{"investment_report.Rmd":"---\ntitle: \"Investment Report\"\ndate: \"`r format(Sys.time(), '%d %B %Y')`\"\noutput: html_document\n---\n\n```{r setup, include = FALSE}\nknitr::opts_chunk$set(fig.align = 'center')\n```\n\n```{r data, include = FALSE}\nlibrary(readr)\nlibrary(dplyr)\nlibrary(ggplot2)\nlibrary(knitr)\n\ninvestment_annual_summary <- read_csv(\"https://assets.datacamp.com/production/repositories/5756/datasets/d0251f26117bbcf0ea96ac276555b9003f4f7372/investment_annual_summary.csv\")\ninvestment_region_summary <- read_csv(\"https://assets.datacamp.com/production/repositories/5756/datasets/52f5414f6504e0503e86eb1043afa9b3d157fab2/investment_region_summary.csv\")\ninvestment_services_projects <- read_csv(\"https://assets.datacamp.com/production/repositories/5756/datasets/bcb2e39ecbe521f4b414a21e35f7b8b5c50aec64/investment_services_projects.csv\")\n```\n\n\n## Datasets \n### Investment Annual Summary\nThe `investment_annual_summary` dataset provides a summary of the dollars in millions provided to each of the following regions for each fiscal year, from 2012 to 2018:\n\n1. East Asia and the Pacific      \n2. Europe and Central Asia        \n3. Latin America and the Caribbean\n4. Middle East and North Africa   \n5. South Asia                     \n6. Sub-Saharan Africa\n\n```{r investment-annual-summary, out.width = '85%', fig.cap = 'Figure 1.1 The Investment Annual Summary for each region for 2012 to 2018.'}\nggplot(investment_annual_summary, aes(x = fiscal_year, y = dollars_in_millions, color = region)) +\n  geom_line() +\n  labs(\n    title = \"Investment Annual Summary\",\n    x = \"Fiscal Year\",\n    y = \"Dollars in Millions\"\n  )\n```\n\n```{r tables}\nkable(investment_region_summary, col.names = c(\"Region\", \"Dollars in Millions\"), align = \"cc\", caption = \"Table 1.1 The total investment summary for each region for the 2012 to 2018 fiscal years.\")\n```\n\n### Investment Projects in Brazil\nThe `investment_services_projects` dataset provides information about each investment project from 2012 to 2018. Information listed includes the project name, company name, sector, project status, and investment amounts.\n\n```{r brazil-investment-projects, out.width = '95%', fig.cap = 'Figure 1.2 The Investment Services Projects in Brazil from 2012 to 2018.'}\nbrazil_investment_projects <- investment_services_projects %>%\n  filter(country == \"Brazil\") \n\nggplot(brazil_investment_projects, aes(x = date_disclosed, y = total_investment, color = status)) +\n  geom_point() +\n  labs(\n    title = \"Investment Services Projects in Brazil\",\n    x = \"Date Disclosed\",\n    y = \"Total IFC Investment in Dollars in Millions\"\n  )\n```\n\n### Investment Projects in Brazil in 2018\nThe `investment_services_projects` dataset was filtered below to focus on information about each investment project from the 2018 fiscal year, and is referred to as `brazil_investment_projects_2018`. \n\n```{r brazil-investment-projects-2018, out.width = '95%', fig.cap = 'Figure 1.3 The Investment Services Projects in Brazil in 2018.'}\nbrazil_investment_projects_2018 <- investment_services_projects %>%\n  filter(country == \"Brazil\",\n         date_disclosed >= \"2017-07-01\",\n         date_disclosed <= \"2018-06-30\") \n\nggplot(brazil_investment_projects_2018, aes(x = date_disclosed, y = total_investment, color = status)) +\n  geom_point() +\n  labs(\n    title = \"Investment Services Projects in Brazil in 2018\",\n    x = \"Date Disclosed\",\n    y = \"Total IFC Investment in Dollars in Millions\"\n  ) \n```\n\n\n"}
Edit and Run Code