选择输入和反应图的闪亮问题

时间:2015-07-06 08:08:23

标签: r shiny

我希望在Sepal.Length和Sepal.Width之间显示不同虹膜种类的反应散点图。但结果只显示了一个图,它无法显示反应结果。谁能帮助我?这是我的代码:

UI:

library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
   # Application title
   titlePanel("Iris data test"),
   # Sidebar with a slider input for the number of bins
   sidebarLayout
   (
      sidebarPanel
      (
      selectInput("Species",label="choice the Species",choices=c("setosa","versicolor","virginica"))
      ),
      mainPanel
      (
      plotOutput("distPlot") 
      )
   )
))

服务器:

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({
      # draw the histogram with the specified number of bins
      data=switch(input$Species,
                  "setosa"=subset(iris,Species=="setosa"),
                  "versicolor"=subset(iris,Species=="versicolor"),
                  "virginica"=subset(iris,Species=="virginica")   
      )
      plot(x=iris$Sepal.Length, y=iris$Sepal.Width)
   })
})

1 个答案:

答案 0 :(得分:0)

感谢NicE对我的问题的评论,我的情节现在没问题,只需将我iris行中的data更改为plot

UI:

library(shiny)
shinyUI(fluidPage(
  # Application title
  titlePanel("Iris data test"),
  # Sidebar with a slider input for the number of bins
  sidebarLayout
  (sidebarPanel
    (
      selectInput(
        "Species",
        label = "choice the Species",
        choices = c("setosa", "versicolor", "virginica")
      )
    ),
    mainPanel
    (plotOutput("distPlot")))
))

服务器:

library(shiny)
shinyServer(function(input, output) {
  output$distPlot <- renderPlot({
    # draw the histogram with the specified number of bins
    data = switch(
      input$Species,
      "setosa" = subset(iris, Species == "setosa"),
      "versicolor" = subset(iris, Species == "versicolor"),
      "virginica" = subset(iris, Species == "virginica")
        )
    plot(x = data$Sepal.Length, y = data$Sepal.Width)
  })
})