Kristina
Kristina

Reputation: 91

Error in x$data[i, j] <- value : number of items to replace is not a multiple of replacement length

I just upgraded to R-3.6.0, and suddenly get this error when turning a matrix into a dataframe, while creating a flextable. I have never experienced this error with the code before, maybe someone can see what went wrong?

So this is one example:

doc201 <- read_docx(path = "Style.docx")
Header <- matrix("",1,3)
Header[1,1] <- paste("Blabla ")
Header[1,2] <- paste("Version:", "1")
Header[1,3] <- format(Sys.Date(), format="%d-%m-%Y")

Head <- flextable(as.data.frame(Header), cwidth = c(3.2,1,2.5))
Head <- delete_part(x = Head, part = "header")
Head <- align(Head, i = 1, j = 1, align = "left") 
Head <- fontsize(Head, size = 11, part = "body")
Head <- bold(Head, bold = TRUE, part = "body")
Head <- font(Head, i = 1, j = (1:3), "Times New Roman", part = "body")

doc201 <- body_add_flextable(doc201, value = Head, pos = "before")

I noticed the error when adding to my doc, but i can see that it already occurs at

Head <- flextable(as.data.frame(Header), cwidth = c(3.2,1,2.5))
Head

My code hasn't changed, and i require() the same packages as before - i have checked that they are all installed. But maybe i have deleted something R related on accident. I just cant figure out what is missing.

Upvotes: 0

Views: 436

Answers (1)

David Gohel
David Gohel

Reputation: 10695

I am not sure it comes from R 3.6.0. However, you can get what you want by using width(). You can also use your code as is but you will have to update flextable to a version > 0.5.2.

library(flextable)
Header <- matrix("",1,3)
Header[1,1] <- paste("Blabla ")
Header[1,2] <- paste("Version:", "1")
Header[1,3] <- format(Sys.Date(), format="%d-%m-%Y")

# you can use one of the following two solutions ----

# solution 1, require flextable > 0.5.2
Head <- flextable(as.data.frame(Header), cwidth = c(3.2,1,2.5))

# solution 2
Head <- flextable(as.data.frame(Header))
Head <- width(Head, width = c(3.2,1,2.5))


# below code is the same than before
Head <- delete_part(x = Head, part = "header")
Head <- align(Head, i = 1, j = 1, align = "left") 
Head <- fontsize(Head, size = 11, part = "body")
Head <- bold(Head, bold = TRUE, part = "body")
Head <- font(Head, i = 1, j = (1:3), "Times New Roman", part = "body")

result

Upvotes: 1

Related Questions