First of all, you need to create a Twitter application.

Go to Scroll down to the bottom and click in the Manage Your App link. This will take you to

Click the Create New App button
Give a name and description to your app.
Enter a valid web site address in the Website field.
In the Callback URL field write:
Agree the developer agreement and click the Create your Twitter application button.

You will see two important numbers, the customer key (API key) and the consumer secret (api secret).
We will use them in the next steps.

now, in your R workspace:

Include twitteR and httpuv libraries in your current workspace:


Use twitter API to your recently created app:

setup_twitter_oauth("your consumer key", "your consumer secret")  

When asked if want to use a local file to auth in twitter enter No.

Wait until your internet explorer opens to authorize your app to use your twitter account.

You should now be able to use twitter API.