与pander的R重新格式化ASCII表

发布于 2025-01-21 09:10:27 字数 4067 浏览 3 评论 0原文

我有以下问题...

我想将此ASCII表从R粘贴到报告软件(GE Viewpoint超声报告软件)中,

pander::pandoc.table(mpg[1:3, 1:3])

结果表看起来像这样...

------------------------------
 manufacturer   model   displ 
-------------- ------- -------
     audi        a4      1.8  

     audi        a4      1.8  

     audi        a4       2   
------------------------------

但是,当我粘贴到报告软件中时,该格式看起来像这个..

“不好的表”

我认为也许解决方法是从报告软件中复制文本并重新格式化ASCII表,以便当我粘贴到报告软件中时看起来正常...

当我从报告软件中复制并粘贴到R ...

------------------------------
          manufacturer   model   displ 
-------------- ------- -------
          audi        a4      1.8  

audi        a4      1.8  

audi        a4       2   
------------------------------

如果我输入报价并将其传递给控制台,我会得到以下内容

> table
[1] "\n------------------------------\n          manufacturer   model   displ \n-------------- 
------- -------\n          audi        a4      1.8  \n\naudi        a4      1.8  \n\naudi        
a4       2   \n------------------------------\n"

。粘贴会正确地粘贴到我的报告软件中吗?

---更新---

我尝试了以下代码,但是现在我意识到我将ASCII粘贴到了可变宽度字体中...因此,这无法100%起作用。实际上,它确实根本无法使用。有人知道如何调整吗?是否有可变宽度字体中每个字符的像素宽度比的参考表?

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

中时表的样子,

这是我将结果发布到报告软件

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..

bad looking table

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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文