简体   繁体   中英

questions in shiny with selectinput and the reactive plot

I want show the reactive scatter plot of different iris species between Sepal.Length and Sepal.Width. But the result shows only one plot, it can't show the reactive result. Could anyone help me? Here are my codes:

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") 
      )
   )
))

server:

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)
   })
})

Thanks NicE comment on my question, my plot is ok now, just change the iris to data in my plot line:

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")))
))

server:

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)
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM