研究のページ/R/shinyとplotly
をテンプレートにして作成
[
トップ
] [
新規
| |
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
#topicpath
** shiny と plotly [#x52817d9]
- 目的~
何をやりたいか。それは、プロダクトに語らせることにする。~
~
- サンプル~
shiny と plotly を使ったページ~
-- [[COVID19 の新規感染者数の推移>https://robo.mydns.jp:3...
--- shiny の働き~
このページにアクセスすると厚労省が提供するCSVファイルがダ...
&color(red){実行時にデータが更新};されることがひとつのポ...
表示する都道府県を&color(red){選択};できる。~
~
--- plotly の働き~
単にグラフを表示するだけでなく、グラフの&color(red){表示...
といった操作ができ、&color(red){マウスオーバーで値が表};...
~
** インスール [#dfef6629]
- 環境
-- Debian Buster
-- R 3.5.2
- 手順
++ shiny パッケージ
sudo su - -c "R -e \"install.packages('shiny', repos='ht...
++ shiny server
sudo apt-get install gdebi-core
wget https://download3.rstudio.org/ubuntu-14.04/x86_64/s...
sudo gdebi shiny-server-1.5.16.958-amd64.deb
++ テスト~
--- http://127.0.0.1:3838/ あるいは、http://インストール...
--- http://127.0.0.1:3838/sample-apps/hello/
~
- 参考URL
-- [[Download Shiny Server for Ubuntu 16.04 or later>http...
** デプロイ(作ったものをウエブで公開) [#ba745620]
- 設置方法~
ui.R と server.R を /srv/shiny-server/ 以下に作ったディレ...
~
- アクセス方法~
http://127.0.0.1:3838/ディレクトリ名 でアクセスする。外部...
~
** サンプルプログラム [#sfcad75a]
-- サイドバーがある例:厚生労働省のサイトから新規感染者数...
--- [[実際のサイト>https://robo.mydns.jp:3838/COVID19/Jap...
--- ui.R~
#divregion
library(shiny)
library(plotly)
#### Define UI for application that plots random distrib...
shinyUI(pageWithSidebar(
## Application title
headerPanel("COVID19 Infection"),
sidebarPanel( width=2,
selectInput("Prefecture",
"Choose a prefecture:",
choices=list("ALL", "Tokyo", "Kanaga...
),
## Show a plot of the generated distribution
mainPanel(
plotlyOutput("distPlot", width=1024, height=600)
)
))
#enddivregion
--- server.R
#divregion
補足説明:ui.R で取得した情報は、input$Prefecture で受け...
renderPlotly の中で使用しないとエラーになる。実際、受け取...
を更新するのは、renderPlotly の中だけである。
library(shiny)
library(plotly)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
Japan <- read.csv("https://covid19.mhlw.go.jp/publi...
Japan$Date <- as.Date(Japan$Date)
output$distPlot <- renderPlotly({
prefec <- input$Prefecture
Pref <- data.frame(date=as.Date(Japan$Date[Ja...
num=Japan$Newly.confirmed.cas...
runmin5 <- runningmean(Pref$date, Pref$num, 5)
runmin7 <- runningmean(Pref$date, Pref$num, 7)
runmin9 <- runningmean(Pref$date, Pref$num, 9)
p <- plot_ly( Pref, x=Pref$date, y=Pref$...
p <- add_trace(p, runmin5, x=runmin5$x, y=runmi...
p <- add_trace(p, runmin9, x=runmin9$x, y=runmi...
p <- add_trace(p, runmin7, x=runmin7$x, y=runmi...
})
})
### 日付のリストを作る関数(昨日まで)
dayslist <- function( zeroday = "2020-01-16", lengthdays...
# lenday <- 24*3600
# days <- as.character(as.POSIXlt(zeroday)+(0:lengthd...
days <- as.character(as.Date(zeroday)+(0:lengthdays))
return(days)
}
### 移動平均を求める関数
### 平均を計算するデータの個数は奇数である必要がある。
runningmean <- function(x,y,num=7){
result <- rep(0, length(y)-(num-1))
for( i in 1:num){
result <- result + y[i:(length(y)-(num-i))]
}
return (data.frame(x=x[(num+1)/2 + 1:(length(y)-(num...
}
#enddivregion
~
-- サイドバーがない例:神奈川県庁のサイトから新規感染者数...
--- [[実際のサイト>https://robo.mydns.jp:3838/COVID19/Kan...
--- ui.R~
#divregion
library(shiny)
library(plotly)
### サイドバーなしの場合
shinyUI(fluidPage(
titlePanel("COVID19 Kanagawa Infection"),
plotlyOutput("distPlot", width=1024, height=600)
))
#enddivregion
~
--- server.R
#divregion
library(shiny)
library(plotly)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
Kanagawa <- read.csv("https://www.pref.kanagawa.jp/o...
datelist <- dayslist()
datan <- NULL
for( datedata in datelist )
{
datan <- c(datan, length(Kanagawa$発表日[Kanagaw...
}
DailyData <- data.frame( date=as.Date(datelist), num...
runmin5 <- runningmean(DailyData$date, DailyData$nu...
runmin7 <- runningmean(DailyData$date, DailyData$nu...
runmin9 <- runningmean(DailyData$date, DailyData$nu...
output$distPlot <- renderPlotly({
p <- plot_ly( DailyData, x=DailyData$date, y=...
p <- add_trace(p, runmin5, x=runmin5$x, y=...
p <- add_trace(p, runmin9, x=runmin9$x, y=...
p <- add_trace(p, runmin7, x=runmin7$x, y=...
})
})
### 日付のリストを作る関数(昨日まで)
dayslist <- function( zeroday = "2020-01-16", lengthdays...
days <- as.character(as.Date(zeroday)+(0:lengthdays))
return(days)
}
### 移動平均を求める関数
runningmean <- function(x,y,num=7){
result <- rep(0, length(y)-(num-1))
for( i in 1:num){
result <- result + y[i:(length(y)-(num-i))]
}
return (data.frame(x=x[(num+1)/2 + 1:(length(y)-(num...
}
#enddivregion
終了行:
#topicpath
** shiny と plotly [#x52817d9]
- 目的~
何をやりたいか。それは、プロダクトに語らせることにする。~
~
- サンプル~
shiny と plotly を使ったページ~
-- [[COVID19 の新規感染者数の推移>https://robo.mydns.jp:3...
--- shiny の働き~
このページにアクセスすると厚労省が提供するCSVファイルがダ...
&color(red){実行時にデータが更新};されることがひとつのポ...
表示する都道府県を&color(red){選択};できる。~
~
--- plotly の働き~
単にグラフを表示するだけでなく、グラフの&color(red){表示...
といった操作ができ、&color(red){マウスオーバーで値が表};...
~
** インスール [#dfef6629]
- 環境
-- Debian Buster
-- R 3.5.2
- 手順
++ shiny パッケージ
sudo su - -c "R -e \"install.packages('shiny', repos='ht...
++ shiny server
sudo apt-get install gdebi-core
wget https://download3.rstudio.org/ubuntu-14.04/x86_64/s...
sudo gdebi shiny-server-1.5.16.958-amd64.deb
++ テスト~
--- http://127.0.0.1:3838/ あるいは、http://インストール...
--- http://127.0.0.1:3838/sample-apps/hello/
~
- 参考URL
-- [[Download Shiny Server for Ubuntu 16.04 or later>http...
** デプロイ(作ったものをウエブで公開) [#ba745620]
- 設置方法~
ui.R と server.R を /srv/shiny-server/ 以下に作ったディレ...
~
- アクセス方法~
http://127.0.0.1:3838/ディレクトリ名 でアクセスする。外部...
~
** サンプルプログラム [#sfcad75a]
-- サイドバーがある例:厚生労働省のサイトから新規感染者数...
--- [[実際のサイト>https://robo.mydns.jp:3838/COVID19/Jap...
--- ui.R~
#divregion
library(shiny)
library(plotly)
#### Define UI for application that plots random distrib...
shinyUI(pageWithSidebar(
## Application title
headerPanel("COVID19 Infection"),
sidebarPanel( width=2,
selectInput("Prefecture",
"Choose a prefecture:",
choices=list("ALL", "Tokyo", "Kanaga...
),
## Show a plot of the generated distribution
mainPanel(
plotlyOutput("distPlot", width=1024, height=600)
)
))
#enddivregion
--- server.R
#divregion
補足説明:ui.R で取得した情報は、input$Prefecture で受け...
renderPlotly の中で使用しないとエラーになる。実際、受け取...
を更新するのは、renderPlotly の中だけである。
library(shiny)
library(plotly)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
Japan <- read.csv("https://covid19.mhlw.go.jp/publi...
Japan$Date <- as.Date(Japan$Date)
output$distPlot <- renderPlotly({
prefec <- input$Prefecture
Pref <- data.frame(date=as.Date(Japan$Date[Ja...
num=Japan$Newly.confirmed.cas...
runmin5 <- runningmean(Pref$date, Pref$num, 5)
runmin7 <- runningmean(Pref$date, Pref$num, 7)
runmin9 <- runningmean(Pref$date, Pref$num, 9)
p <- plot_ly( Pref, x=Pref$date, y=Pref$...
p <- add_trace(p, runmin5, x=runmin5$x, y=runmi...
p <- add_trace(p, runmin9, x=runmin9$x, y=runmi...
p <- add_trace(p, runmin7, x=runmin7$x, y=runmi...
})
})
### 日付のリストを作る関数(昨日まで)
dayslist <- function( zeroday = "2020-01-16", lengthdays...
# lenday <- 24*3600
# days <- as.character(as.POSIXlt(zeroday)+(0:lengthd...
days <- as.character(as.Date(zeroday)+(0:lengthdays))
return(days)
}
### 移動平均を求める関数
### 平均を計算するデータの個数は奇数である必要がある。
runningmean <- function(x,y,num=7){
result <- rep(0, length(y)-(num-1))
for( i in 1:num){
result <- result + y[i:(length(y)-(num-i))]
}
return (data.frame(x=x[(num+1)/2 + 1:(length(y)-(num...
}
#enddivregion
~
-- サイドバーがない例:神奈川県庁のサイトから新規感染者数...
--- [[実際のサイト>https://robo.mydns.jp:3838/COVID19/Kan...
--- ui.R~
#divregion
library(shiny)
library(plotly)
### サイドバーなしの場合
shinyUI(fluidPage(
titlePanel("COVID19 Kanagawa Infection"),
plotlyOutput("distPlot", width=1024, height=600)
))
#enddivregion
~
--- server.R
#divregion
library(shiny)
library(plotly)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
Kanagawa <- read.csv("https://www.pref.kanagawa.jp/o...
datelist <- dayslist()
datan <- NULL
for( datedata in datelist )
{
datan <- c(datan, length(Kanagawa$発表日[Kanagaw...
}
DailyData <- data.frame( date=as.Date(datelist), num...
runmin5 <- runningmean(DailyData$date, DailyData$nu...
runmin7 <- runningmean(DailyData$date, DailyData$nu...
runmin9 <- runningmean(DailyData$date, DailyData$nu...
output$distPlot <- renderPlotly({
p <- plot_ly( DailyData, x=DailyData$date, y=...
p <- add_trace(p, runmin5, x=runmin5$x, y=...
p <- add_trace(p, runmin9, x=runmin9$x, y=...
p <- add_trace(p, runmin7, x=runmin7$x, y=...
})
})
### 日付のリストを作る関数(昨日まで)
dayslist <- function( zeroday = "2020-01-16", lengthdays...
days <- as.character(as.Date(zeroday)+(0:lengthdays))
return(days)
}
### 移動平均を求める関数
runningmean <- function(x,y,num=7){
result <- rep(0, length(y)-(num-1))
for( i in 1:num){
result <- result + y[i:(length(y)-(num-i))]
}
return (data.frame(x=x[(num+1)/2 + 1:(length(y)-(num...
}
#enddivregion
ページ名: