5  换行符保存

文本中含有换行符,需保存为什么格式?

Published

April 26, 2026

1. 先生成一个示例data frame

# Generate a tibble
df <- tibble::tibble(
    x = letters[1:5],
    y = rep(1, 5),
    labels = c("a \nb \nc", "b\nc", "c\nd", "d\ne", "e\nf")
)

# View df
df
# A tibble: 5 × 3
  x         y labels     
  <chr> <dbl> <chr>      
1 a         1 "a \nb \nc"
2 b         1 "b\nc"     
3 c         1 "c\nd"     
4 d         1 "d\ne"     
5 e         1 "e\nf"     

2. 将df分别存储为csv、txt、xlsx、和rds格式

# Save as csv
df |> 
    readr::write_csv(file = "raw_data/2026-04-26_line-feed.csv")

# Save as txt
df |> 
    readr::write_tsv("raw_data/2026-04-26_line-feed.txt")

# Save as excel
df |> 
    writexl::write_xlsx("raw_data/2026-04-26_line-feed.xlsx")

# Save as rds
df |> 
    readr::write_rds("raw_data/2026-04-26_line-feed.rds")

# View the saved files
"raw_data/" |> 
    fs::dir_tree(regexp = "^raw_data/2026-04-26_line-feed")
raw_data/
├── 2026-04-26_line-feed.csv
├── 2026-04-26_line-feed.rds
├── 2026-04-26_line-feed.txt
└── 2026-04-26_line-feed.xlsx

3. 读取存储的4个文件

# read csv file
df_csv <- 
    "raw_data/2026-04-26_line-feed.csv" |> 
    readr::read_csv(show_col_types = FALSE)

# view
df_csv
# A tibble: 5 × 3
  x         y labels     
  <chr> <dbl> <chr>      
1 a         1 "a \nb \nc"
2 b         1 "b\nc"     
3 c         1 "c\nd"     
4 d         1 "d\ne"     
5 e         1 "e\nf"     
# read txt file
df_txt <- 
    "raw_data/2026-04-26_line-feed.txt" |> 
    readr::read_tsv(show_col_types = FALSE)

# view
df_txt
# A tibble: 11 × 3
   x         y labels
   <chr> <dbl> <chr> 
 1 a         1 a     
 2 b        NA <NA>  
 3 c        NA <NA>  
 4 b         1 b     
 5 c        NA <NA>  
 6 c         1 c     
 7 d        NA <NA>  
 8 d         1 d     
 9 e        NA <NA>  
10 e         1 e     
11 f        NA <NA>  
# read xlsx file
df_xlsx <- 
    "raw_data/2026-04-26_line-feed.xlsx" |> 
    readxl::read_xlsx()

# view
df_xlsx
# A tibble: 5 × 3
  x         y labels     
  <chr> <dbl> <chr>      
1 a         1 "a \nb \nc"
2 b         1 "b\nc"     
3 c         1 "c\nd"     
4 d         1 "d\ne"     
5 e         1 "e\nf"     
# read rds file
df_rds <- 
    "raw_data/2026-04-26_line-feed.rds" |> 
    readr::read_rds()

# view
df_rds
# A tibble: 5 × 3
  x         y labels     
  <chr> <dbl> <chr>      
1 a         1 "a \nb \nc"
2 b         1 "b\nc"     
3 c         1 "c\nd"     
4 d         1 "d\ne"     
5 e         1 "e\nf"     

看来若df中含有\n,则不能把df存储为tsv格式。

4. 测试换行符在plot的表现

tidyplots |> library()

p1 <- df |> 
    tidyplot(x = x, y = y, paper = "#cceeff", ink = "#994455") |> 
    add_data_points() |> 
    add_title(title = "P1") |> 
    adjust_title(fontsize = 12)

p2 <- p1 |> 
    add_annotation_text(
        text = df_csv$labels[1],
        x = "a", y = 1, fontsize = 14) |> 
    adjust_title(title = "P2: test line feed of df_csv")

p3 <- p2 |> 
    add_annotation_text(
        text = df_xlsx$labels[3],
        x = "c", y = 1, fontsize = 14) |> 
    adjust_title(title = "p3: test line feed of df_xlsx")

p4 <- p3 |> 
    add_annotation_text(
        text = df_rds$labels[4],
        x = "d", y = 1, fontsize = 14) |> 
    adjust_title(title = "P4: test line feed of df_rds")

patchwork::wrap_plots(p1, p2, p3, p4, ncol = 2) |> 
    save_plot("images/2026-04-26_line-feed.png",
        view_plot = FALSE, width = 140, height = 150)

换行符确实在plot中让换行了😁。

给我买杯茶🍵