如何在Shiny-R代码上正确使用checkboxInput

时间:2015-02-09 10:49:20

标签: r ggplot2 shiny

我有一个工作的应用程序,我想用checkboxInput增强。

1>以下是数据样本:

     StudentID StudentGender GradeName    TermName MeasurementScaleName TestPercentile GoalRITScore1 GoalRITScore2 GoalRITScore3 GoalRITScore4
1         1374             M         3   Fall 2009              Reading             32           188           181           179            NA
50        1297             F         8   Fall 2009       Language Usage             48           224           214           209           228
101       1608             F         8   Fall 2009          Mathematics             40           225           210           211           244
1500      1286             M         1   Fall 2011       Language Usage             NA           218           225           238           221
2345      1196             F         8   Fall 2012       Language Usage             78           230           227           239           223
5498      1376             F         3 Spring 2010              Reading             24           188           194           185            NA
8954       486             M         2 Spring 2014              Reading              2           146           152           174            NA
9000       577             F         2 Spring 2014              Reading             71           196           189           207            NA
     GoalRITScore5 GoalRITScore6
1               NA            NA
50              NA            NA
101            233           227
1500            NA            NA
2345            NA            NA
5498            NA            NA
8954            NA            NA
9000            NA            NA

2 - ;这是工作脚本的一部分 闪亮的用户界面

library(shiny)
shinyUI(navbarPage("MAP results",

  tabPanel("Summaries",

          sidebarLayout(

            sidebarPanel(
              selectInput("testname",           
                          "Select the test to visualize",
                          levels(mapdata$MeasurementScaleName)),
              selectInput("termname",         
                           "Select the term the test was taken",
                           levels(mapdata$TermName)),
              selectInput("ritorpercent",
                          "Display RIT scores or percentiles",
                          choices = c("RIT Scores", "Percentiles")),
              checkboxInput("gender", "Display Gender differences"),
              ),

          mainPanel(
            plotOutput("mapgraph")
            )
          )
        ),

  tabPanel("Growth visualizations")
        )
)

Server.R脚本的一部分。

Server.R

library(shiny)
library(dplyr)
library(tidyr)
library(ggplot2)

mapdata <- read.csv("MAP data raw.csv")

shinyServer(function(input, output) {

  output$mapgraph <- renderPlot({

    graph1RIT <- reactive (mapdata %>% 
                             filter(TermName == input$termname, MeasurementScaleName == input$testname) %>% 
                             group_by(GradeName) %>% 
                             summarise(meanPer = mean(TestPercentile)))
    ggplot(graph1RIT(), aes(as.factor(GradeName), meanPer, fill = as.factor(GradeName))) +
      geom_bar(stat="identity") + 
      #coord_cartesian(ylim = c(150, 250)) + 
      labs(x = "Grade Level", y = "Mean RIT Percentile") + 
      guides(fill = FALSE)

  })
})

现在我想使用我的checkboxInput(“性别”)来制作相同的条形图但具有性别隔离。 ......我想我可以把它添加到server.r

if(input$gender) {
      graph3RIT <- reactive (mapdata %>% 
                               filter(TermName == input$termname, MeasurementScaleName == input$testname) %>% 
                               group_by(GradeName, StudentGender) %>% 
                               summarise(meanPer = mean(TestPercentile)))
      ggplot(graph3RIT(), aes(as.factor(GradeName), meanPer, fill = as.factor(StudentGender))) +
        geom_bar(stat="identity", position = "dodge") + 
        labs(x = "Grade Level", y = "Mean RIT Percentile")
    }

但如果我这样做,那么第一张图就不再出现了。我试图在showmeshiny网站上查找类似情况,但我找到的所有代码都没有代码可用。

有关如何使用该复选框的任何指导,以更改图表

1 个答案:

答案 0 :(得分:0)

弗兰基自己找到了答案。他在评论中写道

  

好的......谢谢NicE。在此期间我确实弄明白了。我只需要   要做的是把我的代码放在else {}之间。然后它工作得很好。 -   弗兰基2月10日&15; 15日9:43