使用R复制文件 [英] using R to copy files

查看:135
本文介绍了使用R复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为在Windows下执行的更大任务的一部分,我想在目录之间复制所选文件。是否可以在R中给出 cp patha / filea * .csv pathb (注意通配符,额外的香料)的命令?

As part of a larger task performed in R run under windows, I would like to copy selected files between directories. Is it possible to give within R a command like cp patha/filea*.csv pathb (notice the wildcard, for extra spice)?

推荐答案

我不认为有一个直接的方式(害怕的shelling-out),但像下面的东西通常适用于我。

I don't think there is a direct way (shy of shelling-out), but something like the following usually works for me.

flist <- list.files("patha", "^filea.+[.]csv$", full.names = TRUE)
file.copy(flist, "pathb")

注意:


  • 我故意分两步,可以合并。

  • 查看正则表达式:R使用true正则表达式,并且还在两个单独的参数中将文件模式与路径分开。

  • 请注意 ^ $ (beg / end of string)在regex中 - 这是一个常见的陷阱,因为这些是通配符类型模式,但是需要regex

  • 在Windows环境中,用户通常会添加 ignore.case = TRUE 参数 list.files ,以模拟目录搜索对此操作系统不区分大小写的事实。

  • R的 glob2rx()函数提供了一种将通配符模式转换为正则表达式的便捷方法。例如 fpattern = glob2rx('filea * .csv')返回不同的但等价的正则表达式。

  • I purposely decomposed in two steps, they can be combined.
  • See the regular expression: R uses true regex, and also separates the file pattern from the path, in two separate arguments.
  • note the ^ and $ (beg/end of string) in the regex -- this is a common gotcha, as these are implicit to wildcard-type patterns, but required with regexes (lest some file names which match the wildcard pattern but also start and/or end with additional text be selected as well).
  • In the Windows world, people will typically add the ignore.case = TRUE argument to list.files, in order to emulate the fact that directory searches are case insensitive with this OS.
  • R's glob2rx() function provides a convenient way to convert wildcard patterns to regular expressions. For example fpattern = glob2rx('filea*.csv') returns a different but equivalent regex.

这篇关于使用R复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆