【问题标题】:Paste glue objects together run in loops将胶水对象粘贴在一起循环运行
【发布时间】:2021-12-20 11:45:04
【问题描述】:

我正在尝试将两个胶水字符串粘贴到一个 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 
------------------------------------- 

【问题讨论】:

    标签: r r-glue


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多