Using package flextable, and reading from the different help pages, I could not find how to double (line) space an entire table, as APA requires.
There's the option hrule():
ft_1 <- flextable(head(iris))
ft_1 <- height(ft_1, height = 1, part = "body")
ft_1 <- height(ft_1, height = 1, part = "head")
ft_1 <- hrule(ft_1, rule = "exact", part = "body")
ft_1 <- hrule(ft_1, rule = "exact", part = "head")
ft_1
Or padding():
ft_1 <- flextable(head(iris))
ft_1 <- padding(ft_1, padding = 20, part = "all")
ft_1
But in both cases, when I check in Word, it doesn't correspond to double (line) spacing (it is still single spacing there).
Question
Is there a better option to make sure it 100% corresponds to double line spacing ("Line and Paragraph Spacing" in Word)?
I assume that using hrule() or padding could create various formatting problems later on, or editors might complain it doesn't correspond to the right kind of double spacing (e.g., it doesn't respond to changing spacing manually after so its harder to change through conventional means).
Or are we expected to manually change to double spacing in Word manually after exporting? Thank you.
I have created a new function named line_spacing (flextable >= 0.5.11). It should do what you want:
ft_1 <- qflextable(head(iris))
ft_1 <- line_spacing(ft_1, space = 1.5, part = "all")
ft_1
Related
Once a footnote has been set via add_footnote it seems like its hard to get rid of again.
library(magrittr)
library(huxtable)
jams <- hux(
Type = c("Strawberry", "Raspberry", "Plum"),
Price = c(1.90, 2.10, 1.80)
) %>% add_footnote("Tasty stuff!")
One solution I tried is this:
head(jams, -1)
Unfortunately, the line at the bottom of the huxtable remains. What I would like is a solution which returns a huxtable as if the footnote had never been set.
EDIT:
The code below will also remove the line:
jams <- head(jams, -1)
attributes(jams)$tb_borders$thickness[nrow(attributes(jams)$tb_borders$thickness), ] <- 0
I'm not sure how robust this is though.
EDIT: One issue is that if you use this to remove a footnote which was never set then you remove a line of data.
If you want to get rid of the border, just use the relevant function:
jams <- hux(
Type = c("Strawberry", "Raspberry", "Plum"),
Price = c(1.90, 2.10, 1.80)
) %>% add_footnote("Tasty stuff!")
head(jams, -1) %>% set_bottom_border(final(1), everywhere, 0)
I have a flextable() where I want to make the header have two font sizes. One size for the main text (say, 20 pt font), and a smaller for the parenthetical text (12 pt font). Here's code that makes the entire header 20 pt.
Note: In the final solution, I'd prefer not to make the parenthetical text an entirely new row unless it can be as near to the original text as the below example shows.
library(flextable)
library(dplyr)
set_flextable_defaults(font.size = 20) #Works but makes all header size 20
flextable(test) %>%
set_header_labels(values = list(name = "Name", med_score = "Score (Median)", mean_score = "Score (Mean)")) %>%
align(align = "center", part = "header")
EDIT: Pictures added
Here's what it looks like currently:
And here's what I want it to look like:
Note: I made this goal table in PowerPoint. It looks different in many ways from the original one because I haven't formatted the original. The only difference that I'm trying to replicate is having (median) and (mean) being smaller than Score as well as them being in the same header cell.
You could use compose, as_paragraph and as_chunk to achieve it.
For reference: https://ardata-fr.github.io/flextable-book/cell-content-1.html#multi-content
library(flextable)
ft <- flextable(airquality[ sample.int(10),])
compose(
ft,
j = "Ozone",
part = "header",
value = as_paragraph(
"Ozone ",
as_chunk(
" (Median)",
props = fp_text_default(color = "#006699", font.size = 5)
)
)
)
I want to set the height of row constant or fixed for DT table output. for the table below you can see difference in height of rows.
so the scenario is when the number of character increases for eg second row in this case in first col then height should get adjust as same for all rows.
df <- data.frame(`quote` = c("the trader belongs to","trader have long ralationship withthe same market with my opinion on thaw its implemented mmnnhhh sdfghj fghj kjhgf tyui cvbnm",
"types of trads happens everytime when market slow","trades have leverage with",
"market is continious with the same platform trades"),
`share`= c(43,65,92,23,73),
`times` = c(86,98,55,12,08),
`parts`=c(4,7,4,8,3))
df<-datatable(df,
options = list(columnDefs =
list(list(className = 'dt-center',
targets = "_all"))),rownames = FALSE)
df
like in flextable i can do something like below but looking for fixed solution or any function for DT tables.
(ncols %in% c(4,5)) {
fl<-width(flxtable, width = d*0.3, j = 1)
fl<-width(flxtable, width = (d*0.7)/(ncols-1), j = 2:ncols)
d is left and right margin of docs template
I had success with this in the past:
DT::datatable(df) %>%
DT::formatStyle(names(df),lineHeight='80%')
Kudos to the post that helped me, however I fail to find it back now.
I am trying to use officer to add images to a Word document. I have a whole directory's worth of images that I want to loop through. The problem I am having is that I need to add the image to the document, then add the next image to the newly created document that I just created by adding the last image.
Here's a sample of the code without loops or functions:
library(magrittr)
library(officer)
read_docx() %>% # create document
body_add_img("img1.png", width = 3, height = 4) %>% # add image
body_add_img("img2.png", width = 3, height = 4) %>% # add image
body_add_img("img3.png", width = 3, height = 4) %>% # add image
print(target = "samp.docx") # write document
Using map and lapply doesn't work in this case because every iteration needs to return the object of the previous iteration. I tried writing a function with a for loop but I think I was way off. Any help and pointers would be appreciated.
I think you can use a reduce here. For example using a bit of purrr
library(purrr)
read_docx() %>%
reduce(1:3, function(docx, idx) {
docx %>% body_add_img(paste0("img", idx, ".png"), width = 3, height = 4)
}, .init=.) %>%
print(target = "samp.docx")
The reduce keeps feeding the result back into itself.
I am not sure what was your attempt with the for loop but this simple loop seem to work.
library(officer)
data <- read_docx()
image_list <- paste0('img', 1:3, '.png')
for(i in image_list) {
data <- body_add_img(data, i, width = 3, height = 4)
}
print(data, target = "samp.docx")
When creating a datatable with filter = 'top' and also using a formatting function on a column, the formatting isn't applied to the filter control for that column. Is there a way to format the filter controls as well?
For example, if I have floating-point numbers formatted as a percentage, the slider in the filter still shows floating point numbers.
library(DT)
my_data <- mtcars
my_data$wt_pctile <- trunc(rank(my_data$wt)) / length(my_data$wt)
datatable(my_data,
filter = 'top') %>%
formatPercentage('wt_pctile')
Not sure if there's a way to do it. A workaround can be multiplying by 100:
my_data$wt_pctile <- my_data$wt_pctile * 100
And than displaying as a string with the % sign:
datatable(my_data, filter = 'top') %>%
formatString(suffix = "%",columns = "wt_pctile")
I think Yanir is halfway there! What if you multiplied by 100 and then
datatable(my_data, filter = 'top') %>%
formatCurrency(columns = "wt_pctile",
currency = "%",
before = FALSE)
This prevents double or triple %s when sorting.