我想使用我的Windows(7 64位)机器从Itunes API获取数据并在我的Mac(64位El Capitan)上处理这些数据。我正在使用RJSONIO包来提取应用程序的名称,它们来自不同国家/地区的不同语言。我附上了一些只有几个应用程序的最小例子。我首选的编码是UTF-8。
library(RJSONIO)
getall<-function(ID){
u<-ID
lapply(X = u, function(u){
dat <- fromJSON(u, encoding = "UTF-8")
Name<-try(dat$results[[1]]$trackName)
Artistname<-try(dat$results[[1]]$artistName)
Seller<-try(dat$results[[1]]$sellerName)
results<-return(list(Name, Artistname,Seller))
})
}
apps1<-c("https://itunes.apple.com/lookup?id=335549244", "https://itunes.apple.com/lookup?id=362032276", "https://itunes.apple.com/lookup?id=353410020", "https://itunes.apple.com/lookup?id=350146139","https://itunes.apple.com/lookup?id=358942449", "https://itunes.apple.com/lookup?id=359871187")
system.time(itunesNew<-data.frame(matrix(unlist(getall(ID = apps1), use.names = FALSE), nrow = length(apps1), ncol = 3, byrow = TRUE),stringsAsFactors=FALSE, byrow=T))
colnames(itunesNew)<-c("Name", "Artistname","Seller")
itunesnew2<-cbind(apps1, itunesNew)
我正在使用R和R Studio(两者都是最新版本),并在全局选项中将标准编码设置为UTF-8。我无法使用
将我的语言环境设置为UTF-8Sys.setlocale("LC_MESSAGES", 'en_GB.UTF-8')
或R中的其他版本。我也尝试下载&#34; latin1&#34; (它在PC上看起来还不错),但在Mac上搞砸了(在R Studio中将编码设置为latin1。)。
问题:
答案 0 :(得分:0)
我没有方便的Windows VM,但试试这个(它在你的系统上使用jsonlite
&amp; dplyr
来查看它是否有帮助(我在OS X上运行它):
library(jsonlite)
library(dplyr)
"%||%" <- function(a, b) { if (!is.null(a)) a else b }
apps <- c("https://itunes.apple.com/lookup?id=335549244",
"https://itunes.apple.com/lookup?id=362032276",
"https://itunes.apple.com/lookup?id=353410020",
"https://itunes.apple.com/lookup?id=350146139",
"https://itunes.apple.com/lookup?id=358942449",
"https://itunes.apple.com/lookup?id=359871187")
bind_rows(lapply(apps, function(x) {
res <- jsonlite::fromJSON(x, flatten=TRUE)$results
data_frame(name=res$trackName %||% NA,
artist_name=res$sellerName %||% NA,
seller=res$sellerName %||% NA)
})) -> dat
glimpse(dat)
## Observations: 6
## Variables: 3
## $ name (chr) "A+ the Waverley Novels Collection (15Books)", "A+ 中國養生寶典[卷一]", "...
## $ artist_name (chr) "rice mi", "CHEUNG PUI MAN", "CHEUNG PUI MAN", "CHEUNG PUI MAN", ...
## $ seller (chr) "rice mi", "CHEUNG PUI MAN", "CHEUNG PUI MAN", "CHEUNG PUI MAN", ...