Fig.align and kable_styling don't work when rendering .Rmd file from R script


#1

I am currently working on a whale catalogue which we use to identify certain individuals to conduct research on these animals. The reports include tables, ggplots and photographs.
I wrote a markdown script to create these reports as .pdfs. I spend quite some time to finalize the layout, positions etc. as this is the first time I am using markdown.

However, I was happy with the single reports. But as there are hundreds of those report-pdfs which need to be created, I want to run an R script with a for loop to create all those reports automatically.

When I use

for (whale in all_data$ID){
     rmarkdown::render(input = "C:/Users/NK/Desktop/research_elding/minke_whale_catalogue/minke_whale_catalogue_final_test1/file_2.Rmd",
            output_format = "pdf_document",
            output_file = paste("test_report_", whale, ".pdf", sep=''),
            output_dir = "C:/Users/NK/Desktop/research_elding/minke_whale_catalogue/minke_whale_catalogue_final_test1/reports",
            clean=TRUE)

}

in my R script, I get different kinds of errors and the script stopps. I guess I am somehow missing an option to tell the r script that the styling of the .Rmd file needs to be used.

I use MikTex with the LaTex engine pdflatex.

I tracked the problems down to the alignment settings fig.align, fig.height, fig.width and kable and kable_styling.

an example:
When I change

kable(ins,format="latex",booktabs=TRUE,align="c") %>%
kable_styling(full_width = T,font_size = 20)

to

kable(ins)

in the Rmd-file the r-script works fine but off course my format, alignment etc. is gone.

Same is true for my ggplots:

an example:

When I change

h_map <- ggplot(date_f, aes(year,month)) +
  geom_tile(aes(fill=Sightings), colour="white") + scale_fill_gradient(low="white",high="black") +
  labs(x="Year", y="Month") + #change axis labels
  theme(axis.ticks=element_blank()) + #remove ticks
  theme(axis.text=element_text(size=13)) #change axis title size

h_map

to

h_map <- ggplot(date_f, aes(year,month)) +
  geom_tile(aes(fill=Sightings), colour="white") + scale_fill_gradient(low="white",high="black") +
  labs(x="Year", y="Month") + #change axis labels
  theme(axis.ticks=element_blank()) + #remove ticks
  theme(axis.text=element_text(size=13)) #change axis title size

h_map

in the Rmd-file the r-script works but the formats of the plot are gone and it looks really shit.

I appreciate any help!