Explanation of the Code
We first start by Installing R. This can be done here:
Next we Install R-studio
This can be done here:
The next step is to install the R-Packages. They are flextable, Officr and dplyr
install.packages("flextable")
install.packages("officer")
install.packages("dplyr")
Before reading the code Here are some helpful resources which i reccomend you go over first:
%>% Pipes 2.Officer
Manual 3.Flextable
Manual 4.Flextable
GalleryBefore beginning to run the code you need to set up your working directory.Below is an example on how to do this :
getwd()
## [1] "C:/Users/Awais/Documents/Sohrab Worksheet/Worksheets"
This will get your current directory.
Now you can change it as per your liking:
setwd("C:/Users/Awais/Documents/Sohrab Worksheet")
Once the directory is set you can save your template on it.
Before running the code it is necessary to crete a template on which
the word file will be written. (Section 3.1 in the officer Manual) I
have sent you the one i used in the code.
"example_2.docx"
Here is a video which helped me in making the template along with additional resources which might be helpful: 1. Create Templates in Word 2.Word styles
library(officer)
library(flextable)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
name <- read.csv("name.csv")
questions <- read.csv("Math_Questions_CSV.csv")
set_flextable_defaults(
font.color = "black",
border.color = "black",
theme_fun = "theme_box",
padding.top = 0,
padding.left = 0)
title <- "Addition of 2 Digit Numbers"
name$X1[1] <- title
questions <- flextable(questions, cwidth = 3.18, cheight = 0.83) %>%
delete_part(part = "header")
In this part of the code the template titled
"example_2.docx" is added. In the second line the
body_add_table() function adds a custom table which i saved
in the word template called "Awais1" Finally we add the
flextable
doc_2 <- read_docx(path = "example_2.docx") %>%
body_add_table(name, style = "Awais1",header = FALSE, align_table = "center",
alignment = c("l","r")) %>%
body_add_par("", style = "Normal") %>%
body_add_flextable(questions)
print(doc_2, target = "Sohrab bhai Worksheet.docx")
Below is the Entire code incase you wish to copy it.
library(officer)
library(flextable)
library(dplyr)
set_flextable_defaults(
font.color = "black",
border.color = "black",
padding.top = 0,
padding.left = 0,
theme_fun = "theme_box" )
name <- read.csv("name.csv")
questions <- read.csv("Math_Questions_CSV.csv")
title <- "Addition of 2 Digit Numbers"
name$X1[1] <- title
questions <- flextable(questions, cwidth = 3.18, cheight = 0.83) %>%
delete_part(part = "header")
doc_2 <- read_docx(path = "example_2.docx") %>%
body_add_table(name, style = "Awais1",header = FALSE, align_table = "center",
alignment = c("l","r")) %>%
body_add_par("", style = "Normal") %>%
body_add_flextable(questions)
print(doc_2, target = "Sohrab bhai Worksheet.docx")