r - 在命令行 (shell/bash) : what to do when column names contain tilde (~) 中将参数传递给 R 脚本

标签 r bash shell command-line rscript

我正在使用 Rscript通过 bash 运行 R 脚本,我想指定要传递给脚本本身内函数的参数。具体来说,我想传递指定的参数:

  • 数据文件的路径( .csv )和
  • 该数据文件中的某些列名称。

  • 当列名包含波浪号 ( ~ ) 时,我遇到了问题。我试过用反引号包裹列名,但仍然不成功。
    例子
    我想编写一个脚本来接收 .csv 中的数据文件格式化并根据用户的选择为一个变量绘制直方图。
    这是我的功能:
    plot_histogram <- function(path_to_input, x_var) {
      
      data_raw <- read.csv(file = path_to_input)
      
      path_to_output_folder <- dirname(path_to_input)
      
      png(filename = paste0(path_to_output_folder, "/", "output_plot.png"))
      
      hist(as.numeric(na.omit(data_raw[[x_var]])), main = "histogram", xlab = "my_var")
      
      replicate(dev.off(), n = 20)
    }
    
    让我们在一些假数据上运行它
    set.seed(123)
    df <- data.frame(age = sample(20:80, size = 100, replace = TRUE))
    
    write.csv(df, "some_age_data.csv")
    
    plot_histogram(path_to_input = "some_age_data.csv",
                   x_var = "age")
    
    正如预期的那样,我得到了一个 .png与情节的文件,保存到.csv所在的同一目录中在
    hist
    现在自定义要从命令行运行的 R 脚本
    plot_histogram.R
    args <- commandArgs(trailingOnly = TRUE)
    
    ## same function as above
    plot_histogram <- function(path_to_input, x_var) {
      
      data_raw <- read.csv(file = path_to_input)
      path_to_output_folder <- dirname(path_to_input)
      png(filename = paste0(path_to_output_folder, "/", "output_plot.png"))
      hist(as.numeric(na.omit(data_raw[[x_var]])), main = "histogram", xlab = "my_var")
      replicate(dev.off(), n = 20)
    }
    
    plot_histogram(path_to_input = args[1], x_var = args[2])
    
    然后使用 Rscript 通过命令行运行
    $ Rscript --vanilla plot_histogram.R /../../../some_age_data.csv "age"
    
    也有效!
    但是,如果列名包含波浪号,事情就会中断
    第 1 步:创建假数据
    library(tibble)
    
    set.seed(123)
    df <- tibble(`age-blah~value` = sample(20:80, size = 100, replace = T))
    
    write.csv(df, "some_age_data.csv")
    
    第 2 步:使用 Rscript :
    $ Rscript --vanilla plot_histogram.R /../../../some_age_data.csv "age-blah~value"
    

    Error in hist.default(as.numeric(na.omit(data_raw[[x_var]])), main = "histogram", : invalid number of 'breaks' Calls: plot_histogram -> hist -> hist.default Execution halted


    底线
    使用时Rscript ,如何传递指定包含波浪号的列名的参数?或者,我该如何解决 .csvRscript 的框架内,列名中具有这种波浪号格式的文件?
    谢谢!

    最佳答案

    您正在成功传递一个参数,该参数指定包含波浪号的列名。然而,read.csv已“固定”列名,因此它实际上不包含波浪号。read.csv正在默默地将列名转换为 age.blah.value .使用 check.names = FALSE使其成为age-blah~value .

    data_raw <- read.csv(file = path_to_input, check.names = FALSE)
    

    关于r - 在命令行 (shell/bash) : what to do when column names contain tilde (~) 中将参数传递给 R 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64598593/

    相关文章:

    r - 快速将向量写入文件 r

    r - 如何找到统计模式?

    linux - 具有 2 个不同 .pem key 的 2 个服务器之间的 scp 命令

    python - openssl 命令如何处理用 python 添加的 PKCS#7 填充

    r - 将下拉列表添加到 DT 表中的每一列,其中下拉列表中的值是从另一个数据帧获取的

    html - 如何在Shiny中默认选择verbatimTextOutput中的文本?

    bash - 散列文本文件每一行的最有效方法?

    bash - 向 GNU screen 发送命令

    linux - xargs 无法获取用户输入?

    c - 向程序中添加一个函数,并从函数中的命令行调用该函数