I have my results as a table from R code. When I View(results) in R, I would get a pretty table like :
Then I transfer my code to shiny app, with download option. I could not find a proper command in Rmarkdown to drown my table properly. I have tried every single packages, like 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")
```
Without using align
its look like :
and with align
(I have tried to show all columns ) :
Beside that when I tried to use the rotate.colnames=TRUE
I have got the error :
Error : pandoc document conversion failed with error 43
My aim is to have the table in a single piece! I was not able to find a command that fix the column's width and break the rows to have multi lines!
Any Idea is highly appreciated!
There is a new possibility of taking a screenshot of html widgets for further implementation, in for example pdf document (You need to download for that package: webshot
). The screenshot of the datatable (DT
package) is taken and used as an image in rmarkdown
. You should try it out, the table is nicely formatted.
Here is a sample code:
---
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
I have tried full code which You have gave me on base of this shiny app example
Shiny App:
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)
```
And it is working perfectly giving me desired pdf output: