【发布时间】:2016-05-09 13:24:02
【问题描述】:
我想让其他用户轻松使用我的代码。为此,我想提供一个文件“parameters.txt”,用户可以在其中轻松更改值。我还想在其中插入 cmets(以使其更清晰)。它必须是这样的:
# File of parameters
#
# Here put the year you want to analyze.
# You can choose:
# 1) 2000
# 2) 2001
# 3) 2002
# 4) 2003
year 2004
# Here put the team you want to analyze
# Choose it between
# 1) "Lazio"
# 2) "Juventus"
# 3) "Inter"
team Lazio
当我阅读文件时,我不想发表评论。换句话说,我想阅读除以“#”开头的所有行。
- 这是传递参数的好方法吗?
- 你知道我怎样才能只取我感兴趣的线路吗?
【问题讨论】:
-
你可以做类似
xx <- readLines("/tmp/tmpfile.txt"); xx[!grepl("^#", xx)]的事情。至于这是否是一个好方法,可能更常见的是使用 YAML 之类的标记语言来处理这类事情。但是,如果您可以确保参数文件总是像上面那样格式正确,那么您自己解析我们的参数应该不难。 -
read.table会很好地读取这个文件。默认情况下,它会跳过以#开头的行
标签: r input parameter-passing