6

R Shiny アプリケーションに関する一般的な質問があります。エンドユーザーがドロップダウン メニュー ( selectInput) から選択できる ID のリストがあり、この ID に基づいて対応するプロットを作成したいと考えています。つまり、選択した変数は plot 関数の引数として渡され、別の ID を選択するたびにプロットも変更されます。これがシャイニーで実行可能かどうかを知ることはできますか? 誰かが同様の問題に関する実用的な例を提供していただければ幸いです。ありがとうございました!

4

1 に答える 1

15

これは、ドロップダウンを引数として使用したサンプルの実例です。このプログラムの出力はhttp://glimmer.rstudio.com/bishwamitrad/ggplot2browser/で見ることができます:

ui.R

library(shiny)
library(ggplot2)

## Define UI for miles per gallon application

dataset <- diamonds

title <- "Diamonds data Analysis"

## Define UI for application that plots random distributions

shinyUI(pageWithSidebar(

  ## Application title
  headerPanel(title),

  ## Sidebar with a slider input for number of observations

  sidebarPanel (

    sliderInput('sampleSize','Sample Size', min=1, max=nrow(dataset),
                value=min(1000,nrow(dataset)),
                step=500,
                round=0),

    selectInput('x','X',names(dataset)),
    selectInput('y','Y',names(dataset),
                names(dataset)[[2]]),

    selectInput('color','Color',c('None',names(dataset))),

    selectInput('shape','Shape',c('None',names(dataset))),

    checkboxInput('jitter','Jitter'),
    checkboxInput('smooth','Smooth'),

    selectInput('facet_col','Facet Column',
                c(None='.',names(dataset))),

    selectInput('facet_row','Facet Row',
                c(None='.',names(dataset)))

  ),

  ## Show a plot of the generated distribution

  mainPanel(plotOutput('plot',height="700px"))


)

)

サーバー.R

library(shiny)
library(ggplot2)

## Define server logic required to generate and plot a random distribution
shinyServer(function(input,output) {

  dataset <- reactive(function(){
    diamonds[sample(nrow(diamonds),input$sampleSize),]
  })

output$plot <- renderPlot(function(){

  p <- ggplot(dataset(),aes_string(x=input$x, y=input$y))+geom_point()

  if(input$color != 'None')
    p <- p + aes_string(color=input$color)

  if (input$shape != 'None')
    p <- p + aes_string(shape=input$shape)

  facets <- paste(input$facet_row, '~', input$facet_col)

  if (facets != '. ~ .')
    p <- p + facet_grid(facets)

  if (input$jitter)
    p <- p + geom_jitter()

  if (input$smooth)
    p <- p + geom_smooth()


  print(p)

})


})
于 2013-07-14T01:23:34.983 に答える