How to write a reproducible as_chunk command in flextable R

88 Views Asked by At

While in the process to create a flextable in R, I'am unable to write a reproducible quote for a section.

This table will be updated every month, therefore adding a new column. To make this reproducible most of my code is in the form on column numbers.

I'm trying to add the image of red_upward and green_downward arrows based on the logic of increase or decrease from the previous month.

The table looks like so:

enter image description here

To be able to achieve this I used the as_paragraph command within compose. And within as_paragraph I put together the value from the column followed by the image of the arrow. The problem is in picking the value of the column. I cannot make the column selection reproducible.

flextable(tab3_data) %>%
  theme_zebra(odd_header = "transparent", even_header = "transparent") %>%
  theme_box() %>%
  align(align = "center", part = "header") %>%
  width(j = c(2:(ncol(tab3_data) - 6)), width = 1.2, unit = "cm") %>% # col. no. deciding the width
  width(j = c((ncol(tab3_data) - 7 + 1):(ncol(tab3_data))), width = 1.9, unit = "cm") %>%
  width(j = ~`Disease Description`, width = 4.6, unit = "cm") %>%
  vline(j = c(1, ((ncol(tab3_data) - 6)), (ncol(tab3_data) - 4), (ncol(tab3_data) - 2)), border = fp_border(color = "black", width = 1.5)) %>% # col. no. deciding the location of thinker vertical lines
  border_outer(border = fp_border(color = "black", width = 1.5)) %>%
  paginate(init = FALSE, hdr_ftr = TRUE) %>%
  compose(
    i = tab3_data[(ncol(tab3_data) - 6)] > tab3_data[(ncol(tab3_data) - 6 - 1)], # col. no. for logic
    j = colnames(tab3_data[(ncol(tab3_data) - 6)]), # col. no. for applying the logic to
    value = as_paragraph(
      as_chunk(
        sprintf("%.0f", `2023_Mar`), # here column number and name are both not working
        fp_text_default(color = "black")
      ), " ",
      as_image(src = red_arrow, width = 0.15, height = 0.25)
    )
  ) %>%
  compose(
    i = tab3_data[(ncol(tab3_data) - 6)] < tab3_data[(ncol(tab3_data) - 6 - 1)],
    j = colnames(tab3_data[(ncol(tab3_data) - 6)]),
    value = as_paragraph(
      as_chunk(
        sprintf("%.0f", `2023_Mar`), # here column number and name are both not working
        fp_text_default(color = "black")
      ), " ",
      as_image(src = green_arrow, width = 0.15, height = 0.25)
    )
  )

2023_Mar is going to change to 2023_Apr and so on....

The options I have tried:

  1. Col. no.--> tab3_data[(ncol(tab3_data)-6)]

Error - Error in sprintf("%.1f", tab3_data[(ncol(tab3_data) - 6)]) : 'list' object cannot be coerced to type 'double'

  1. Using the col name --> colnames(tab3_data[ncol(tab3_data)-6])

Error in sprintf("%.1f", colnames(tab3_data[ncol(tab3_data) - 2])) : invalid format '%.1f'; use format %s for character objects

so I changed the format to %s. but then it prints the column name and not the value in that cell.

I'm expecting the output as in the image, but by using reproducible code and not hard coding the column name.

0

There are 0 best solutions below