rmarkdown中的表格?

时间:2017-07-30 10:22:02

标签: r shiny markdown xtable

我将结果作为R代码表。当我在R中查看(结果)时,我会得到一个漂亮的表: link 然后我将我的代码转移到闪亮的应用程序,下载选项。我在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,它的外观如下:

enter image description here

align(我试图显示所有列):

enter image description here

除此之外,当我尝试使用rotate.colnames=TRUE时,我收到了错误:

  

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

我的目标是将桌子放在一块!我无法找到修复列宽度的命令,并将行分成多行!

任何想法都非常感谢!

1 个答案:

答案 0 :(得分: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输出:

enter image description here