error while printing chart for list of data frames

I am trying to knitprint graph output for list of data frames but getting and error. knit print is working for tables output but not working for graphs. any other solution we have ...???

mchart_single_bar <- function(data, var){
  
  list_data <-  split(dataset = data, split_lstt = lst)
  
  for (d in 1:length(banner)) {
    cat('\n\n#### ', banner[[d]], '\n\n')
    ifelse(length(lst)==1,md<-data,md<-ldata[[d]])
    
    chrt <- bar_chart(md,var)
    
    cat(knit_print(chrt))
  } 
  cat('\n\n')
  
}

i am getting error is

Error in cat(knit_print(chrt)) : argument 1 (type 'list') cannot be handled by 'cat'

Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.


this is the reproducible example :-

dataa<-data.frame(
  aa = c("q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c","q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c"),
  col1=c(1,2,3,2,1,2,3,4,4,4,5,3,4,2,1,2,5,3,2,1,2,4,2,1,3,2,1,2,3,1,2,2,4,4,4,1,2,5,3,5),
  col2=c(2,1,1,7,4,1,2,7,5,7,2,6,2,2,6,3,4,3,2,5,7,5,6,4,4,6,5,6,4,1,7,3,2,7,7,2,3,7,2,4)
)

dataa$col3 <- ifelse(dataa$aa == "y",1,NA)
dataa$col4 <- ifelse(dataa$col2 == 7,1,NA)


val_lab(dataa$col3)<-c("Eur"=1)
val_lab(dataa$col4)<-c("USA"=1)

lst<-list(total(), dataa$col3, dataa$col4)

banner<-list("Europe","USA")


split_data <- function(dataset, split_lstt) {
  if (length(split_lstt) == 1) {return(dataset)
  }else {
    lapply(split_lstt, function(x, dataset) {
      dataset[!is.na(x), ]},dataset)
  }}


freq_for_charts<- function(data,var){
  data<- data[!is.na(data[[var]]),]
  T1<-as.data.frame(table(data[[var]]))
  T1<-T1%>% mutate(Q=as.character(Var1),Freq=as.numeric(Freq))%>% select(Q,Freq)
  all<-sum(T1[,2])
  T1}


single_bar <- function(data, var) {
  
  table <- freq_for_charts(data, var) %>% filter(Freq != 0) %>% mutate(Freq = Freq * 100)
  table$Q <- factor(table$Q, levels = rev(levels(data[[var]])))
  
   g <- ggplot(table, aes(x=reorder(Q,Freq), y=Freq))
  
  chart <- g + 
    geom_bar(stat = "identity", width = 0.5, fill = "#005D5D") +
    labs(x = NULL, y = NULL, subtitle = NULL) +       
    ylab(NULL) +
    coord_flip()   
  return(chart)
}




mchart_single_bar <- function(data, var){
  
  ldata <-  split_data (dataset = data, split_lstt = lst)
  
  for (d in 1:length(banner)) {
    cat('\n\n#### ', banner[[d]], '\n\n')
    ifelse(length(lst)==1,md<-data,md<-ldata[[d]])
    
    chrt <- single_bar(md,var)
    
    cat(knit_print(chrt))
  } 
  cat('\n\n')
  
}


mchart_single_bar(dataa,"col1")

I added missing libraries.
I put the code in an rmarkdown file (with appropriate formatting)
I removed cat() from cat(knitr_print(chrt))

It seemed to knit results. Not sure what the cat() part was trying to acheive, so not sure if you have what you hoped for or not.

---
title: "R Notebook"
output:
  html_document:
    df_print: paged
---

```{r echo=FALSE, include=FALSE}
library(expss)
library(tidyverse)
library(knitr)
dataa<-data.frame(
  aa = c("q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c","q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c"),
  col1=c(1,2,3,2,1,2,3,4,4,4,5,3,4,2,1,2,5,3,2,1,2,4,2,1,3,2,1,2,3,1,2,2,4,4,4,1,2,5,3,5),
  col2=c(2,1,1,7,4,1,2,7,5,7,2,6,2,2,6,3,4,3,2,5,7,5,6,4,4,6,5,6,4,1,7,3,2,7,7,2,3,7,2,4)
)

dataa$col3 <- ifelse(dataa$aa == "y",1,NA)
dataa$col4 <- ifelse(dataa$col2 == 7,1,NA)


val_lab(dataa$col3)<-c("Eur"=1)
val_lab(dataa$col4)<-c("USA"=1)

lst<-list(total(), dataa$col3, dataa$col4)

banner<-list("Europe","USA")


split_data <- function(dataset, split_lstt) {
  if (length(split_lstt) == 1) {return(dataset)
  }else {
    lapply(split_lstt, function(x, dataset) {
      dataset[!is.na(x), ]},dataset)
  }}


freq_for_charts<- function(data,var){
  data<- data[!is.na(data[[var]]),]
  T1<-as.data.frame(table(data[[var]]))
  T1<-T1%>% mutate(Q=as.character(Var1),Freq=as.numeric(Freq))%>% select(Q,Freq)
  all<-sum(T1[,2])
  T1}


single_bar <- function(data, var) {
  
  table <- freq_for_charts(data, var) %>% filter(Freq != 0) %>% mutate(Freq = Freq * 100)
  table$Q <- factor(table$Q, levels = rev(levels(data[[var]])))
  
  g <- ggplot(table, aes(x=reorder(Q,Freq), y=Freq))
  
  chart <- g + 
    geom_bar(stat = "identity", width = 0.5, fill = "#005D5D") +
    labs(x = NULL, y = NULL, subtitle = NULL) +       
    ylab(NULL) +
    coord_flip()   
  return(chart)
}


mchart_single_bar <- function(data, var){
  
  ldata <-  split_data (dataset = data, split_lstt = lst)
  
  for (d in 1:length(banner)) {
    cat('\n\n#### ', banner[[d]], '\n\n')
    ifelse(length(lst)==1,md<-data,md<-ldata[[d]])
    
    chrt <- single_bar(md,var)

    knit_print(chrt)
  } 
  cat('\n\n')
  
}
```

Our Results 
```{r}
mchart_single_bar(dataa,"col1")

```