按性别和频率过滤并返回条件结果

时间:2015-10-16 08:57:32

标签: r conditional shiny dplyr

我有一个使用数据框的Shiny应用程序。 数据框如下所示:

Name  Gender  Frequency
John       M         45
Mary       F         32

我的UI.R文件包含以下小部件:

radioButtons("sex", "Sex:", c("Female", "Male"),
             selected = c("Female")),
             br(),
             textInput("searchText", "Search: ", "")

我的server.R文件有:

# reactive function
  getReactionDTResult <- reactive({
    return(list(
      text = input$searchText,
      sex = input$sex
    ))
  })

计算功能:

# Plot results of dataframe search
getDTResult <- function(df, reaction){
  maintable <-df[[1]]
  sex = reaction$sex
  name = reaction$text

  if (sex == "Female"){
    sex = "F"
  }
  else {sex = "M"}

    g1 <- maintable %>% filter(Name == name, Sex == sex)

  if(g1$Frequency == 0)

  { text = "Not found" }

  else if(g1$Frequency <= 3)

  {text = "There are less than 3 persons with this name"}

  else

  {text == "There are g1$Frequency men/women with this name"}

  return(text)
}

输出功能

output$text <- renderText({print(getDTResult(localFrame, getReactionDTResult()))})

现在函数应该根据输入的名称和性别搜索数据帧,然后返回条件结果 如果0文本“没有...” 如果少于3“,则少于3 ..” 否则“有x人”

我无法使我的功能正常工作,因为它给了我错误:

  

if(g1 $ Frequency == 0){:参数长度为零

时出错

请帮忙

0 个答案:

没有答案