Jea*_*ams 13 r batch-processing twitter-oauth
我正在使用R包twitteR将项目发布到Twitter.我把所有东西都放在一个函数中,它工作正常.但是,我想在没有提示响应的情况下运行该函数,我还没想出如何做到这一点.有什么建议?
以下是我的功能:
doit <- function(<snip>) {
<snip>
# connect to Twitter
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
<snip>
}
Run Code Online (Sandbox Code Playgroud)
当我从命令行运行该函数时,系统会提示我进行交互式响应.
[1] "Using direct authentication"
Use a local file to cache OAuth access credentials between R sessions?
1: Yes
2: No
Selection:
Run Code Online (Sandbox Code Playgroud)
当setup_twitter_oauth()函数在函数之外时,我可以通过在以下行中输入我的响应来直接在脚本中提供此信息,这与其他用户输入函数(如readline()或scan())非常相似.
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
1
Run Code Online (Sandbox Code Playgroud)
但是,当setup_twitter_oauth()是函数的INSIDE时,我无法使用此方法.
我将不胜感激任何有关如何在不需要用户输入的情况下运行此建议的建议.
=====
来自@NicE的答案就是诀窍.我将我的功能中的选项设置合并为:
doit <- function(<snip>) {
<snip>
# connect to Twitter
origop <- options("httr_oauth_cache")
options(httr_oauth_cache=TRUE)
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
options(httr_oauth_cache=origop)
<snip>
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*icE 13
您可以尝试将该httr_oauth_cache选项设置为TRUE:
options(httr_oauth_cache=T)
Run Code Online (Sandbox Code Playgroud)
twitteR包使用该httr包,在该包的Token手册页上,他们提供了有关缓存的提示:
OAuth tokens are cached on disk in a file called .httr-oauth
saved in the current working directory. Caching is enabled if:
The session is interactive, and the user agrees to it, OR
The .httr-oauth file is already present, OR
getOption("httr_oauth_cache") is TRUE
You can suppress caching by setting the httr_oauth_cache option to FALSE.
Run Code Online (Sandbox Code Playgroud)