Reputation: 443
I have the following problem...
I want to paste this ascii table from R into a reporting software (GE Viewpoint Ultrasound Reporting Software)
pander::pandoc.table(mpg[1:3, 1:3])
The resulting table looks like this...
------------------------------
manufacturer model displ
-------------- ------- -------
audi a4 1.8
audi a4 1.8
audi a4 2
------------------------------
However when I paste into the reporting software, the format appears like this..
I thought that perhaps a workaround would be to copy the text back from the reporting software and re-format the ascii table so that it would look normal when I pasted into the reporting software...
When I copy from the reporting software and paste into R... I get the following
------------------------------
manufacturer model displ
-------------- ------- -------
audi a4 1.8
audi a4 1.8
audi a4 2
------------------------------
If I put in quotes and pass to the console I get the following...
> table
[1] "\n------------------------------\n manufacturer model displ \n--------------
------- -------\n audi a4 1.8 \n\naudi a4 1.8 \n\naudi
a4 2 \n------------------------------\n"
Any ideas on how I could reformat this string of text into a table which will paste correctly into my reporting software??
---UPDATE---
I tried the following code, but I realize now that I am pasting ascii into a variable width font... so this does not work 100%. In fact, it really isn't working at all. Anyone know how to adjust for this? Is there are reference table of pixel width ratios for each character in a variable width font?
library(gt)
library(tidyverse)
library(pander)
raw <- mpg[1:3, 1:3]
text <- pander::pandoc.table.return(raw)
text <- str_replace_all(text, " ", " ")
text <- str_replace_all(text, "-", "--")
num <- str_extract(text, " \n(.*?)\n")
num <- str_replace(num, " \n", "")
num <- str_replace(num, "\n", "")
num <- str_split(num, " ")
num <- nchar(max(num[[1]]))
mat <- str_split(text, "\n")
raw <- rbind(
colnames(raw),
raw
)
new_table <- c()
for (j in 1:dim(raw)[1]){
out <- ""
for (i in 1:length(raw[j, ])){
word_char <- nchar(raw[j, ][i])
word_space <- (num - word_char)
space_before <- floor(word_space)/2
space_after <- ceiling(word_space)/2
new_word <- paste(
paste(rep(" ", space_before), collapse = ""),
raw[j, i],
paste(rep(" ", space_after), collapse = ""),
sep = ""
)
out <- paste(out, new_word, sep = "")
# print(new_word)
# print(word_char)
# print(word_space)
# print(space_before)
# print(space_after)
}
new_table[j] <- out
}
mat[[1]][2]
mat[[1]][2] <- paste(rep("-", ncol(raw)*num), collapse = "")
mat[[1]][length(mat[[1]])-2] <- paste(rep("-", ncol(raw)*num), collapse = "")
num_spaces_to_insert <- nchar(mat[[1]][2]) - nchar(mat[[1]][4])
num_spaces_to_insert <- num_spaces_to_insert/(ncol(raw)-1)*2
mat[[1]][4] <- str_replace_all(mat[[1]][4], " ", paste(rep(" ", num_spaces_to_insert), collapse = ""))
x <- 3
while (x < length(mat[[1]])-2){
# print(mat[[1]][x])
# print(x)
for (i in new_table){
print(x)
print(i)
print(mat[[1]][x])
mat[[1]][x] <- i
print(mat[[1]][x])
x <- x + 2
}
}
mat[[1]] <- paste(mat[[1]], collapse = "\n")
gt::html(mat[[1]])
Here is what the table looks like when I post the result into the reporting software
Upvotes: 0
Views: 45