getwd()
txt<-readLines("shiny.txt")
wordLst<-strsplit(txt,"[[:space:]]|[[:punct:]]")
wordLst<-unlist(wordLst)
wordLst<-tolower(wordLst)
wordLst<- wordLst[wordLst != ""]
wordLst
## [1] "shiny" "is" "an" "r" "package"
## [6] "that" "makes" "it" "easy" "to"
## [11] "build" "interactive" "web" "apps" "straight"
## [16] "from" "r" "you" "can" "host"
## [21] "standalone" "apps" "on" "a" "webpage"
## [26] "or" "embed" "them" "in" "r"
## [31] "markdown" "documents" "or" "build" "dashboards"
## [36] "you" "can" "also" "extend" "your"
## [41] "shiny" "apps" "with" "css" "themes"
## [46] "htmlwidgets" "and" "javascript" "actions"
getWordList<- function(fname) {
txt<-readLines(fname)
wordLst<-strsplit(txt,"[[:space:]]|[[:punct:]]")
wordLst<-unlist(wordLst)
wordLst<-tolower(wordLst)
wordLst<- wordLst[wordLst != ""]
wordLst<- sort(table(wordLst), decreasing=TRUE)
return(wordLst)
}
source("getWordList.R")
getWordList("shiny.txt")
## wordLst
## apps r build can or shiny
## 3 3 2 2 2 2
## you a actions also an and
## 2 1 1 1 1 1
## css dashboards documents easy embed extend
## 1 1 1 1 1 1
## from host htmlwidgets in interactive is
## 1 1 1 1 1 1
## it javascript makes markdown on package
## 1 1 1 1 1 1
## standalone straight that them themes to
## 1 1 1 1 1 1
## web webpage with your
## 1 1 1 1
freq<-sort(table(wordLst), decreasing=TRUE)
全体を1としたときの出現率
relative <- freq / sum(freq)
## wordLst
## apps r build can or shiny
## 0.06122449 0.06122449 0.04081633 0.04081633 0.04081633 0.04081633
## you a actions also an and
## 0.04081633 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816
## css dashboards documents easy embed extend
## 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816
## from host htmlwidgets in interactive is
## 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816
## it javascript makes markdown on package
## 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816
## standalone straight that them themes to
## 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816 0.02040816
## web webpage with your
## 0.02040816 0.02040816 0.02040816 0.02040816
relative
## wordLst
## apps r build can or shiny
## 0.061 0.061 0.041 0.041 0.041 0.041
## you a actions also an and
## 0.041 0.020 0.020 0.020 0.020 0.020
## css dashboards documents easy embed extend
## 0.020 0.020 0.020 0.020 0.020 0.020
## from host htmlwidgets in interactive is
## 0.020 0.020 0.020 0.020 0.020 0.020
## it javascript makes markdown on package
## 0.020 0.020 0.020 0.020 0.020 0.020
## standalone straight that them themes to
## 0.020 0.020 0.020 0.020 0.020 0.020
## web webpage with your
## 0.020 0.020 0.020 0.020
colors = c("red", "blue", "green")
barplot(freq, las=3,col=colors)
plot(0,0,pch=8)
plot(0,0,pch=8,cex=5)
plot(0,0,pch=8,cex=5,col="red")
インタラクティブなプロット
library(manipulate)
picker()関数
manipulate(plot(0,0,pch=8,cex=5,col=myColors), myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan") )
picker()関数
manipulate(
plot(0,0,pch=myMarkers,cex=5,col=myColors), myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan",initial="violet"),
myMarkers=picker(1,2,3,4,5,6,7,8,initial="5")
)
picker()関数
manipulate(
plot(0,0,pch=8,cex=mySize,col="blue"),
mySize=slider(1,10,initial=5)
)
plot(0,0,type="n")
text(0,0, "R",cex=1,col="blue")
manipulate({
plot(0,0,type="n")
text(0,0, "R",cex=1,col=myColors)
},
myColors=picker("red", "yellow", "green", "violet", "orange", "blue", "pink", "cyan") )
freqData <- data.frame(freq)
relativeData <- data.frame(relative)
## wordLst Freq
## 1 apps 3
## 2 r 3
## 3 build 2
## 4 can 2
## 5 or 2
## 6 shiny 2
## wordLst Freq
## 1 apps 0.061
## 2 r 0.061
## 3 build 0.041
## 4 can 0.041
## 5 or 0.041
## 6 shiny 0.041
freqMtx <- merge(freqData, relativeData, all=T, by="wordLst")
## wordLst Freq.x Freq.y
## 1 a 1 0.020
## 2 actions 1 0.020
## 3 also 1 0.020
## 4 an 1 0.020
## 5 and 1 0.020
## 6 apps 3 0.061
names(freqMtx) <- c("term","raw", "relative")
## term raw relative
## 1 a 1 0.020
## 2 actions 1 0.020
## 3 also 1 0.020
## 4 an 1 0.020
## 5 and 1 0.020
## 6 apps 3 0.061
write.csv(freqMtx,"shiny_freq.csv")
dirName <- "testData"
files <- list.files(dirName)
files
## [1] "test1.txt" "test2.txt" "test3.txt"
filesDir <- unlist(lapply(dirName, paste, files, sep = "/"))
filesDir
## [1] "testData/test1.txt" "testData/test2.txt" "testData/test3.txt"
getWordList(filesDir[1])
source("getFreqMtx.R")
res<-getFreqMtx("shiny.txt")
head(res)
## term raw relative
## 1 a 1 0.020
## 2 actions 1 0.020
## 3 also 1 0.020
## 4 an 1 0.020
## 5 and 1 0.020
## 6 apps 3 0.061