2017-07-30 582 views
1

我有我的结果作为从R代码表。当我查看(结果)在R,我会得到一个漂亮的表,如: enter image description here 然后,我将我的代码转移到闪亮的应用程序,并有下载选项。我无法在Rmarkdown中找到正确的命令来正确淹没我的表。我曾尝试每一个包,就像xtable:rmarkdown中的表格?

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

我的目标是让表中单件!我无法找到修复列宽的命令,并将行分成多行!

任何想法高度赞赏!

+0

PDF文档的格式与LaTeX的,你可能有更多的运气询问关于tex.stackexchange,例如这个问题: [在多行上写列标题](https://tex.stackexchange.com/questions/79544/write-column-header-on-multiple-line)。 –

+0

我在列名中使用Latex'parbox'函数来控制列的宽度。类似于'names(mydfTable)< - c(“\\ parbox [c] [2.5em] [c] {0.6in} {\\ centering col title 1 line 1 \\\\ col col title 1 line2}”, \\ parbox [c] [2.5em] [c] {0.6in} {\\ centering col title 2 line 1 \\\\ col title title line2}“)'等等。这将迫使这些内容在必要时进行包装。 – lmo

回答

1

有服用HTML控件的截图进一步实施,例如PDF文档的一种新的可能性(您需要下载该软件包:webshot)。数据表(DT包)的屏幕截图在中作为图像使用。你应该尝试一下,表格格式很好。

这里是一个示例代码:

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

UPDATE

我已经试过你已经给了我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

+0

谢谢您的回答,但我完全复制了您的代码,并且出现以下错误:错误:在文档中定位胶乳输出时生成HTML输出的函数。 请将此文档的输出类型更改为HTML。另外,您也可以允许在非HTML格式 HTML输出,加入这个选项 您rmarkdown文件的YAML前事: always_allow_html:是 然而要注意的HTML输出将不会在非HTML可见格式。 –

+0

我试过了我发布的代码,它工作正常,请查看我添加的屏幕截图...您是否将此文档保存为'.Rmd'并针织它?你下载了'webshot'和'DT'包吗?如果是,你有什么版本? –

+0

我已经将它保存为.Rmd,并且我安装了两个软件包:webshot_0.4.1和DT_0.2 –