Reputation: 4470
I am using pipes in R using magrittr library. I need to use for loop in between of the pipes, how would i do that
tmrank.ft <- regulartable(
data = tm.rank.out) %>%
theme_booktabs() %>%
autofit() %>%
merge_v(j=missing_species) %>%
align(align = 'center') %>%
align(align = 'center', part = 'header') %>%
bold(part = 'header') %>%
height(height = 1, part = 'header') %>%
fontsize(size = 14, part = 'all') %>%
width(j=1, width = 1.0 ) %>%
width(j=2:ncol(tm.rank.out), width = 2.5) %>%
{
for (ii in 2:length(species))
{
group_result <- group_vector(tm.rank.out.temp, grp.type, species[ii])
for (jj in 1:length(group_result))
{
text_color <- grp.clr[[unique(grp.type[,grp][jj])]]
color(i = group_result[[jj]], j = ~ species[ii], color = text_color)
}
}
}
it throws me
Error in inherits(x, "flextable") : argument "x" is missing, with no default
Upvotes: 1
Views: 9196
Reputation: 1893
Piping requires functions whose first argument is x
. So just pre-define the function with an argument x
:
foo <- function(x){
for (ii in 2:length(species))
{
group_result <- group_vector(tm.rank.out.temp, grp.type, species[ii])
for (jj in 1:length(group_result))
{
text_color <- grp.clr[[unique(grp.type[,grp][jj])]]
color(i = group_result[[jj]], j = ~ species[ii], color = text_color)
}
}
}
tmrank.ft <- regulartable(
data = tm.rank.out) %>%
theme_booktabs() %>%
autofit() %>%
merge_v(j=missing_species) %>%
align(align = 'center') %>%
align(align = 'center', part = 'header') %>%
bold(part = 'header') %>%
height(height = 1, part = 'header') %>%
fontsize(size = 14, part = 'all') %>%
width(j=1, width = 1.0 ) %>%
width(j=2:ncol(tm.rank.out), width = 2.5) %>%
foo()
Upvotes: 3
Reputation: 613
I hope the code can help you.
seq(1:10) %>% (function(x){
print(x)
})
and here is talk about this method
Upvotes: 2