使用 selectinput 和 reactive plot 闪亮的问题
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 对我的问题的评论,我的情节现在可以了,只需将 plot
行中的 iris
更改为 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)
})
})
我想显示 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 对我的问题的评论,我的情节现在可以了,只需将 plot
行中的 iris
更改为 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)
})
})