`str_replace_all()` 在 html 输出上(来自 `huxtable()`)
`str_replace_all()` on html output (from `huxtable()`)
我的 R 代码生成一些 html 输出,我想对其进行两个非常简单的 "find and replace" 类型调整:
- 而不是 html 中的
R2
,我想用 R<sup>2</sup>
替换
- 代替 html 中的
[number] ***
,我想用 [number]<sup>***</sup>
替换,即删除 space 并添加上标。
我一直在尝试用 str_replace_all()
来做到这一点。如果我能在 tidyverse 中解决我的问题那就太好了。
对于可重现的示例,我将使用 mtcars
从 huxtable::huxreg()
生成 html,这与在我的实际问题中生成输出的函数相同。
library(huxtable)
library(tidytext)
fit1 <- lm(mpg ~ disp, data = mtcars)
huxreg(fit1) %>%
quick_html()
给出的输出是 html 版本:
─────────────────────────────────────────────────
(1)
─────────────────────────
(Intercept) 29.600 ***
(1.230)
disp -0.041 ***
(0.005)
─────────────────────────
N 32
R2 0.718
logLik -82.105
AIC 170.209
─────────────────────────────────────────────────
*** p < 0.001; ** p < 0.01; * p < 0.05.
Column names: names, model1
所以我尝试在 R2
和 ***
上 str_replace_all()
,但我的输出似乎没有变化。有没有简单的方法让我进行替换?
huxreg(fit1) %>%
quick_html() %>%
str_replace_all(pattern = "R2", replacement = "R<sup>2</sup>") %>%
str_replace_all(pattern = " ***", replacement = "<sup>***</sup>")
quick_html()
returns NULL
,而不是它生成的 HTML 的文本,它保存到一个文件中(huxtable-output.html
,默认情况下) .您可以读回该文件并在其上使用 运行 正则表达式:
library(huxtable)
library(stringr)
fit1 <- lm(mpg ~ disp, data = mtcars)
filepath <- 'huxtable-output.html'
huxreg(fit1) %>%
quick_html(file = filepath, open = FALSE)
readLines(filepath) %>%
str_replace_all(pattern = "R2", replacement = "R<sup>2</sup>") %>%
str_replace_all(pattern = fixed(" ***"), replacement = "<sup>***</sup>") %>%
writeLines(filepath)
# open file in browser
browseURL(filepath)
或者正如@27ϕ9 在下面的评论中提到的那样,您可以使用 huxtable::to_html()
来避免回读:
huxreg(fit1) %>%
to_html() %>%
str_replace_all(pattern = "R2", replacement = "R<sup>2</sup>") %>%
str_replace_all(pattern = fixed(" ***"), replacement = "<sup>***</sup>") %>%
writeLines(filepath)
Maybe better not to parse HTML with regex, though. Check out rvest and xml2 用于为此目的设计的更强大的工具。
让我们保持简单:
h <- huxreg(fit1)
h[7, 1] <- "R<sup>2</sup>"
escape_contents(h)[7, 1] <- FALSE
h <- map_contents(h, by_regex("***" = "<sup>***</sup>",
.grepl_args = list(fixed = TRUE)))
h <- map_escape_contents(h, by_regex("***" = FALSE,
.grepl_args=list(fixed = TRUE)))
quick_html(h)
我的 R 代码生成一些 html 输出,我想对其进行两个非常简单的 "find and replace" 类型调整:
- 而不是 html 中的
R2
,我想用R<sup>2</sup>
替换
- 代替 html 中的
[number] ***
,我想用[number]<sup>***</sup>
替换,即删除 space 并添加上标。
我一直在尝试用 str_replace_all()
来做到这一点。如果我能在 tidyverse 中解决我的问题那就太好了。
对于可重现的示例,我将使用 mtcars
从 huxtable::huxreg()
生成 html,这与在我的实际问题中生成输出的函数相同。
library(huxtable)
library(tidytext)
fit1 <- lm(mpg ~ disp, data = mtcars)
huxreg(fit1) %>%
quick_html()
给出的输出是 html 版本:
─────────────────────────────────────────────────
(1)
─────────────────────────
(Intercept) 29.600 ***
(1.230)
disp -0.041 ***
(0.005)
─────────────────────────
N 32
R2 0.718
logLik -82.105
AIC 170.209
─────────────────────────────────────────────────
*** p < 0.001; ** p < 0.01; * p < 0.05.
Column names: names, model1
所以我尝试在 R2
和 ***
上 str_replace_all()
,但我的输出似乎没有变化。有没有简单的方法让我进行替换?
huxreg(fit1) %>%
quick_html() %>%
str_replace_all(pattern = "R2", replacement = "R<sup>2</sup>") %>%
str_replace_all(pattern = " ***", replacement = "<sup>***</sup>")
quick_html()
returns NULL
,而不是它生成的 HTML 的文本,它保存到一个文件中(huxtable-output.html
,默认情况下) .您可以读回该文件并在其上使用 运行 正则表达式:
library(huxtable)
library(stringr)
fit1 <- lm(mpg ~ disp, data = mtcars)
filepath <- 'huxtable-output.html'
huxreg(fit1) %>%
quick_html(file = filepath, open = FALSE)
readLines(filepath) %>%
str_replace_all(pattern = "R2", replacement = "R<sup>2</sup>") %>%
str_replace_all(pattern = fixed(" ***"), replacement = "<sup>***</sup>") %>%
writeLines(filepath)
# open file in browser
browseURL(filepath)
或者正如@27ϕ9 在下面的评论中提到的那样,您可以使用 huxtable::to_html()
来避免回读:
huxreg(fit1) %>%
to_html() %>%
str_replace_all(pattern = "R2", replacement = "R<sup>2</sup>") %>%
str_replace_all(pattern = fixed(" ***"), replacement = "<sup>***</sup>") %>%
writeLines(filepath)
Maybe better not to parse HTML with regex, though. Check out rvest and xml2 用于为此目的设计的更强大的工具。
让我们保持简单:
h <- huxreg(fit1)
h[7, 1] <- "R<sup>2</sup>"
escape_contents(h)[7, 1] <- FALSE
h <- map_contents(h, by_regex("***" = "<sup>***</sup>",
.grepl_args = list(fixed = TRUE)))
h <- map_escape_contents(h, by_regex("***" = FALSE,
.grepl_args=list(fixed = TRUE)))
quick_html(h)