rstudio / rmarkdown-cookbook

R Markdown Cookbook. A range of tips and tricks to make better use of R Markdown.
https://bookdown.org/yihui/rmarkdown-cookbook/
578 stars 224 forks source link

Knit to PDFs with different names for several documents #380

Closed simplentrepreneurship closed 1 year ago

simplentrepreneurship commented 1 year ago

Hi all,

I am doing text mining / analysis of several documents (PDF files) that I store in a folder and I want to export the results in a PDF format with different names for each document analyzed. I use RMarkdown which embed the R code for the analysis. So far I do the knit for each document one by one manually, by pressing the knit button in RStudio and it gives me a PDF named the .rmd file.

I really need to automatize the knitting through my .rmd file which can execute the Rcode for several documents stored in a folder one by one (what I have in mind is creating a loop to load the document one by one and execute the Rcode embedded) --> and the output are several PDF files with different name accordingly.

I was thinking to do it by customizing the knit button at YAML and making loop. However I really have difficulties in putting it in my RMarkdown file. Below is part of the Rcode in my RMarkdown : which read "Book_Part_3.pdf". There are Book_Part_4.pdf etc. stored in a folder. What I need is by pressing the knit button only 'once' then the code will read and analyze the Book_Part_xxx.pdf files one by one and produce .PDF files (results of the text analysis) with different name.

Is it possible? I really need some insights. Thank you in advance.


title: "DOCUMENT ANALYSIS REPORT" author: "Simple Entrepreneur" date: "r format(Sys.time(), '%d %B, %Y')" mainfont: Calibri fontsize: 12pt output: pdf_document: latex_engine: xelatex word_document : default html_document : default



knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)

# My Code is here:

library("pdftools")
library(stringr)
library(quanteda)
library(quanteda.textplots)
library(quanteda.textmodels)
library(quanteda.textstats)
library(ggplot2)
library(udpipe)
library(readtext)
library(lattice)
library(tinytex)
library(igraph)
library(ggraph)
library(syuzhet)
library(RColorBrewer)

setwd("C:/R_Analysis")
getwd()

#PDFTOOLS: From PDF to TXT
Book_text <- pdf_text("Book_Part_3.pdf") %>%
  readr::read_lines()
cderv commented 1 year ago

Hi,

For question, you will have more success asking in Q&A website like https://community.rstudio.com/ There is a broader community to answer to you and share experience.

As a generic answer, you can definitely automate rendering of document using R Script. You can also use tools like Makefile if this is something you are confortable with. RStudio IDE can be configure as project using Makefile, and the Build pane will be usingthe Makefile.

I am not sure if I understand correctly, but you should read the part in the cookbook about projects, there are several way to work with multiple R scripts to load in Rmd files, or using child document to reuse content.

You should also have a look at parametrized report as you can then use one Rmd, and use parameter to change on which value it will be applied (like the pdf filename).

I was thinking to do it by customizing the knit button at YAML and making loop.

I would look at solution using R code to automate and not the knit button. You can control it, but it more like a hack. If you have a script that does what you need, it still one button to execute the script. Makefile as a I said is a good solution for project too.

Overall, we can't do this for you, but I believe the Cookbook will give you all the recipies you need to solve this and if this is not the case, please do ask on https://community.rstudio.com/ and share you work - people will be happy to help and share experience!

I'll close this as this is not an issue about the Cookbook, but please do continue the conversation if you need.