我可以强制Rscript在命令行参数中传递通配符吗?

时间:2016-11-02 01:44:25

标签: r shell command-line-arguments glob

当我使用包含通配符*的命令参数运行Rscript时,我可以理清(因为我没有太多运气找到它的文档),该参数扩展为字符向量匹配的文件路径,如果没有匹配则传递。有没有办法一直传递通配符,所以我可以在脚本中自己处理它(例如使用Sys.glob之类的东西)?

这是一个最小的例子,从终端运行:

ls
## foo.csv bar.csv baz.txt
Rscript -e "print(commandArgs(T))" *.csv
## [1] "foo.csv" "bar.csv"
Rscript -e "print(commandArgs(T))" *.txt
## [1] "baz.txt"
Rscript -e "print(commandArgs(T))" *.rds
## [1] "*.rds"
编辑:我了解到这种行为来自bash,而不是Rscript。有没有办法在R中解决这种行为,或者为特定的R脚本而不是Rscript命令抑制通配符扩展?在我的特定情况下,我想运行一个带有两个参数Rscript collapse.R *.rds out.rds的函数,它将许多单个RDS文件的内容连接到一个列表中,并将结果保存在out.rds中。但是由于通配符在传递给R之前被扩展,我无法检查是否已经提供了第二个参数。

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您不希望 public Form1() { InitializeComponent(); } List<Book> books = new List<Book>(); private void Form1_Load(object sender, EventArgs e) { } //Settng Values public void button1_Click(object sender, EventArgs e) { Book b = new Book(); b.Title = textBox1.Text; b.ISBN = textBox2.Text; b.Onloan = trueCheckBox.Checked; listView1.Items.Add(b.Title + ", " + b.ISBN + ", " + b.Onloan); books.Add(b); textBox1.Text = null; textBox2.Text = null; } //Method to check if item is aviable or note - boolean type void avaiable() { if (trueCheckBox.Checked = true) { bool onloan = true; } else { bool onloan = false; } } private void button2_Click(object sender, EventArgs e) { Remove(); } //Remove item from both the List & Listview void Remove() { try { listView1.Items.Remove(listView1.SelectedItems[0]); books.RemoveAt(listView1.SelectedItems[0].Index); } catch { } } //Display information within their field when selected an item is selected private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0) { return; } else { textBox1.Text = books[listView1.SelectedItems[0].Index].Title; textBox2.Text = books[listView1.SelectedItems[0].Index].ISBN; trueCheckBox.Checked = books[listView1.SelectedItems[0].Index].Onloan; } } //Update the values without having to re-add private void button3_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count == 0) { return; } else { books[listView1.SelectedItems[0].Index].Title = textBox1.Text; books[listView1.SelectedItems[0].Index].ISBN = textBox2.Text; books[listView1.SelectedItems[0].Index].Onloan = trueCheckBox.Checked; listView1.SelectedItems[0].Text = textBox1.Text + ", " + textBox2.Text + ", "+ trueCheckBox.Checked; } } private void searchBox_TextChanged(object sender, EventArgs e) { //Here's where I am stuck, I've added a textField and labelled it search box } } //Class - set & get methods class Book { public string isbn; public string title; private Boolean onloan; public Book() { this.isbn = isbn; this.title = title; } public string ISBN { get { return isbn; } set { isbn = value; } } public string Title { get { return title; } set { title = value; } } public Boolean Onloan { get { return onloan; } set { onloan = value; } } } 为您填充通配符,您想要传递表达式本身,例如bash。一些选项包括:

  1. 在引用文本中传递表达式并在R中处理它,或者通过在另一个命令中进行评估或以其他方式

    *.csv
  2. 仅传递扩展名并按上下文

    处理R中的Rscript -e "list.files(pattern = commandArgs(T))" "*\.csv$"
    *
  3. 通过复杂且不必要的方法,为该命令禁用通配:Stop shell wildcard character expansion?

  4. 注意:我已将参数更改为正则表达式,以防止它过于贪婪地匹配。