在系统命令中调用变量

Call a variable in a system command

我在 R 脚本中调用 image magick,它工作正常(见第 1 行)。但是,我想让它从 R 调用一个变量,并根据这个变量更改输出文件的名称。我试图将它粘贴到系统命令中(见第 2 行),但它似乎不起作用。有人知道怎么做吗?

第 1 行:

    system("magick convert -delay 40 *.png K-10 trail_cost - 3 K.gif")  # make a gif of all the photos 

第 2 行:

    system(paste("magick convert -delay 40 *.png K - ", K, "trail_cost - ", trail_cost, ".gif"))  # make a gif of all the photos 

你有一些额外的空间,我会使用 paste0 并在 运行:

之前检查是否相同
K = 10
trail_cost = "3 K"

# check if identical before running system
identical("magick convert -delay 40 *.png K-10 trail_cost - 3 K.gif",
          paste0("magick convert -delay 40 *.png K-", K, " trail_cost - ", trail_cost, ".gif"))
# [1] TRUE

注意:避免在文件名中使用空格。

K = 10
trail_cost = 3

paste0("magick convert -delay 40 *.png K-", K, "trail_cost-", trail_cost, "K.gif")