Il existe une nouvelle possibilité de prendre une capture d'écran des widgets html pour une implémentation ultérieure, par exemple dans un document pdf (Vous devez télécharger pour ce package: webshot
). La capture d'écran du datatable (package DT
) est prise et utilisée comme image dans . Vous devriez l'essayer, la table est bien formatée.
Voici un exemple de 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))
```
MISE À JOUR
J'ai essayé code complet que vous me avez donné à base de this shiny app example
App Brillant:
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)
```
Et il fonctionne parfaitement me donner la sortie désirée pdf:
documents PDF sont formatés avec LaTeX, vous pourriez avoir plus de chance de demander sur tex.stackexchange, par exemple la question suivante: [écrire l'en-tête d'une colonne sur plusieurs lignes] (https://tex.stackexchange.com/questions/79544/write-column-header-on-multiple-line). –
J'utilise la fonction Latex 'parbox' dans les noms de colonnes afin de contrôler la largeur des colonnes. Quelque chose comme 'names (mydfTable) <- c (" \\ parbox [c] [2.5em] [c] {0.6in} {\\ centring col titre 1 ligne 1 \\\\ col titre 1 ligne2} "," \\ parbox [c] [2.5em] [c] {0.6in} {\\ centering col titre 2 ligne 1 \\\\ col titre ligne2} ")' et ainsi de suite. Cela va forcer le contenu de ceux-ci à envelopper si nécessaire. – lmo