looping functions to produce reports for multiple checks in R_markdown

I am looking for a functionality where i can iterate my functions in rmarkdown to create multiple reports rather than updating chunks because that will take a lot of time and effort.
you can get a understanding of my requirement after running below example

# I have a data frame like below:-
df<-data.frame(
  gender = c(1,2,1,2,1,2,1,2,2,2,2,1,1,2,2,2,2,1,1,1,1,1,2,1,2,1,2,2,2,1,2,1,2,1,2,1,2,2,2),
  sector = c(3,3,1,2,5,4,4,4,4,3,3,4,3,4,2,1,4,2,3,4,4,4,3,1,2,1,5,5,4,3,1,4,5,2,3,4,5,1,4),
  col1=c(1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,1,2,2,2,1,1,1,2,1,1,2,2,2,2,1,2,2,2,2,1,2,1),
  col2=c(1,1,1,1,1,2,2,2,2,1,1,1,1,1,2,2,2,1,1,1,2,1,1,1,1,1,2,2,2,1,1,1,1,1,1,1,2,2,2),
  col3=c(1,1,2,2,2,2,2,1,2,2,2,2,2,2,2,2,1,2,2,2,2,2,1,1,1,2,2,2,1,2,2,1,1,1,1,1,2,2,1),
  col4=c(1,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2),
  col5=c(1,2,1,1,1,2,1,2,2,1,2,1,1,1,2,2,2,1,1,1,2,1,2,1,1,1,2,2,2,1,1,2,2,1,1,1,2,2,2)
)

# Now I have done some labeling for the data

df$gender<-factor(df$gender, levels=c(1,2), labels=c("Male","female"))
df$sector<-factor(df$sector, levels=c(1,2,3,4,5), labels=c("TX","CA","NY","LA","WA"))
df$col1<-factor(df$col1, levels=c(1,2), labels=c("Sales_1","Sales_2"))
df$col2<-factor(df$col2, levels=c(1,2), labels=c("OPS_1","OPS_2"))
df$col3<-factor(df$col3, levels=c(1,2), labels=c("Management","MGMR"))
df$col4<-factor(df$col4, levels=c(1,2), labels=c("HR","HT"))
df$col5<-factor(df$col5, levels=c(1,2), labels=c("National","Overseas"))

# as a requirement i have created some subsets

dd1<- subset(df, df$sector=="TX")
dd2<- subset(df, df$col2=="OPS")
dd3<- subset(df, df$col4=="HT")

# Now i am am creating a list of headers for which i want to show my summary accordingly , this list can be many....

ldat<-list("MGMT1" = dd1, "MGMT2" =dd2, "MGMT3" = dd3)

# Now here I am iterating my cro_cpct function for example for some checks

panderOptions('knitr.auto.asis', FALSE)

for (i in 1:length(ldat)) {
  
  cat('\n\n### table for ..... `', names(ldat)[i], '`\n\n')
  md<-ldat[[i]]
  pander::pander(expss::cro_cpct(md[["col3"]], md[["col5"]] ))
  cat('\n\n')
}

after running this you will know that i am looking for what kind of tables

so this a example what my requirement is.........
but i have created many function to create summary table,i want like all the functions should iterate for "ldat" accordingly........

I'm sorry but your code (which doesnt work as you would wish) can't possibly stand in for a clear explanation of what you wish it would do...

Hey, if I swap out the undefined function cro_cpct for a simple function like dplyr::bind_cols I can get some output from your first post... but ... so what ? I have no idea what is wrong with that...

Do you have a specific problem you want help to solve?
Can you illustrate that in a simple/straightforward way?

it strikes me as just possible that your second post is an attempt to place a function tab_std in place of cro_cpct because, but ,you are leaving a lot of guesswork for me vis what params need passing and from where and why.

I have just updated my question

yes i will be using this kind of functionality for

ok, so assuming it is a function

tab_std<-function(data, var, Name_of_variable, footer){

can you show how you invisage it being called., have you tested passing parameters to it ?
and can you state what your problem is ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.