Shinyでヒストグラムを表示


library(shiny)
library(ggplot2)

rm(list=ls())

# データを定義
data("iris")
x <- iris
head(x)

# ユーザインタフェースを定義
ui <- fluidPage(
  h1("Iris Histogram"),
  selectInput(inputId = "colname", label = "列名", choices = colnames(x)[-ncol(x)]),
  plotOutput(outputId = "histogram")
)

# サーバの処理(ヒストグラムの作成)を定義
server <- function(input, output, session){
  output$histogram <- renderPlot({
    ggplot(data = x, mapping = aes(x = x[, input$colname])) + geom_histogram() + xlab(input$colname)
  })
}

# アプリを起動
shinyApp(ui = ui, server = server)