refaforest.blogg.se

Rmarkdown print variable
Rmarkdown print variable






Let’s create a scatter plot with GDP per capita on the x axis, life expectancy on the y axis, population for the size of the points, and continent for color. We can also use the year parameter in other places in the code, such as a label in a plot. library(tidyverse) gapminder %>% filter(year = params$year) %>% head() We’ll replace 2002 with the year parameter we just created.

rmarkdown print variable

title: "Hardcoded Parameters" author: "RealDrewData" date: "" output: html_document params: year: 2002. When running an R Markdown, a params object will be created, and the named parameters will be available for our use. In our yaml header, we will add another item called params and underneath it, with 2 spaces in front, we’ll create a year parameter.

rmarkdown print variable

We might want to run this report for a different year, making this a good candidate to create a parameter for. Using the tidyverse, that code looks like this: library(tidyverse) gapminder %>% filter(year = 2002) %>% head() We’ll start by filtering the data frame for the year 2002. The gapminder data set contains 6 variables: country, continent, year, lifeExp (life expectancy), pop (population), and gdpPercap ( gross domestic product per capita). # Setup # install.packages("gapminder") library(gapminder) # The data frame gapminder Check out Hans Rosling’s (one of the Gapminder founders) TED talk, or the Gapminder website for more information. It has a subset of the indicators collected by the Gapminder foundation. This is a data set available from the gapminder package. By referencing the parameters from the yaml header, any updates to them that need to be made will only require one change.įor this example, we’ll look at the gapminder data set. Once they’re coded in, they will be available in the params object for use in the rest of the analysis. In the yaml header (the section at the top of the markdown document), you just have to add a couple new lines for your parameters to hardcode them in. Parameters in an R Markdown document are very simple to use. Luckily R Markdown has a feature called parameters which provide solutions to all of these problems. While dotenv is an option, it requires slightly more setup to work correctly. Giving out that R Markdown file or leaving it on a shared network drive raises security concerns.

rmarkdown print variable

When working on some ad hoc analysis, it is common to leave database passwords in the code while working out the details of the analysis. Giving an analysis out can also be troublesome. It could be in multiple filters that all need to get changed. We run into the same issue when switching the analysis from a group A to a group B or changing the time frame of the analysis data. If the connection or file to import is defined in multiple places, it can be easy to miss one and have invalid output.

rmarkdown print variable

csv to another or from the testing database to the production database. Changing the source of an analysis can be difficult, whether it is from one. There are some problems that we often encounter when trying to fulfill those requests. We’ve all been in a meeting where someone asks, “can you run that analysis again for a different time period,” or, “can you run it for group B instead of group A,” or, “can you send me that analysis so that I can play around with it?”








Rmarkdown print variable