【问题标题】:Tables in rmarkdown?rmarkdown 中的表格?
【发布时间】:2017-07-30 10:22:02
【问题描述】:

我的结果是来自 R 代码的表格。当我在 R 中查看(结果)时,我会得到一个漂亮的表格,例如: 然后我将我的代码转移到闪亮的应用程序,带有下载选项。我在 Rmarkdown 中找不到正确的命令来正确淹没我的桌子。我已经尝试过每一个包,比如 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 时,我遇到了错误:

错误:pandoc 文档转换失败,错误 43

我的目标是把桌子做成一体!我找不到修复列宽并将行拆分为多行的命令!

非常感谢任何想法!

【问题讨论】:

  • PDF 文档是用 LaTeX 格式化的,你可能会更幸运地在 tex.stackexchange 上提问,例如这个问题:write column header on multiple line
  • 我在列名中使用 Latex parbox 函数来控制列的宽度。诸如names(mydfTable) &lt;- c("\\parbox[c][2.5em][c]{0.6in}{\\centering col title 1 line 1\\\\col title 1 line2}", "\\parbox[c][2.5em][c]{0.6in}{\\centering col title 2 line 1\\\\col title line2}") 之类的东西。如有必要,这将强制其中的内容换行。

标签: r shiny markdown xtable


【解决方案1】:

有一种新的可能性可以截取 html 小部件的屏幕截图以供进一步实施,例如 pdf 文档(您需要下载该软件包:webshot)。获取数据表(DT 包)的屏幕截图并用作rmarkdown 中的图像。您应该尝试一下,表格的格式很好。

这是一个示例代码:

---
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 输出:

【讨论】:

  • 感谢您的回答,但我完全复制了您的代码,但出现以下错误:错误:在针对乳胶输出的文档中找到产生 HTML 输出的函数。请将此文档的输出类型更改为 HTML。或者,您可以通过将此选项添加到 rmarkdown 文件的 YAML 前面部分来允许以非 HTML 格式输出 HTML:always_allow_html: yes 但是请注意,HTML 输出在非 HTML 格式中将不可见。
  • 我已经尝试了我发布的代码并且效果很好,请查看我添加的屏幕截图...您是否将此文档保存为.Rmd 并编织它?您是否下载了 webshotDT 软件包?如果是,您有哪些版本?
  • 我已将其保存为 .Rmd,是的,我安装了两个软件包:webshot_0.4.1 和 DT_0.2
  • 你有什么版本的DTwebshot
  • 当knitr检测到代码块中的HTML widget对象时,要么在当前输出格式为HTML时正常渲染widget,要么将widget保存为HTML页面并调用webshot进行抓取输出格式不是HTML时HTML页面的画面:来源:bookdown.org/yihui/bookdown/html-widgets.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 2014-10-08
  • 2021-10-15
相关资源
最近更新 更多