13

I am using the R package twitteR to post items to Twitter. I put everything inside of a function and it works fine. However, I would like to run the function without being prompted for a response, and I haven't figured out how to do that. Any suggestions?

Here are the bare bones of my function:

doit <- function(<snip>) {
    <snip>
    # connect to Twitter
    setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
    <snip>
    }

When I run the function from the command line, I am prompted for an interactive response.

[1] "Using direct authentication"
Use a local file to cache OAuth access credentials between R sessions?
1: Yes
2: No

Selection: 

I can provide this information directly in a script when the setup_twitter_oauth() function is outside of a function, by entering my response in the following line, much like can be done for other user input functions like readline() or scan().

setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
1

However, I haven't been able to get this approach to work when setup_twitter_oauth() is INSIDE of a function.

I would appreciate any suggestions on how to get this to run without requiring user input.

=====

The answer from @NicE below did the trick. I incorporated the options setting in my function as:

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>
    }
NicE
  • 21,165
  • 3
  • 51
  • 68
Jean V. Adams
  • 4,634
  • 2
  • 29
  • 46
  • Your question ended up helping me. Since i just wanted to pass the selection (1) outside an function. Thanks! – Jhonatas Kleinkauff Apr 08 '16 at 18:56
  • I couldn't got an error trying your code with the part. What do you suggest I replace that with? – nak5120 Oct 22 '17 at 23:55
  • The `` is not actual R code, it's just my shorthand way of indicating that in my script I've removed some other code (not relevant to the question) for clarity. – Jean V. Adams Oct 24 '17 at 19:16

3 Answers3

14

You can try setting the httr_oauth_cache option to TRUE:

options(httr_oauth_cache=T)

The twitteR package uses the httr package, on the Token manual page for that package they give tips about caching:

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.
NicE
  • 21,165
  • 3
  • 51
  • 68
0

This works perfectly fine.

install.packages("twitteR", dependencies = T)
install.packages(c('ROAuth','RCurl'))
install.packages("httr")
library(httr)
require('ROAuth')
require('RCurl')
library(twitteR)



reqURL <- "https://api.twitter.com/oauth/request_token"

accessURL <- "https://api.twitter.com/oauth/access_token"

authURL <- "https://api.twitter.com/oauth/authorize"

consumerKey <- "XXXXXXXXXXXXX"

consumerSecret <- "XXXXXXXXXXXXXXXXXXXXX"

twitCred <-     OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)

download.file(url="https://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")

 twitCred$handshake(cainfo="cacert.pem")

  setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
-1

I don't know much about it.

But if it's in batch, maybe try this:

doit <- function(<snip>) {
    <snip>
    # connect to Twitter
    setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
    < echo 1
    <snip>
}

Also have you tried posting the 1 outside the function to see if it does the same?

And maybe it will work if you put the 1 under the snip

These are just suggestions, cause i don't know very much about the topic, but it might help though.

  • I'm not sure what you mean by the `< echo 1` line. The `<` is a relational operator. And I know of no function with the name `echo`. When I pasted it in, I got `Error: unexpected '<' in: "setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret) <"`. I also tried your suggestion of putting the `1` under the `snip`. That didn't work. I will try the `1` outside of the function next. – Jean V. Adams Feb 02 '15 at 18:01
  • 2
    Probably a good idea to test your solution before posting it as an answer, especially if you "don't know much about the subject"... – nrussell Feb 02 '15 at 21:31
  • 1
    @nrussel I was just trying to help, it might have worked, but i guess it isn't batch. Sorry it was stupid of me trying to help withouth knowing much about it. – Jeroen Tondeleir Feb 03 '15 at 07:33
  • That's what comments are for. – nrussell Feb 03 '15 at 11:55
  • 2
    @nrussell I don't have enough reputation yet, to post comments – Jeroen Tondeleir Feb 03 '15 at 14:01