Documentos dinâmicos

ReporteRs

Prof. Walmes Zeviani

2018-03-22

Justificativa:

Objetivos:

ReporteRs

Ficha técnica

ReporteRs is an R package for creating MS Word and MS Powerpoint documents.

Uso do pacote

install.packages(pkgs = "ReporteRs",
                 dependencies = TRUE,
                 repos = "http://cran-r.c3sl.ufpr.br/")
# Carrega o pacote.
library(ReporteRs)
## Loading required package: ReporteRsjars
ls("package:ReporteRs")
##  [1] "addCodeBlock"                 "addColumnBreak"              
##  [3] "addDate"                      "addDocument"                 
##  [5] "addFlexTable"                 "addFooter"                   
##  [7] "addFooterRow"                 "addHeaderRow"                
##  [9] "addImage"                     "addPageBreak"                
## [11] "addPageNumber"                "addParagraph"                
## [13] "addPlot"                      "add.pot"                     
## [15] "addRScript"                   "addSection"                  
## [17] "addSlide"                     "addSubtitle"                 
## [19] "addTitle"                     "addTOC"                      
## [21] "as.FlexTable"                 "as.html"                     
## [23] "borderDashed"                 "borderDotted"                
## [25] "borderNone"                   "borderProperties"            
## [27] "borderSolid"                  "cellBorderBottom"            
## [29] "cellBorderNone"               "cellBorderTB"                
## [31] "cellBorderTop"                "cellProperties"              
## [33] "chprop"                       "CodeBlock"                   
## [35] "colorProperties"              "deleteBookmark"              
## [37] "deleteBookmarkNextContent"    "docx"                        
## [39] "FlexCell"                     "FlexRow"                     
## [41] "FlexTable"                    "Footnote"                    
## [43] "is.color"                     "light.table"                 
## [45] "list_bookmarks"               "map_title"                   
## [47] "office_web_viewer"            "parCenter"                   
## [49] "parJustify"                   "parLeft"                     
## [51] "parProperties"                "parRight"                    
## [53] "pot"                          "pot_img"                     
## [55] "pptx"                         "renderFlexTable"             
## [57] "RScript"                      "setColumnsColors"            
## [59] "setFlexTableBackgroundColors" "setFlexTableBorders"         
## [61] "setFlexTableWidths"           "set_of_paragraphs"           
## [63] "setRowsColors"                "setZebraStyle"               
## [65] "slide.layouts"                "spanFlexTableColumns"        
## [67] "spanFlexTableRows"            "styles"                      
## [69] "textBold"                     "textBoldItalic"              
## [71] "text_extract"                 "textItalic"                  
## [73] "textNormal"                   "textProperties"              
## [75] "toc.options"                  "vanilla.table"               
## [77] "writeDoc"
# Templates disponíveis.
fl <- dir(system.file("templates", package = "ReporteRs"),
          full.names = TRUE)
fl
## [1] "/usr/lib/R/site-library/ReporteRs/templates/bookmark_example.docx"
## [2] "/usr/lib/R/site-library/ReporteRs/templates/EMPTY_DOC.docx"       
## [3] "/usr/lib/R/site-library/ReporteRs/templates/EMPTY_DOC.pptx"
# Copia para seu diretório de trabalho para inspecionar.
file.copy(from = fl[2], to = getwd())
## [1] TRUE
# Começa um documento.
doc <- docx(title = "Meu documento")

# Propriedades.
str(doc)
## List of 6
##  $ obj          :Formal class 'jobjRef' [package "rJava"] with 2 slots
##   .. ..@ jobj  :<externalptr> 
##   .. ..@ jclass: chr "org/lysis/reporters/docs/docx4R"
##  $ title        : chr "Meu documento"
##  $ basefile     : chr "/usr/lib/R/site-library/ReporteRs/templates/EMPTY_DOC.docx"
##  $ plot_first_id: int 1
##  $ styles       : Named chr [1:27] "Normal" "Titre1" "Titre2" "Titre3" ...
##   ..- attr(*, "names")= chr [1:27] "Normal" "heading 1" "heading 2" "heading 3" ...
##  $ header.styles: Named chr [1:9] "Titre1" "Titre2" "Titre3" "Titre4" ...
##   ..- attr(*, "names")= chr [1:9] "heading 1" "heading 2" "heading 3" "heading 4" ...
##  - attr(*, "class")= chr "docx"
# Adiciona título ao documento.
doc <- addTitle(doc = doc,
                value = "As primeiras 5 linhas do dataset iris",
                level = 1)

# Insere uma tabela no documento.
doc <- addFlexTable(doc = doc,
                    flextable = vanilla.table(iris[1:5, 1:2]))

# Adiciona outro título.
doc <- addTitle(doc = doc,
                value = "Um gráfico",
                level = 1)

library(lattice)

# Adiciona um gráfico.
myplot <- xyplot(dist ~ speed, data = cars)
doc <- addPlot(doc = doc,
               fun = print,
               x = myplot)

# Mais um título
doc <- addTitle(doc = doc,
                value = "Exemplo de texto",
                level = 1)

# Adiciona um parágrafo.
doc <- addParagraph(doc = doc,
                    value =
                        "Overall, SAS is about 11 years behind R and S-Plus in statistical capabilities (last year it was about 10 years behind) in my estimation. -- Frank Harrell (SAS User, 1969-1991), R-help (September 2003).",
                    stylename = "Normal")

# Escreve o arquivo em diretório temporário.
# filename <- tempfile(fileext = ".docx")
writeDoc(doc = doc, file = "meudoc.docx")

Orientações e precauções

Outros recursos

Para mais opções, visite a Task View de pesquisa reproduzível: https://cran.r-project.org/view=ReproducibleResearch.

Próxima aula