getwd()
2+3
## [1] 5
n1<-2
n2<-3
n1*n2
## [1] 6
lst <- c(2,3)
lst[1]*lst[2]
## [1] 6
arg <- 4
sqrt <- sqrt(arg)
paste("平方根は",arg)
## [1] "平方根は 4"
printSQRT<- function(arg) {
sqrt <- sqrt(arg)
paste("平方根は",arg)
}
source("printSQRT.R")
printSQRT(7)
## [1] "平方根は..... 7"
2^3
## [1] 8
calcPower<- function(arg1, arg2) {
power <- arg1^arg2
return (power)
}
calcPower(2,5)
## [1] 32
一行ずつ読み込んで、リストに格納
txt<-readLines("shiny.txt")
## [1] "Shiny is an R package that makes it easy to build interactive web apps straight from R. "
## [2] "You can host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. "
## [3] "You can also extend your Shiny apps with CSS themes, htmlwidgets, and JavaScript actions."
txt[1]
## [1] "Shiny is an R package that makes it easy to build interactive web apps straight from R. "
length(txt)
## [1] 3
Punctuation characters:
! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~.
wordLst<-strsplit(txt,"[[:space:]]|[[:punct:]]")
## [[1]]
## [1] "Shiny" "is" "an" "R" "package"
## [6] "that" "makes" "it" "easy" "to"
## [11] "build" "interactive" "web" "apps" "straight"
## [16] "from" "R" ""
##
## [[2]]
## [1] "You" "can" "host" "standalone" "apps"
## [6] "on" "a" "webpage" "or" "embed"
## [11] "them" "in" "R" "Markdown" "documents"
## [16] "or" "build" "dashboards" ""
##
## [[3]]
## [1] "You" "can" "also" "extend" "your"
## [6] "Shiny" "apps" "with" "CSS" "themes"
## [11] "" "htmlwidgets" "" "and" "JavaScript"
## [16] "actions"
wordLst<-unlist(wordLst)
## [1] "Shiny" "is" "an" "R" "package"
## [6] "that" "makes" "it" "easy" "to"
## [11] "build" "interactive" "web" "apps" "straight"
## [16] "from" "R" "" "You" "can"
## [21] "host" "standalone" "apps" "on" "a"
## [26] "webpage" "or" "embed" "them" "in"
## [31] "R" "Markdown" "documents" "or" "build"
## [36] "dashboards" "" "You" "can" "also"
## [41] "extend" "your" "Shiny" "apps" "with"
## [46] "CSS" "themes" "" "htmlwidgets" ""
## [51] "and" "JavaScript" "actions"
wordLst<-tolower(wordLst)
## [1] "shiny" "is" "an" "r" "package"
## [6] "that" "makes" "it" "easy" "to"
## [11] "build" "interactive" "web" "apps" "straight"
## [16] "from" "r" "" "you" "can"
## [21] "host" "standalone" "apps" "on" "a"
## [26] "webpage" "or" "embed" "them" "in"
## [31] "r" "markdown" "documents" "or" "build"
## [36] "dashboards" "" "you" "can" "also"
## [41] "extend" "your" "shiny" "apps" "with"
## [46] "css" "themes" "" "htmlwidgets" ""
## [51] "and" "javascript" "actions"
wordLst<-wordLst[nchar(wordLst)>0]
wordLst<- wordLst[wordLst != ""]
tokens <- length(wordLst)
## [1] 49
types <- length(unique(wordLst))
## [1] 40
\[TTR=\frac{types}{tokens} \times 100 \]
types/tokens*100
## [1] 81.63265
TTR <- round(types/tokens*100,2)
## [1] 81.63
freq<-sort(table(wordLst), decreasing=TRUE)
## 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
全体を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
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")
barplot(freq, las=3)
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")
インタラクティブなプロット
install.packages("manipulate")
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") )
plot(0,0,type="n")
text(0.5,-0.5,"Shiny",col="red",cex=1.5)
text(0.0,0.5,"R",col="blue",cex=1.5)
manipulate({
plot(0,0,type="n")
text(x,-0.5,"Shiny",col="red",cex=1.5)
},
x=slider(-1,1,initial=0, step=0.2)
)
テキストファイル名を引数にして、単語の頻度数と相対頻度をマージした行列データを出力する関数を作成しなさい。
関数名はgetFreqMtxとし、関数ファイル(getFreqMtx.R)をメールで提出すること。
提出前に、テキストファイル"shiny.txt"を使用して、正しく実行できるかを必ず確認すること。
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