【问题标题】:Automate Response at Prompt in R interactive在 R 交互中自动响应提示
【发布时间】:2018-01-23 18:29:38
【问题描述】:

请参阅下面我对按照这些思路提出的上一个问题的参考。

我在 R 中运行库 taxize。Taxize 包含一个用于获取与科学名称 get_tsn() 关联的稳定数字的函数。

我可以在交互模式或非交互模式下运行它,这样我要么 分别提示或不提示在多个命中中进行选择。

互动:

> tax.num <- get_tsn("Acer rubrum", ask=TRUE)
Retrieving data for taxon 'Acer rubrum'
tsn      target                      commonNames      nameUsage
1  28728 Acer rubrum                 red maple        accepted
2  28730 Acer rubrum ssp. drummondii NA not           accepted
3 526853 Acer rubrum var. drummondii Drummond's maple accepted
...
More than one TSN found for taxon 'Acer rubrum'!
Enter rownumber of taxon (other inputs will return 'NA'):

非交互:

> tax.num <- get_tsn("Acer rubrum", ask=TRUE)
Retrieving data for taxon 'Acer rubrum'
Warning message:
> 1 result; no direct match found 

我需要在交互模式下运行这个库,这样当有多个匹配项时我不会得到空结果。然而,照看这个脚本对于我的数据量来说是完全不现实的,这些数据有数百万个学名。因此,我想自动响应提示,以便答案始终为1。这可能是 99% 的案例的正确答案,并且最终仍会在 100% 的案例中导致下游的正确答案,原因可能超出了这个问题的范围。

因此,如何自动将响应始终为1

我查看了this 问题并尝试相应地修改我的代码。

options(httr_oauth_cache=T)
tax.num <- get_tsn("Acer rubrum",ask=T) 

但是,这给出了与上述交互模式相同的结果。

感谢您的帮助。

【问题讨论】:

标签: r interactive non-interactive


【解决方案1】:

更新:忽略下面。显然,Nathan Werth 在上面的评论中发布了最佳答案。

tax.num <- get_tsn_(searchterm = "Acer rubrum", rows = 1)

效果很好!

...

我决定修改源代码来处理这个问题。我怀疑有一个更理想的解决方案,但这一个满足我的需要。

因此,在源文件get_tsn.R 中,我替换了以下代码块

      # prompt
      message("\n\n")
      print(tsn_df)
      message("\nMore than one TSN found for taxon '", x, "'!\n
      Enter rownumber of taxon (other inputs will return 'NA'):\n") 
      # prompt
      take <- scan(n = 1, quiet = TRUE, what = 'raw')

      take <- 1

我本可以删除其他对屏幕位的回显,这是不必要的,现在不正确。

我使用trace("get_tsn",edit=TRUE)测试的修改后的函数返回如下:

> print(tax.num)
[1] "28728"
attr(,"match")
[1] "found"
attr(,"multiple_matches")
[1] TRUE
attr(,"pattern_match")
[1] FALSE
attr(,"uri")
[1] "http://www.itis.gov/servlet/SingleRpt/SingleRpt?
search_topic=TSN&search_value=28728"
attr(,"class")
[1] "tsn"

我现在将在 Linux 上重新编译并安装它,并进行编辑以用于此特定项目。

我仍然欢迎其他更好的答案。

【讨论】:

    猜你喜欢
    • 2019-05-02
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多