Shinyの基礎

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel(h1("あいりすぷろったー ver.1.0",align="center")),
  br(),
  h4("@moyahima",align="center"),
  br(),
  h3(textOutput("hogeText"),align='center'),
  br(),
  fluidRow(
    column(9,offset=2,mainPanel(plotOutput("irisPlot",height=600,width=1000)))
  ),  
  
 fluidRow(
    column(3,offset=3,
      selectInput("x_var", label = h3("X軸"), 
          choices = c("Sepal.Length", "Sepal.Width", "Petal.Length","Petal.Width"), selected = "Sepal.Length")),
    
    column(3,offset=1,
      selectInput("y_var", label = h3("Y軸"), 
          choices = c("Sepal.Length", "Sepal.Width", "Petal.Length","Petal.Width"), selected = "Sepal.Width"))
  )
))

server.R

library(ggplot2)
shinyServer(function(input,output){

 datasetInputX <- reactive({
    switch(input$x_var,
          "Sepal.Length" = "Sepal.Length",
           "Sepal.Width" = "Sepal.Width",
           "Petal.Length" = "Petal.Length",
           "Petal.Width" = "Petal.Width")
  })  
 datasetInputY <- reactive({
    switch(input$y_var,
           "Sepal.Length" = "Sepal.Length",
           "Sepal.Width" = "Sepal.Width",
           "Petal.Length" = "Petal.Length",
           "Petal.Width" = "Petal.Width")
  })  

  output$hogeText <- renderText({"hogehoge"})

  output$irisPlot <- renderPlot({
    x_var <- datasetInputX() 
    y_var <- datasetInputY()
    p <- ggplot(iris,aes_string(x=x_var,y=y_var,color="Species"))+geom_point()+theme_bw() 
    print(p)
  })  
})