-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00_Functions.R
142 lines (133 loc) · 3.82 KB
/
00_Functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# All Useful Functions and Packages
if (!require(shiny)){
install.packages("shiny")
library(shiny)}
if (!require(png)){
install.packages("png")
library(png)}
if (!require(ggplot2)){
install.packages("ggplot2")
library(ggplot2)}
if (!require(ggformula)){
install.packages("ggformula")
library(ggformula)}
if (!require(devtools)){
install.packages("devtools")
library(devtools)}
if (!require(devtools)){
install.packages("devtools")
library(devtools)}
if (!require(reshape2)){
install.packages("reshape2")
library(reshape2)}
if (!require(magick)){
install.packages("magick")
library(magick)}
if (!require(tm)){
install.packages("tm")
library(tm)}
if (!require(wordcloud2)){
install.packages("wordcloud2")
library(wordcloud2)}
if (!require(httr)){
install.packages("httr")
library(httr)}
if (!require(dplyr)){
install.packages("dplyr")
library(dplyr)}
if (!require(data.tree)){
install.packages("data.tree")
library(data.tree)}
if (!require(igraph)){
install.packages("igraph")
library(igraph)}
if (!require(visNetwork)){
install.packages("visNetwork")
library(visNetwork)}
if (!require(circlepackeR)){
devtools::install_github("jeromefroe/circlepackeR")
library(circlepackeR)}
ifnull0 <-function(Null){
if(is.null(Null)){
return(0)
}
else if(is.na(Null)){
return(0)
}
else{
return(Null)
}
}
ifnulls <-function(Null){
if(is.null(Null)){
return("")
}
else if(is.na(Null)){
return("")
}
else{
return(Null)
}
}
getattackmoves <- function(move){
base="http://pokeapi.co/api/v2/"
url=paste0(base, "move/", tolower(move), "/")
s=GET(url)
w2= content(s)
movestats=data.frame(w2$id, w2$name, ifnulls(w2$type$name), as.numeric(ifnull0(w2$power)), as.numeric(ifnull0(w2$accuracy)), as.character(ifnulls(w2$damage_class$name)), as.numeric(ifnull0(w2$pp)), stringsAsFactors = F)
names(movestats)=c("id", "name", "type", "power", "accuracy", "class", "pp")
return(movestats)
}
getpokemonmoves<- function(poke){
base="http://pokeapi.co/api/v2/"
url=paste0(base, "pokemon/", tolower(poke), "/")
s=GET(url)
w= content(s)
pokenow=c()
for (i in 1:length(w$moves)){
pokenow=c(pokenow,as.character(w$moves[[i]]$move$name))
}
return(pokenow)
}
getpokemonstats <- function(poke){
base="http://pokeapi.co/api/v2/"
url=paste0(base, "pokemon/", tolower(poke), "/")
s=GET(url)
w= content(s, "parsed")
pokemonstats=data.frame(w$id,w$name, w$stats[[1]]$base_stat,
w$stats[[2]]$base_stat,w$stats[[3]]$base_stat,
w$stats[[4]]$base_stat,w$stats[[5]]$base_stat,
w$stats[[6]]$base_stat, w$sprites$front_default,
w$types[[1]]$type$name,
if(class(try(w$types[[2]]$type$name,silent = T))=="try-error"){NA} else {w$types[[2]]$type$name})
names(pokemonstats)=c("id","name", "speed", "special-defense", "special-attack",
"defense", "attack", "hp", "picture","type", "type2")
return(pokemonstats)
}
getflavourText<- function(poke){
base="http://pokeapi.co/api/v2/"
url=paste0(base, "pokemon-species/", tolower(poke), "/")
s=GET(url)
w2= content(s, "parsed")
flavtext=data.frame(w2$name, c(""))
colnames(flavtext)= c("name", "text")
t=""
#Create long string with just words of pokedex entry
for (i in 1:length(w2$flavor_text_entries)){
if (w2$flavor_text_entries[[i]]$language$name== "en"){
t=paste(t, w2$flavor_text_entries[[i]]$flavor_text, sep = " ")
}
}
flavtext$text=t
return(flavtext)
}
createWordCloud=function(text, remove=c("")){
r=Corpus(VectorSource(text))
r<-tm_map(r,removeWords, stopwords(c("english")))
r<-tm_map(r,removeWords, remove)
rtm=TermDocumentMatrix(r)
m=as.matrix(rtm)
v=sort(rowSums(m), decreasing = T)
d=data.frame(word=names(v), freq=v)
return(d)
}