我希望显示不同数量的图表,具体取决于下拉菜单中选择的输入类别。我无法看到这样做的方法,因为R Shiny似乎只允许一个输出。我在下面概述了一个非常简单的例子。我意识到直方图是不必要的,可能会在一个图表中显示;但是我正在做的实际例子更复杂,需要多个图表。
gender <- c("male","male","female","female","male","male","male")
sports <-c("running","swimming","basketball","fishing","karate","hockey","gymnastics")
regularplayer <-c(.2,.4,.3,.5,.1,.1,.1)
casualplayer <-c(.2,.3,.6,.2,.2,.2,.2)
nonplayer <-c(.6,.3,.1,.3,.7,.7,.7)
sports <-as.data.frame(cbind(gender,sports,regularplayer,casualplayer,nonplayer))
ui <- fluidPage(
selectInput("gender","Select Gender",c("Male","Female"))
)
server <-function(input, output) {
output$gender <- renderPlot({
if (input$gender == "Male") {
# Need to create 5 histograms, one for each sport by% played
}
else if (input$gender == "Female") {
# Need to create 2 histograms, one for each sport by% played
}
#return graphs as single output, either 5 or 2 depending on gender selected
})
}
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
这是一种可行的方法:
global.R 保存数据。将它放在与ui.R和server.R相同的文件夹中 不要担心融化功能的警告信息,因为不是所有的运动都存在于女性的情况下。
library(reshape2)
gender <- c("male","male","female","female","male","male","male")
sports <-c("running","swimming","basketball","fishing","karate","hockey","gymnastics")
regularplayer <-c(.2,.4,.3,.5,.1,.1,.1)
casualplayer <-c(.2,.3,.6,.2,.2,.2,.2)
nonplayer <-c(.6,.3,.1,.3,.7,.7,.7)
sports <-as.data.frame(cbind(gender,sports,regularplayer,casualplayer,nonplayer))
sports_melted <- melt(sports, id.vars = c("gender", "sports"), variable.name = "player_type", value.name = "player_percent")
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
selectInput("gender","Select Gender",c("Male","Female"))
),
mainPanel(
plotOutput("gender")
)
)
))
server.R
library(shiny)
library(dplyr)
library(ggplot2)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
output$gender <- renderPlot({
if (input$gender == "Male") {
male_data <- filter(sports_melted, gender == "male")
gg <- ggplot(male_data, aes(x=player_type, y = player_percent))
gg <- gg + geom_bar(stat = "identity")
gg <- gg + facet_grid(.~sports)
# Need to create 5 histograms, one for each sport by% played
}
else if (input$gender == "Female") {
female_data <- filter(sports_melted, gender == "female")
gg <- ggplot(female_data, aes(x=player_type, y = player_percent))
gg <- gg + geom_bar(stat = "identity")
gg <- gg + facet_grid(.~sports)
}
gg
})
})