-1

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

taxize在 R 中运行该库。 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% 的案例中导致下游的正确答案,原因可能超出了这个问题的范围。

因此,我怎样才能将响应自动化为 always be 1

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

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

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

感谢您的帮助。

4

1 回答 1

0

更新:忽略下面。显然,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 上重新编译并安装它,并进行编辑以供此特定项目使用。

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

于 2018-01-23T19:26:06.807 回答