LoginSignup
3
2

More than 5 years have passed since last update.

shinyの出力にはoutputのリスト名でidが振られている

Last updated at Posted at 2016-02-10

タイトルの通りなんですけども、
shinyを使ってレンダリングしているオブジェクトには
どれもidが振られています。
そして、id名はouptut$hogehogeという風に指定した
リストのattribute名がidとして振られます。

試しに、shinyのチュートリアルコードを走らせてみましょう

# こっちがserver.R
library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  # Expression that generates a histogram. The expression is
  # wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should re-execute automatically
  #     when inputs change
  #  2) Its output type is a plot

  output$distPlot <- renderPlot({
    x    <- faithful[, 2]  # Old Faithful Geyser data
    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')
  })
})
# こっちがUI.R
library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the 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")
    )
  )
))

で、アプリケーションを走らせて、プロットの要素を見てみると

<div id="distPlot" class="shiny-plot-output shiny-bound-output" style="width: 100% ; height: 400px"><img src="data:image/png....." width="610" height="400"></div>

みたいに出力されます。
shinyで指定した出力output$distPlotが、divのidに同じ名前でついてますね。

ということは、このidに対してCSSでお化粧をさせる事なんかができますね!
あ、勿論 classがshiny-plot-outputとかで出てくるので、
プロットはこういうお化粧、テーブルはこう…という指定もできますが…

bootstrapのまんまなUIじゃヤダ!カスタム開発したBIっぽく魅せたい!という時に効果的ですね。

ちなみに、cssなんかは必ずserver.RとUI.Rが置いてある階層に
wwwという名前のディレクトリをつくっておいて、そこへ放り込めばOKです。
なお、このディレクトリ名はshinyの仕様上、必ずこの名前じゃなきゃダメらしいです。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2