0

I have a data frame in R called data and looks like this:

library(tibble)
library(flextable)
library(dplyr)

# Create a tibble
data <- tibble(
  country = c("USA", "Canada", "Germany", "France", "Japan", "USA", "USA", "Germany"),
  project = c("Project A", "Project B", "Project C", "Project D", "Project E", "Project F", "Project G", "Project H"),
  val1 = c(10, 20, 30, 40, 50, 60, 70, 80),
  val2 = c(15, 25, 35, 45, 55, 65, 75, 85),
  val3 = c(12, 22, 32, 42, 52, 62, 72, 82)
)


> data
# A tibble: 8 × 5
  country project    val1  val2  val3
  <chr>   <chr>     <dbl> <dbl> <dbl>
1 USA     Project A    10    15    12
2 Canada  Project B    20    25    22
3 Germany Project C    30    35    32
4 France  Project D    40    45    42
5 Japan   Project E    50    55    52
6 USA     Project F    60    65    62
7 USA     Project G    70    75    72
8 Germany Project H    80    85    82

I want to do some basic calculations and then show in different pages in a pdf each project table.

My attempt is:

flextables_list =  data %>%
  rowwise() %>%
  mutate(mean_val = mean(c_across(val1:val3))) %>%
  group_split(project) %>%
  lapply(function(project_data) {
    flextable::flextable(project_data[, c("project", "country", "mean_val")])
  }) 

and then I print them in order to show them:

for (tbl in flextables_list) {
  print(tbl)
}

But when I render the rmarkdown to PDF, the tables (flextables) do not show. Why?

3
  • 3
    see stackoverflow.com/a/79416220/3315962 Commented Feb 8 at 9:54
  • @DavidGohel I worked your solution on the link in your comment. But when I try to flextable::merge_at(i = 6:7, j = 1) gives me : Error in parse_block(g[-1], g[1], params.src, markdown_mode) : ` Duplicate chunk label 'unnamed-chunk-2', which has been used for the chunk: x` Commented Feb 9 at 8:45
  • 1
    that means you have a duplicate chunk label 'unnamed-chunk-2' Commented Feb 9 at 18:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.