2017-07-30 585 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 –