2015年12月20日日曜日

Rのgooglesheetsパッケージの利用開始について

 グーグルドライブのSheets上のデータテーブルを取得し,Rで処理するためのパッケージgooglesheetsの利用開始方法に関するメモです.


1.パッケージをインストールします.

install.packages("googlesheets")
install.packages("dplyr")

2. ライブラリを読み込みます.
library(googlesheets)
 library(dplyr) 

3. gs_auth関数によって認証をおこないます.引数は以下のとおり
gs_auth(new_user = TRUE)

4.すると以下の反応が返ってきます.書いてあるように,提示されたURLにアクセス.
Removing old credentials ...
httpuv not installed, defaulting to out-of-band authentication
Please point your browser to the following url: 
  https://accounts.google.com/o/oauth2/auth?client_id=17xxxxxxxxxx-f4scmimctv2o96isfppehg1qesrpvjro.apps.googleusercontent.com&scope=https%3A%2F%2Fspreadsheets.google.com%2Ffeeds%20https%3A%2F%2Fdocs.google.com%2Ffeeds&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code
 5.その後,グーグルのアカウント認証を求められますので,それらが終わると画面にPlease copy this code, switch to your application and paste it there:という説明の下に認証コードが提示されます.


6.このコードをEnter authorization code:というプロンプトに続けて入力すると,認証完了です.

7.認証が成功したかどうか確認するには,グーグルアカウントの設定にいきます.https://security.google.com/settings/security/permissionsにあるアプリの設定に,googlesheetsが新しく加わっていれば成功です.

8.次にSheetsファイルの登録が必要になります.登録については以下のサイトが参考になります.今回は,gs_title関数を用いました.
https://htmlpreview.github.io/?https://raw.githubusercontent.com/jennybc/googlesheets/master/vignettes/basic-usage.html#register-a-spreadsheet

9.設定が終わると,Sheetsファイルの中身をRに読み込むことができるようになります.




0 件のコメント: