1

I'm new to Markdown, and I want to create a PDF report that has separate paragraphs (or pages will be even better) for each row of data in a data frame. There is a similar post here, but I can't seem to replicate the answer.

Here is my sample data frame:

df <- data.frame(
  Company = c("Uber", "AT&T", "Ford"),
  Stock.Ticker = c("UBER", "T", "F"),
  Price = c(61, 26, 11),
  Date = c("01/11/2021","01/13/2021","1/21/2021"),
  Description = c("Uber Technologies, Inc. develops and operates proprietary technology applications.",
                  "AT&T Inc. provides telecommunication, media, and technology services worldwide.",
                  "Ford Motor Company designs, manufactures, markets, and services a range of Ford trucks, cars, sport utility vehicles.")
)

The output I'm looking for is:

[Uber Technologies, Inc. develops and operates proprietary technology applications.] [Uber]'s stock ticker is [UBER]. As of [01/11/2021], the stock price is $[61].


[AT&T Inc. provides telecommunication, media, and technology services worldwide.] [AT&T]'s stock ticker is [T]. As of [01/13/2021], the stock price is $[26].


[Ford Motor Company designs, manufactures, markets, and services a range of Ford trucks, cars, sport utility vehicles.] [fORD]'s stock ticker is [f]. As of [01/21/2021], the stock price is $[11].
T-T
  • 693
  • 1
  • 10
  • 24

2 Answers2

3

You will want to output the results of your code chunk with the chunk option results='asis' and cat() the output so that you are able to essentially write the necessary LaTeX directly.

Note, I had to escape the ampersand in the two AT&Ts with \\ because the ampersand is a special character in LaTeX and needs to be escaped with a \ and we need to escape that backslash with another backslash to represent a single backslash in an R character. There is an identical issue for getting the two \newline inserted in the final LaTeX.

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

```{r, results='asis'}
df <- data.frame(
  Company = c("Uber", "AT\\&T", "Ford"),
  Stock.Ticker = c("UBER", "T", "F"),
  Price = c(61, 26, 11),
  Date = c("01/11/2021","01/13/2021","1/21/2021"),
  Description = c("Uber Technologies, Inc. develops and operates proprietary technology applications.",
                  "AT\\&T Inc. provides telecommunication, media, and technology services worldwide.",
                  "Ford Motor Company designs, manufactures, markets, and services a range of Ford trucks, cars, sport utility vehicles.")
)

with(df, cat(sprintf(
  "%s %s's stock ticker is %s. As of %s, the stoke price is $%s.\\newline\\newline",
  Description, Company, Stock.Ticker, Date, Price
)))
```
the-mad-statter
  • 5,650
  • 1
  • 10
  • 20
  • Thanks! How can I get rid of the `[]` in the output file? I used it in my example to show that they are from the data frame. And what would be the code for a new paragraph? – T-T Feb 11 '21 at 21:33
  • I removed the `[]`s from the `sprintf()` format argument and swapped the `\\newpage` for two `\\newline`s to get new paragraphs. – the-mad-statter Feb 11 '21 at 21:39
0

I would use a loop.


for (company in df$company) {

   cat ( paste(
       df$description[df$company == company],
       company,
       df$Stock.Ticker))

   cat ("\n\\newpage")
}
CALUM Polwart
  • 497
  • 3
  • 5