粘贴后换行或软断

Line break or soft break after paste

我有这个数据框:

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

我尝试 添加一个新列,将两列与软分隔符或换行符组合起来:

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

期望的输出:

背景: 在这个项目中 我想用 soft breakline break

添加答案

在第一个问题中应该像这里这样打印。哪个数字有E?:

如果您需要 answer 列而不显示整个数据框,您可以尝试

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

或者,逐项

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

cat(df2$answer[1])
cat(df2$answer[2])
...
cat(df2$answer[n])

对于 shiny-rendered 文档内联 HTML 使用换行符 <br> 可以使用。

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