[英]How do I pull .js or .json data from a specific URL into an R data frame
I have been trying to pull data from this specific URL: http://leafletjs.com/examples/us-states.js using RJSONIO. 我一直在尝试使用RJSONIO从此特定URL: http ://leafletjs.com/examples/us-states.js提取数据。 I went to that URL and saved the data by pressing CTRL+A to select all of the data there and then pasted it into notepad++, saving it as test.json.
我转到该URL并通过按CTRL + A选择那里的所有数据来保存数据,然后将其粘贴到notepad ++中,将其保存为test.json。 Here is what I have tried in R from that point:
从那时起,这就是我在R中尝试过的方法:
library(RJSONIO)
json_file <- dir("test.json")
jsonIntoR <- fromJSON(readLines(json_file)[1])
And I get the following error: 我得到以下错误:
Error in fromJSON(readLines(json_file)[1]) :
error in evaluating the argument 'content' in selecting a method for function 'fromJSON': Error in file(con, "r") : invalid 'description' argument
I'd like to convert the data at this URL into a dataframe, but have not been able to overcome this error. 我想将此URL上的数据转换为数据框,但无法克服此错误。 I have tried using the solution at this link: Import JSON file from URL into R but it does not work for me.
我尝试在此链接上使用该解决方案:将URL中的JSON文件导入R,但对我不起作用。 Thank you for your help.
谢谢您的帮助。
Don't use readLines(...)
. 不要使用
readLines(...)
。
library(rjson)
library(httr)
url <- "http://leafletjs.com/examples/us-states.js"
text <- content(GET(url),type="text")
text <- sub("var statesData = ","",text)
text <- sub(";$","",text)
json <- fromJSON(text)
Your file is actually javascript, so to make it interpretable as json you need to strip off the leadling var statesData =
and the trailing ;
您的文件实际上是javascript,因此要将其解释为json,您需要删除开头的
var statesData =
和结尾的;
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.