Snoop Dogg
Snoop Dogg

Reputation: 391

How to include the description of a data set in RMarkdown?

I am creating an RMarkdown for teaching and I would like to include the description of the data set on the R Markdown file. For example, if I use the data marketing from the R package datarium, I would like to be able to include the description obtained with ?marketing without having to open it online or in R.

marketing {datarium}    R Documentation
Marketing Data Set
Description
A data frame containing the impact of three advertising medias (youtube, facebook and newspaper) on sales. Data are the advertising budget in thousands of dollars along with the sales. The advertising experiment has been repeated 200 times.

Usage
data("marketing")
Format
A data frame with 200 rows and 4 columns.

Examples
data(marketing)
res.lm <- lm(sales ~ youtube*facebook, data = marketing)
summary(res.lm)

Is this possible?

Upvotes: 1

Views: 352

Answers (1)

Snoop Dogg
Snoop Dogg

Reputation: 391

Using @MrFlick (more like MrShy) suggestion:

How to get text data from help pages in R?

We can create an R Markdown (I also wanted to hide the function used to get the help text) showing the description of the data as follows:

---
title: "Marketing"
author: 'Jon Doe'
date: ""
output: html_document
---

```{r}
library(datarium)

data("marketing")
```

```{r include=FALSE}
help_text <- function(...) {
  file <- help(...)
  path <- dirname(file)
  dirpath <- dirname(path)
  pkgname <- basename(dirpath)
  RdDB <- file.path(path, pkgname)
  rd <- tools:::fetchRdDB(RdDB, basename(file))
  capture.output(tools::Rd2txt(rd, out="", options=list(underline_titles=FALSE)))
}
```
```{r}
# ?marketing
cat(help_extract(marketing), sep="\n")


# Data
head(marketing, 4)
```

Upvotes: 1

Related Questions