Search code examples
rdataframepasteline-breakslearnr

Line break or soft break after paste


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


Solution

  • 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")
      )
     )