r语言 - 在Rshiny中将输入链接到我的plot输出时遇到麻烦



我正在创建一个带有两个选项卡的Rshiny。数据是一个学生列表,通过在下拉列表中输入年级选择来筛选图/表。我在标签1上的表工作得很好,但我所做的一切都是为了将第二个标签上的最后两个图连接到输入,这是不工作的。现在我有它的地方,它只是显示总数,而不使用等级的输入过滤器。谁能详细说明如何连接我的输入到两个输出图?我把我的代码放在

下面
library(shiny)
library(tidyverse)
students = read.csv("C:/Users/j062d273/Downloads/RShiny Grade EX.csv",
stringsAsFactors = FALSE)

# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
headerPanel("Student Data"),
# tabs set up
tabsetPanel(
tabPanel(title = "Students by Grade",
mainPanel(width = 12, align = "center",
selectInput("grade", "Select Grade", choices=unique(sort(students$Grade, 
decreasing = FALSE)), selected = 1),
submitButton("Go"),
tags$h3("Student List"),
div(style = "border:1px black solid;width:80%",tableOutput("student_list"))
)),
tabPanel(title = "Trends by Grade",
mainPanel(width = 12,align = "center",
div(style = "float:left;width:36%;",plotOutput("male_fem_dist")),
div(style = "float:right;width:64%;",plotOutput("ethnicity_plot")))
)))

# Define server logic required to draw plot
server <- function(input, output) {
output$student_list <- renderTable({
gradefilter <- subset(students, students$Grade == input$grade)
})

output$male_fem_dist <- renderPlot({
ggplot(students, aes(x=Gender)) +
geom_bar(fill = "blue", color = "red") +
ggtitle("Gender Count by Selected Grade")
})

output$ethnicity_plot <- renderPlot({
ggplot(students, aes(x=Ethnicity)) +
geom_bar(fill = "red", color = "blue") +
ggtitle("Ethnicity Count by Selected Grade")
})
}
# Run the application 
shinyApp(ui = ui, server = server)

先过滤数据集,然后在表格和绘图中使用。

试试这个

server <- function(input, output) {
gradefilter <- reactive({subset(students, students$Grade == input$grade)})

output$student_list <- renderTable({gradefilter()})

output$male_fem_dist <- renderPlot({
ggplot(gradefilter(), aes(x=Gender)) +
geom_bar(fill = "blue", color = "red") +
ggtitle("Gender Count by Selected Grade")
})

output$ethnicity_plot <- renderPlot({
ggplot(gradefilter(), aes(x=Ethnicity)) +
geom_bar(fill = "red", color = "blue") +
ggtitle("Ethnicity Count by Selected Grade")
})
}

最新更新