首页 > 解决方案 > 将胶水对象粘贴在一起循环运行

问题描述

我试图在 for 循环中将两个胶水串粘贴在一起,但我没有得到想要的结果。例如,在复制示例中,我有两列,我想先循环第一列(一次)并在第二列的每个值上应用函数(x),依此类推,但是胶水代码运行第一列(一次又一次)-功能(第二列)。

到目前为止,我觉得这个问题令人困惑,希望下面的示例可以帮助澄清我的问题。

#Reproduible example
#sample dataframe
col_A <- rep(c("one","two", "three", "four") ,each = 3)
col_B <- rep(c("yes", "No", "Maybe"),times = 4)

df <- bind_cols(a = col_A, b = col_B)

glucode_combined <- "" # Initialize empty string

# the loop over values to create a flexdashboard

for (i in unique(df$a)){

code_A <- glue(
    "{i} \n",
    "======================================================================= \n",
    )

code_B <- df %>% 
    filter(a == i) %>% 
    arrange(b) %>%
    glue_data(

"------------------------------------- \n",
"> ColumnA: {a} | ColumnB: {b} \n",
"------------------------------------- \n",
" \n",
    )

        
glucode_combined <- paste(glucode_combined, code_A, code_B, sep = "\n")
}

writeLines(glucode_combined,"glucode_combined.txt")

这会产生如下所示的结果(循环的第一部分一遍又一遍地重复

one 
======================================================================= 
------------------------------------- 
> ColumnA: one | ColumnB: Maybe 
------------------------------------- 
 
two 
======================================================================= 
------------------------------------- 
> ColumnA: two | ColumnB: Maybe 
------------------------------------- 
 
three 
======================================================================= 
------------------------------------- 
> ColumnA: three | ColumnB: Maybe 
------------------------------------- 
 
four 
======================================================================= 
------------------------------------- 
> ColumnA: four | ColumnB: Maybe 
------------------------------------- 
 

one 
======================================================================= 
------------------------------------- 
> ColumnA: one | ColumnB: No 
------------------------------------- 
 
two 
======================================================================= 
------------------------------------- 
> ColumnA: two | ColumnB: No 
------------------------------------- 
 
three 
======================================================================= 
------------------------------------- 
> ColumnA: three | ColumnB: No 
------------------------------------- 
 
four 
======================================================================= 
------------------------------------- 
> ColumnA: four | ColumnB: No 
------------------------------------- 

但是我想产生如下结果,但我不确定我错过了什么

one 
======================================================================= 

------------------------------------- 
> ColumnA: one | ColumnB: yes 
------------------------------------- 

------------------------------------- 
> ColumnA: one | ColumnB: Maybe 
------------------------------------- 

------------------------------------- 
> ColumnA: one | ColumnB: No 
------------------------------------- 


 
two 
======================================================================= 

------------------------------------- 
> ColumnA: one | ColumnB: yes 
------------------------------------- 

------------------------------------- 
> ColumnA: one | ColumnB: Maybe 
------------------------------------- 

------------------------------------- 
> ColumnA: one | ColumnB: No 
------------------------------------- 

标签: rr-glue

解决方案


问题在于它paste是向量化的,并且code_B是一个长度为 3 的向量,因此 paste 尝试组合code_B 中每个元素的字符串。看起来你不想要这种行为,所以首先使用图书馆中的code_B东西str_flatten来展平:stringr

code_B <- df %>% 
    filter(a == i) %>% 
    arrange(b) %>%
    glue_data(
        "------------------------------------- \n",
        "> ColumnA: {a} | ColumnB: {b} \n",
        "------------------------------------- \n",
        " \n",
    ) %>%
    stringr::str_flatten()

推荐阅读