4

I have this dataframe:

df <- structure(list(letters = c("A", "B", "C", "D"), Number = 1:4, 
    Info = c("additionalinfo", "additionalinfo", "additionalinfo", 
    "additionalinfo")), class = "data.frame", row.names = c("1", 
"2", "3", "4"))


  letters Number           Info
1       A      1 additionalinfo
2       B      2 additionalinfo
3       C      3 additionalinfo
4       D      4 additionalinfo

I try to add a new column combining two columns with a soft break or line break:

df %>% 
  mutate(answer = paste(Number, Info, sep = " \n" ))

Desired output:

enter image description here

Background: Within this project Randomly take wrong answers of a quiz question from dataframe column, instead of doing by hand I want to add the answer with a soft break or line break

It should print like here in the first question. Which number has E?: enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66

2 Answers2

2

If you need the answer column without displaying the entire dataframe you can try

df %>% 
  mutate(answer = paste(Number, Info, sep = "\n" )) %>% 
  pull(answer) %>% 
  cat(sep = "\n")
1
additionalinfo
2
additionalinfo
3
additionalinfo
4
additionalinfo

Or, item by item

df2 <- df %>% 
  mutate(answer = paste(Number, Info, sep = "\n" ))

cat(df2$answer[1])
cat(df2$answer[2])
...
cat(df2$answer[n])
Leonardo
  • 2,439
  • 33
  • 17
  • 31
2

For shiny-rendered docs inline HTML using line break <br> could be used.

---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---

#```{r setup, include=FALSE}
library(learnr); library(dplyr); library(glue)
knitr::opts_chunk$set(echo = FALSE)
df <- structure(list(letters = c("A", "B", "C", "D"), Number = 1:4, 
    Info = c("additionalinfo", "additionalinfo", "additionalinfo", 
    "additionalinfo")), class = "data.frame", row.names = c("1", 
"2", "3", "4"))
df <- df %>% mutate(answer = glue("{Number}<br>{Info}"))
#```

```{r quiz}
# This works not
quiz(
 question("Which number has E?",
       answer(df[[4]][1]),
       answer(df[[4]][2]),
       answer(df[[4]][3], correct = TRUE),
       answer(df[[4]][4])         
  ),
  # This one works
  question("Which number has E?",
       answer("1"),
       answer("4"),
       answer("5", correct = TRUE),
       answer("8")
  )
 )
Donald Seinen
  • 4,179
  • 5
  • 15
  • 40