繁体   English   中英

selectinput和反应式图闪闪发光的问题

[英]questions in shiny with selectinput and the reactive plot

我想显示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)
   })
})

感谢NicE对我的问题的评论,我的绘图现在可以了,只需将iris更改为plot线上的data

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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