在 R Shiny 中将输入链接到我的绘图输出时遇到问题

Having trouble linking the input to my plot output in Rshiny

我正在创建一个带有两个选项卡的 Rshiny。数据是一个学生列表,plots/tables是通过在下拉列表中输入年级选择来过滤的。我在第一个选项卡上的 table 工作正常,但我试图将第二个选项卡上的最后两个图连接到输入的所有操作都不起作用。现在我把它带到了只显示总计而不使用等级输入过滤器的地方。谁能详细说明如何将我的输入连接到两个输出图?我会把我的代码放在下面

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)

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

试试这个

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