Shiny
完全に理解した

@Med_KU

20181201
Japan.R#6
LINE株式会社 グローバルオフィス

今日の話

Shiny

Interact. Analyze. Communicate.

Let users interact with your data and your analysis. And do it all with R.

Gallary

htmlwidgets

reveal.js にShiny を組み込めばいいのでは?

revealjs_presentation is not compatible with runtime 'shiny'

そういうことらしい

Shiny 使いたい

シミュレーションしたかった

薬剤のコンパートメントモデル

薬を投与していい感じの濃度を維持したい

微分方程式を解けば解析的に解は求まる

使い勝手が悪い?

パラメータをいろいろ変えたい

R を持ってない人にも使ってもらいたい

Shiny 使ったら ?

Shiny の作り方

app.R 内にuiserver を用意する

個別にui.Rserver.R ファイルを作ってもよい

library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
   # Application title
   titlePanel("Old Faithful Geyser Data"),
   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(sliderInput("bins", "Number of bins:", min=1, max=50, value=30)),
      # Show a plot of the generated distribution
      mainPanel(plotOutput("distPlot"))
   )
)
# Define server logic required to draw a histogram
server <- function(input, output) {
   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out=input$bins+1)
      # draw the histogram with the specified number of bins
      hist(x, breaks=bins, col='darkgray', border='white')
   })
}
# Run the application 
shinyApp(ui = ui, server = server)

図や表を表示できる

library(shiny)
library(googleVis)
library(ggplot2)
library(plotly)
apropos("^render")
[1] "renderDataTable" "renderGvis"      "renderImage"     "renderPlot"     
[5] "renderPlotly"    "renderPrint"     "renderTable"     "renderText"     
[9] "renderUI"       

googleVis

library(googleVis)

dat <- data.frame(year=seq(tsp(Nile)[1], tsp(Nile)[2]), flow=Nile@.Data)
opt <- list(lineWidth=3, width=800, height=300, legend="none",
            title="Nile flow", vAxis="{title: 'Flow'}", hAxis="{title:'Year'}")
g <- gvisLineChart(dat, xvar="year", yvar="flow", options=opt)
LineChartID25f85dc5130

ブログに直接貼り付ける

Rmd ではresults="asis" でhtml として表示される

日本の出生数 calendarchart

CalendarID25f82a560f68

土日祝は分娩誘発・帝王切開をしない

Google が提供するクラスとメソッドから逸脱する修飾は諦める

googlevis の geochart

GeoChartID25f87dd40f33

時系列シミュレーションを見せたかった

時系列シミュレーションを見せたかった

20151205japanr from Med_KU

heatmap をGIF的に見せる

gganimate: gifski がubuntu でうまくいかないのでボツ

  • aes

plotly: なんかうまくいった(?)

  • ~

plotly

plt <- plot_ly(dataframe, x=~xv, y=~yv, z=~zv, type="heatmap", frame=~frame,
               width=560, height=560) %>%
       layout( ... ) %>%
       animation_opts(frame=300, transition=300, redraw =FALSE)

output$plotly <- renderPlotly( ... )

公開

5つよりたくさん作らないならばshinyapps.io

Rstudio でワンクリックかrsconnect::deployApp

薬剤(フェンタニル)シミュレーション \(\to\) shiny

ワクチンと感染症シミュレーション \(\to\) shiny

まとめ

インタラクティブなパラメータ操作ができる

プロット結果をそのままブログに使える