通过文件传递代码参数" parameters.txt"

时间:2016-05-09 13:24:02

标签: r input parameter-passing

我想让其他用户可以轻松使用我的代码。为了做到这一点,我想提供一个文件" parameters.txt"用户可以轻松更改值。我也想在其中插入评论(以使其更清晰)。它必须是这样的:

# 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

当我阅读文件时,我不想发表评论。换句话说,除了以"#"开头的那些之外,我想阅读所有的行。

  1. 这是一个很好的传递参数吗?
  2. 你知道我怎么才能只选择我感兴趣的专栏?

1 个答案:

答案 0 :(得分:0)

根据MrFlick的建议,我找到了read.table的解决方案:

parameters <- read.table("./parametri.txt",                                           
                          header = FALSE, blank.lines.skip = TRUE, comment.char="#",   
                          skip = 12     , skipNul = TRUE         , fill=TRUE)

假设参数文件在当前文件夹中。