Search code examples
rflextable

Why am I getting invalid values when using flextable's tabulator() function?


I am new to R and using its library. I am trying to make a table where the headers are in the values of the columns itself and am using flextable.

Here is my goal (mock-up) where there are multiple header levels: enter image description here

Here is the documentation on flextable's tabulator: https://ardata-fr.github.io/flextable-book/tabulation.html

Here is my attempt (example data):

library(flextable)
library(dplyr)

df <- data.frame(question = c("Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction"),
                 column_one = c("Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Region", "Region", "Region", "Region"),
                 column_two = c("Small", "Medium", "Large", "Small", "Medium", "Large", "West", "East", "West", "East"),
                 row = c("Pizza", "Pizza", "Pizza", "Hamburger", "Hamburger", "Hamburger", "Pizza", "Pizza", "Hamburger", "Hamburger"),
                 percent = c(0.83679353, 0.341159874, 0.084278807, 0.071733007, 0.363397833, 0.405083242, 0.094760815, 0.762002269, 0.782725923, 0.657853129),
                 standardError = c(0.824884898, 0.69073481, 0.664255159, 0.099570678, 0.735837865, 0.940523571, 0.933454698, 0.669219927, 0.669924278, 0.537262473))

cft <- tabulator(
  x = dat,
  rows = "row",
  columns = c("question", "column_one","column_two"),
  `P` = as_paragraph(percent),
  `SE` = as_paragraph(standardError)
)

ft <- as_flextable(cft)
ft

I am getting:

Error in melt.data.table(as.data.table(dat), id.vars = c(columns, rows), : One or more values in 'id.vars' is invalid.


Solution

  • You are not using the table df you created but another named dat that does not contain the column named row.

    Here is probably what you wanted to write:

    library(flextable)
    library(dplyr)
    
    dat <- data.frame(question = c("Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction", "Food Satisfaction"),
                     column_one = c("Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Portion Size", "Region", "Region", "Region", "Region"),
                     column_two = c("Small", "Medium", "Large", "Small", "Medium", "Large", "West", "East", "West", "East"),
                     row = c("Pizza", "Pizza", "Pizza", "Hamburger", "Hamburger", "Hamburger", "Pizza", "Pizza", "Hamburger", "Hamburger"),
                     percent = c(0.83679353, 0.341159874, 0.084278807, 0.071733007, 0.363397833, 0.405083242, 0.094760815, 0.762002269, 0.782725923, 0.657853129),
                     standardError = c(0.824884898, 0.69073481, 0.664255159, 0.099570678, 0.735837865, 0.940523571, 0.933454698, 0.669219927, 0.669924278, 0.537262473))
    
    cft <- tabulator(
      x = dat,
      rows = "row",
      columns = c("question", "column_one","column_two"),
      `P` = as_paragraph(percent),
      `SE` = as_paragraph(standardError)
    )
    
    ft <- as_flextable(cft)
    ft
    

    enter image description here