を上記のURLを貼り付けて表示することができます現代のRでREST APIとインタフェースするより(IMO)慣用的な方法である:
httr::GET()
を使用して
library(httr)
library(jsonlite)
library(dplyr)
res <- GET("https://api.typeform.com/v1/form/JlCM2J",
query=list(key="ecab3f590a2af4ca55468adc95686a043bbf6c9a"))
content(res, as="text") %>%
fromJSON(flatten=FALSE) -> out
glimpse(out$responses$answers)
## Observations: 2
## Variables: 26
## $ textfield_38991412 <chr> NA, "A"
## $ dropdown_38991418 <chr> NA, "Accounting"
## $ textarea_38991420 <chr> NA, "A"
## $ textfield_38991413 <chr> NA, "A"
## $ textarea_38991421 <chr> NA, "A"
## $ listimage_38991426_choice <chr> NA, "Company"
## $ textfield_38991414 <chr> NA, "A"
## $ website_38991435 <chr> NA, "http://A.com"
## $ textarea_38991422 <chr> NA, "A"
## $ listimage_38991427_choice <chr> NA, "Sincere"
## $ listimage_38991428_choice <chr> NA, "Male"
## $ list_38991436_choice <chr> NA, "17 or younger"
## $ list_38991437_choice <chr> NA, "Upper class"
## $ listimage_38991429_choice_49501105 <chr> NA, "Store"
## $ listimage_38991430_choice <chr> NA, "Product"
## $ textarea_38991423 <chr> NA, "A"
## $ listimage_38991431_choice <chr> NA, "Techy"
## $ listimage_38991432_choice_49501124 <chr> NA, "Fuchsia Rose"
## $ listimage_38991433_choice <chr> NA, "Classic"
## $ list_38991438_choice <chr> NA, "$3,000 or less"
## $ listimage_38991434_choice_49501140 <chr> NA, "Brand Design"
## $ textarea_38991424 <chr> NA, "A"
## $ textfield_38991415 <chr> NA, "A"
## $ dropdown_38991419 <chr> NA, "Afghanistan"
## $ email_38991439 <chr> NA, "[email protected]"
## $ textarea_38991425 <chr> NA, "A"
- 余分なパラメータを簡単に管理できるようになります。
jsonlite::fromJSON()
を使用して応答を取得し、(必要に応じて)生のテキストはきめ細かな処理を可能に取得するためにhttr::content()
を使用し
- は、直接個々のJSON処理オプションよりはるかに細かく制御することができます。
しかし、本当にすべてのもの(楽しい事実:それはカバーの下に上記のイディオムを次の):簡素化するRパッケージrtypeform
あります
library(rtypeform)
library(dplyr)
res <- get_results("JlCM2J")
glimpse(res$responses$answers)
## Observations: 2
## Variables: 26
## $ textfield_38991412 <chr> NA, "A"
## $ dropdown_38991418 <chr> NA, "Accounting"
## $ textarea_38991420 <chr> NA, "A"
## $ textfield_38991413 <chr> NA, "A"
## $ textarea_38991421 <chr> NA, "A"
## $ listimage_38991426_choice <chr> NA, "Company"
## $ textfield_38991414 <chr> NA, "A"
## $ website_38991435 <chr> NA, "http://A.com"
## $ textarea_38991422 <chr> NA, "A"
## $ listimage_38991427_choice <chr> NA, "Sincere"
## $ listimage_38991428_choice <chr> NA, "Male"
## $ list_38991436_choice <chr> NA, "17 or younger"
## $ list_38991437_choice <chr> NA, "Upper class"
## $ listimage_38991429_choice_49501105 <chr> NA, "Store"
## $ listimage_38991430_choice <chr> NA, "Product"
## $ textarea_38991423 <chr> NA, "A"
## $ listimage_38991431_choice <chr> NA, "Techy"
## $ listimage_38991432_choice_49501124 <chr> NA, "Fuchsia Rose"
## $ listimage_38991433_choice <chr> NA, "Classic"
## $ list_38991438_choice <chr> NA, "$3,000 or less"
## $ listimage_38991434_choice_49501140 <chr> NA, "Brand Design"
## $ textarea_38991424 <chr> NA, "A"
## $ textfield_38991415 <chr> NA, "A"
## $ dropdown_38991419 <chr> NA, "Afghanistan"
## $ email_38991439 <chr> NA, "[email protected]"
## $ textarea_38991425 <chr> NA, "A"
いずれかの方法では、これはRを使用して、あなたの最初の時間でなければなりません(あなたのリストにあるフィールドにアクセスするために$
を使用することに慣れていない場合は、初めてです。 APIデータを扱う前に、Rを学ぶのに本当に時間を費やすべきです。誤った結果と自己挫折は、コーディングの途中でカット&ペーストして祈る代わりに、あなたが得ようとしている唯一のものです。それでもCSVファイル(これはwrite.csv()
)にこれを取得する必要があります。
Excelを使用して最終的には、フォームレスポンスをプログラムで取得するのはなぜですか?残りの作業にRを使用しない場合は、データのダウンロードとサイトへのログインをスクリプト化しない限り、これは不必要なステップのようです。
最後には公開されたフォーラムに投稿したのですぐに無効化してAPIキーを再生成します。"Branding Questionnaire"と "Test Form"という2つのフォームがあり、Typeformで何をしているかを監視して、いつでもフォームデータを取得できるようになりました。 rtypeform
パッケージを使用すると、typeform_api
環境変数(このデータを保持するために~/.Renviron
を使用できます)にAPIキーを格納できるので、スクリプト内で再び公開する必要はありません。
'data.table :: fread'はテーブルを読み込むためのものです。あなたが提供した構造はテーブルではありません。それはそれよりはるかに構造化されています。 (2年以上更新されていない 'rjson'の代わりに' jsonlite'を提案します。) 'x < - readLines(url);を試してください。 str(jsonlite :: fromJSON(x)) 'と言って、私が何を意味するのか見てみましょう。 – r2evans
ありがとうございます。コードを変更した後、これは出力の一部です。回答データフレームのみをどのように抽出しますか? $ answers: 'data.frame':\t 2 obs。 $ dropdown_38991418:chr [1:2] NA "Accounting" .. .. $ textarea_38991420:chr [1:2] NA "A" .. $ textarea_38991420:chr [ 1:2] NA "A" 。 。 –
BTW:重要なコンテンツや実際のコード/出力を追加するときは、コメントを出力するよりも質問を編集するほうが良いでしょう(* "Update:..." *)。方法。あなたは@ hrbrmstrの答えを "受け入れる"必要があります。それはあなたが必要とする(そして習慣的です)。 – r2evans