rmarkdown 中的表格?
Tables in rmarkdown?
我的结果是来自 R 代码的 table。当我在 R 中查看(结果)时,我会得到一个漂亮的 table,例如:
然后我将我的代码传输到闪亮的应用程序,带有下载选项。我在 Rmarkdown 中找不到合适的命令来正确淹没我的 table。我已经尝试了每一个包,比如 xtable :
---
title: "All pages landscape"
output: pdf_document
classoption: landscape
---
```{r results = "asis", echo=FALSE}
x.side <- xtable:: xtable(ali1(), caption = "A sideways table",align=c("rp{2cm}p{0.7cm}p{0.7cm}p{1cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}"))
print(x.side, floating = TRUE,type="latex")
```
不使用 align
它看起来像:
和align
(我试图显示所有列):
除此之外,当我尝试使用 rotate.colnames=TRUE
时出现错误:
Error : pandoc document conversion failed with error 43
我的目标是 table 合二为一!我找不到修复列宽和将行拆分为多行的命令!
非常感谢任何想法!
现在可以截取 html 小部件的屏幕截图以进一步实现,例如 pdf 文档(您需要下载该包:webshot
)。数据table(DT
包)的屏幕截图被用作rmarkdown
中的图像。您应该尝试一下,table 的格式很好。
这是一个示例代码:
---
output:
pdf_document:
toc: yes
---
```{r, fig.align='center', fig.pos='htb!', echo=FALSE, cache=FALSE, warning = FALSE, message = FALSE, tidy=TRUE}
library(DT)
library(webshot)
datatable(mtcars[1:15,],rownames=FALSE, options = list(dom='t',ordering=F))
```
更新
我已经尝试了您在 this shiny app example
基础上给我的完整代码
闪亮的应用程序:
library(shiny)
library(rmarkdown)
library(knitr)
shinyApp(
ui = fluidPage(
title = 'Download a PDF report',
sidebarLayout(
sidebarPanel(
helpText(),
selectInput('x', 'Build a regression model of mpg against:',
choices = names(mtcars)[-1]),
radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
inline = TRUE),
downloadButton('downloadReport')
),
mainPanel(
plotOutput('regPlot')
)
)
),
server = function(input, output) {
data <- reactive({mtcars[ ,input$x, drop=FALSE]})
regFormula <- reactive({
as.formula(paste('mpg ~', input$x))
})
output$regPlot <- renderPlot({
par(mar = c(4, 4, .1, .1))
plot(regFormula(), data = mtcars, pch = 19)
})
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},
content = function(file) {
src <- normalizePath('report_file.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report_file.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report_file.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()
))
file.rename(out, file)
}
)
}
)
report_file.Rmd:
Here is my regression model:
```{r model, collapse=TRUE}
options(digits = 4)
fit <- lm(regFormula(), data = mtcars)
b <- coef(fit)
summary(fit)
```
```{r, fig.align='center', fig.pos='htb!', echo=FALSE, cache=FALSE, warning = FALSE, message = FALSE, tidy=TRUE}
library(DT)
library(webshot)
datatable(data(),rownames=FALSE, options = list(dom='t',ordering=F))
```
The fitting result is $mpg = `r b[1]` + `r b[2]``r input$x`$.
Below is a scatter plot with the regression line.
```{r plot, fig.height=5}
par(mar = c(4, 4, 1, 1))
plot(regFormula(), data = mtcars, pch = 19, col = 'gray')
abline(fit, col = 'red', lwd = 2)
```
它工作得很好,给了我想要的 pdf 输出:
我的结果是来自 R 代码的 table。当我在 R 中查看(结果)时,我会得到一个漂亮的 table,例如:
---
title: "All pages landscape"
output: pdf_document
classoption: landscape
---
```{r results = "asis", echo=FALSE}
x.side <- xtable:: xtable(ali1(), caption = "A sideways table",align=c("rp{2cm}p{0.7cm}p{0.7cm}p{1cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}p{0.5cm}"))
print(x.side, floating = TRUE,type="latex")
```
不使用 align
它看起来像:
和align
(我试图显示所有列):
除此之外,当我尝试使用 rotate.colnames=TRUE
时出现错误:
Error : pandoc document conversion failed with error 43
我的目标是 table 合二为一!我找不到修复列宽和将行拆分为多行的命令!
非常感谢任何想法!
现在可以截取 html 小部件的屏幕截图以进一步实现,例如 pdf 文档(您需要下载该包:webshot
)。数据table(DT
包)的屏幕截图被用作rmarkdown
中的图像。您应该尝试一下,table 的格式很好。
这是一个示例代码:
---
output:
pdf_document:
toc: yes
---
```{r, fig.align='center', fig.pos='htb!', echo=FALSE, cache=FALSE, warning = FALSE, message = FALSE, tidy=TRUE}
library(DT)
library(webshot)
datatable(mtcars[1:15,],rownames=FALSE, options = list(dom='t',ordering=F))
```
更新
我已经尝试了您在 this shiny app example
基础上给我的完整代码闪亮的应用程序:
library(shiny)
library(rmarkdown)
library(knitr)
shinyApp(
ui = fluidPage(
title = 'Download a PDF report',
sidebarLayout(
sidebarPanel(
helpText(),
selectInput('x', 'Build a regression model of mpg against:',
choices = names(mtcars)[-1]),
radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
inline = TRUE),
downloadButton('downloadReport')
),
mainPanel(
plotOutput('regPlot')
)
)
),
server = function(input, output) {
data <- reactive({mtcars[ ,input$x, drop=FALSE]})
regFormula <- reactive({
as.formula(paste('mpg ~', input$x))
})
output$regPlot <- renderPlot({
par(mar = c(4, 4, .1, .1))
plot(regFormula(), data = mtcars, pch = 19)
})
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},
content = function(file) {
src <- normalizePath('report_file.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report_file.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report_file.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()
))
file.rename(out, file)
}
)
}
)
report_file.Rmd:
Here is my regression model:
```{r model, collapse=TRUE}
options(digits = 4)
fit <- lm(regFormula(), data = mtcars)
b <- coef(fit)
summary(fit)
```
```{r, fig.align='center', fig.pos='htb!', echo=FALSE, cache=FALSE, warning = FALSE, message = FALSE, tidy=TRUE}
library(DT)
library(webshot)
datatable(data(),rownames=FALSE, options = list(dom='t',ordering=F))
```
The fitting result is $mpg = `r b[1]` + `r b[2]``r input$x`$.
Below is a scatter plot with the regression line.
```{r plot, fig.height=5}
par(mar = c(4, 4, 1, 1))
plot(regFormula(), data = mtcars, pch = 19, col = 'gray')
abline(fit, col = 'red', lwd = 2)
```
它工作得很好,给了我想要的 pdf 输出: