1

I have a sample dataframe with values:

data <- structure(list(A = c("Date)", "Values"), B = c("2023-04-03", "Heat Capacity\nSpecific Heat Capacity\nHeat Index" )), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame" ))

Here the 2nd row of last column extends to the right. I want the each values to be in new line without adding new rows.

My desired output:

enter image description here

The above table is created using gt():

library(gt)
data %>%   
mutate(b = str_replace_all(b, "\n", "<br>")) %>%    
gt() %>%    
fmt_markdown(columns = TRUE)

But this is not useful as my final desired output I want is in xlsx file and using gt() wouldn't work. Is it possible to do anything with dplyr and stringr to achieve this similar result

Golem
  • 100
  • 9
  • so what specifically are you wanting? Because I took your example input, exported it using writexl, and importing it into Google Sheets, my spreadsheet looks identical to the gt output you wanted – Mark Aug 05 '23 at 04:28
  • Can you show your code – Golem Aug 05 '23 at 10:38
  • @Mark - But when I output it using `writexl`, its similar to the dataframe – Golem Aug 05 '23 at 10:44
  • so the process is essentially 1. create the dataframe 2. export the dataframe 3. import the dataframe into Excel/Google Sheets. One or all of these could be not working correctly. Going backwards through these steps backwards is the best bet for debugging this – Mark Aug 05 '23 at 11:46
  • Okay, so debugging step #3, I uploaded my own output to Google Drive. Can you download it and open it and tell me if it gives you the correct output? https://docs.google.com/spreadsheets/d/17S1ng9jmWGpu5kdxaESQta5s7yQ89b41/edit?usp=sharing&ouid=107688141380082141299&rtpof=true&sd=true – Mark Aug 05 '23 at 11:47
  • @Mark Yes I can download it and the output is correct – Golem Aug 05 '23 at 12:53
  • https://github.com/rstudio/gt/issues/240#top I think `gt` tables to `xlsx` file is still in development – Golem Aug 05 '23 at 12:55
  • okay now try `structure(list(A = c("Date)", "Values"), B = c("2023-04-03", "Heat Capacity\nSpecific Heat Capacity\nHeat Index" )), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame" )) |> writexl::write_xlsx(path = "test.xlsx")`, then take the test.xlsx which is created, and open that, and tell me if that is rendered correctly – Mark Aug 05 '23 at 23:42
  • @Mark Nope didnt work – Golem Aug 05 '23 at 23:57
  • 1
    hmm that's really odd!! Because that is literally what I ran on mine to create the .xlsx file you opened – Mark Aug 06 '23 at 01:45
  • I think the problem I'm facing is due to how the reading the xlsx (Excel) file. When I attempted to read the file and display the output, it returned a lengthy row of values. This output matches what is shown in the sample dataframe above. `read_xlsx("file_name.xlsx", sheet = 1)` – Golem Aug 06 '23 at 13:12
  • @Mark I think the problem was with my Microsft Excel. When I try to open the excel in google sheets it showed me the correct format – Golem Aug 09 '23 at 10:37
  • 1
    odd! Unsure if this is a feature difference between Sheets and Excel, if something is wrong with your Excel, or it needs to be updated or what – Mark Aug 09 '23 at 14:04

1 Answers1

1

You can use package gt to format presentation quality tables.
See this introduction to gt.

Here is the code, output omitted.

suppressPackageStartupMessages({
  library(tidyverse)
  library(gt)
})

data <- structure(list(
  A = c("Date)", "Values"), 
  B = c("2023-04-03", "Heat Capacity\nSpecific Heat Capacity\nHeat Index" )), 
  row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame" ))

data %>%
  mutate(B = gsub("\\n", "<br>", B)) %>%
  gt() %>%
  fmt_markdown(columns = everything())

Created on 2023-08-05 with reprex v2.0.2


Edit

You can export to a HTML file and open the file in Excel:

data %>%
  mutate(B = gsub("\\n", "<br>", B)) %>%
  gt() %>%
  fmt_markdown(columns = everything()) %>%
  as_raw_html() %>%
  writeLines(con = "~/Temp/so.html")
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • From here, How do you convert it into Excel file. I tried write_xlsx() but it gave an error – Golem Aug 05 '23 at 10:58
  • @Golem See if the edit solves it. – Rui Barradas Aug 05 '23 at 11:06
  • I'm sorry, but I need this in an XLSX file because the main logic resides within a Shiny app. This app takes an Excel file, manipulates it, and then returns the manipulated XLSX file – Golem Aug 05 '23 at 13:00