使用闪亮的交互式输入来过滤不起作用的NA值

时间:2018-02-07 21:50:23

标签: r shiny dplyr

我正在使用包含各种变量(性别,居住地,SES)的数据集,并且此数据集的某些案例具有NA值。根据用户输入,我一直在尝试使用dplyr来过滤掉NA个案例,以便它们不会被绘制成图形。但是,filter(!is.na()) r函数似乎不起作用,因为NA值仍然是图形。

我的代码:

gender <- c("Male", "Female", NA)
residency <-c("InsideUS", "OutsideUS", NA)
category <- c("A", "B", NA)
SES <- c("Lower", "Middle", "Upper", NA)
choices <- c("gender", "residency", "SES")
variables <- c("SES")
cat<- c("category")


set.seed(1)
df <- data.frame( gender=as.factor(sample(gender, size=20, replace=TRUE)), 
              residency=as.factor(sample(residency, size=20, replace=TRUE)), 
              category=as.factor(sample(category, size=20, replace=TRUE)), 
              SES=as.factor(sample(SES, size=20, replace=TRUE)))

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "choiceDisplay1",
              label= "Please select an option",
              choices= c("Gender"="gender",
                         "Residency"="residency",
                         "Social Economic Status"="SES"),
              selected="gender")
    ),
    mainPanel(
      verbatimTextOutput("summary")
    )
  )
)

server <- function(input, output) {
  #Target Market
  output$summary <- renderPrint({
    df %>% filter_(!is.na(input$choiceDisplay1)) %>%
      group_by_(.choices=input$choiceDisplay1, .cat=c("category"), 
                 .variables=c("SES")) %>%
      summarize(n=n()) %>%
      ungroup()  %>% complete(.choices, .cat, .variables,
                          fill=list(n=0)) %>%
      group_by(.choices, .cat) %>%
      mutate(perc= n/sum(n))
  })
}

shinyApp(ui,server)

我尝试了filter(is.na(.choices=choiceDisplay1))之类的变体,但收到以下错误:supplied argument name '.choices' does not match 'x'

为什么这个dpylr功能不起作用?

谢谢!

0 个答案:

没有答案