Skip to content

Instantly share code, notes, and snippets.

@colearendt
Last active March 28, 2018 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colearendt/132e4ae84342e3aa93772a21ddf49a2a to your computer and use it in GitHub Desktop.
Save colearendt/132e4ae84342e3aa93772a21ddf49a2a to your computer and use it in GitHub Desktop.
RMarkdown Examples
---
title: "airplanes"
params:
carrier: AA
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE)
```
```{r}
print(params$carrier)
library(odbc)
library(DBI)
con <- dbConnect(odbc::odbc(), "Postgres (DSN)")
# pull data using DBI
airlines <- dbReadTable(con, "airlines")
# get the name of the airline
my_airline <- airlines[airlines$carrier == params$carrier, "name"]
```
```{r}
library(dplyr)
library(dbplyr)
flights <- tbl(con, "flights")
# use dbplyr to create SQL query
my_carrier_flights <- flights %>%
filter(carrier == params$carrier) %>%
select(carrier, year, month, day, dep_time, arr_time)
```
# `r my_airline`
This is a report about `r my_airline` and all of the wonderful arrival times that they have.
# A Plot!
```{r}
library(ggplot2)
ggplot(my_carrier_flights %>% collect(), aes(arr_time)) + geom_histogram()
```
---
title: "rmarkdown"
output: html_document
params:
animal: Elephant
---
# A fantastic report about my `r params$animal`
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
# Call the Python and R Notebook
rmarkdown::render(
"name-notebook.Rmd"
, params = list(name = "Cole")
, output_file="cole.html"
) %>%
rstudioapi::viewer() # this opens the output in RStudio
# Build a bunch of animal reports
animals <- list("Elephant", "Giraffe", "Koala", "Anaconda")
lapply(animals
, function(x){
rmarkdown::render(
"animal-report.Rmd"
, params=list(animal = x)
, output_file = tolower(
paste0(x,".html")
)
)
}
)
# Build the airplane reports (requires a database with airlines and flights tables)
carriers <- list("AA", "DL", "WN")
build_airplane_report <- function(carrier) {
rmarkdown::render("airplane-report.Rmd"
, params=list(carrier = carrier)
, output_file = tolower(paste0(carrier,".html"))
)
}
lapply(carriers, build_airplane_report)
---
title: "R Notebook"
params:
name: my-name
output:
html_document:
df_print: paged
---
```{r}
library(reticulate)
py_available(initialize = TRUE)
```
# `r params$name` Report
## in Python
```{python}
print(r.params['name'])
```
## in R
```{r}
print(params$name)
```
## Check it out
[Python & R - Together Forever](https://blog.rstudio.com/2018/03/26/reticulate-r-interface-to-python)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment