将函数作为参数传递给Shiny App中的Filter dplyr

时间:2019-05-01 07:15:34

标签: r shiny

大家好,这是有关我的Shiny仪表板的一般问题 我正在尝试在dplyr下传递函数-过滤器

我收到错误消息 警告:获取错误:找不到“功能”模式的对象“ f”

library(shiny)
require(shinydashboard)
library(dplyr)
library(shinyWidgets)

Quarter <- c("Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q1","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q2","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q3","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy17Q4","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q1","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q2","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q3","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy18Q4","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1","Fy19Q1")
QuarterInNum <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9)
OperatingGroupDesc <- ("Communications, Media & Technology","Communications, Media & Technology","Communications, Media & Technology","Financial Services","Financial Services","Financial Services","Health & Public Service","Health & Public Service","Health & Public Service","Products","Products","Products","Resources","Resources","Resources","Communications, Media & Technology","Communications, Media & Technology","Communications, Media & Technology","Financial Services","Financial Services","Financial Services","Health & Public Service","Health & Public Service","Health & Public Service","Products","Products","Products","Resources","Resources","Resources","Communications, Media & Technology","Communications, Media & Technology","Communications, Media & Technology","Financial Services","Financial Services","Financial Services","Health & Public Service","Health & Public Service","Health & Public Service","Products","Products","Products","Resources","Resources","Resources","Communications, Media & Technology","Communications, Media & Technology","Communications, Media & Technology","Financial Services","Financial Services","Financial Services","Health & Public Service","Health & Public Service","Health & Public Service","Products","Products","Products","Resources","Resources","Resources","Communications, Media & Technology","Communications, Media & Technology","Communications, Media & Technology","Financial Services","Financial Services","Financial Services","Health & Public Service","Health & Public Service","Health & Public Service","Products","Products","Products","Resources","Resources","Resources","Communications, Media & Technology","Communications, Media & Technology","Communications, Media & Technology","Financial Services","Financial Services","Financial Services","Health & Public Service","Health & Public Service","Health & Public Service","Products","Products","Products","Resources","Resources","Resources","Communications, Media & Technology","Communications, Media & Technology","Communications, Media & Technology","Financial Services","Financial Services","Financial Services","Health & Public Service","Health & Public Service","Health & Public Service","Products","Products","Products","Resources","Resources","Resources","Communications, Media & Technology","Communications, Media & Technology","Communications, Media & Technology","Financial Services","Financial Services","Financial Services","Health & Public Service","Health & Public Service","Health & Public Service","Products","Products","Products","Resources","Resources","Resources","Communications, Media & Technology","Communications, Media & Technology","Communications, Media & Technology","Financial Services","Financial Services","Financial Services","Health & Public Service","Health & Public Service","Health & Public Service","Products","Products","Products","Resources","Resources","Resources")
RiskTierDesc <- c("Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal","Above Normal","High","Normal")
Freq <- c(64,13,225,86,11,273,52,6,125,158,17,325,87,5,213,97,38,244,106,12,343,52,4,106,131,27,358,84,6,226,118,16,251,110,15,297,56,7,118,191,18,385,81,13,233,97,13,255,102,8,281,57,5,95,144,29,367,71,6,197,88,7,249,91,10,313,66,4,108,168,18,359,68,7,218,105,4,329,98,8,428,52,5,116,164,25,468,100,4,300,144,9,303,122,15,435,65,2,144,199,26,440,68,9,269,118,8,297,100,8,354,71,7,134,195,22,374,72,11,292,68,3,352,106,10,369,69,2,135,142,17,459,93,9,302)
FreqPercent <- c(21,4,75,23,3,74,28,3,68,32,3,65,29,2,70,26,10,64,23,3,74,32,2,65,25,5,69,27,2,72,31,4,65,26,4,70,31,4,65,32,3,65,25,4,71,27,4,70,26,2,72,36,3,61,27,5,68,26,2,72,26,2,72,22,2,76,37,2,61,31,3,66,23,2,74,24,1,75,18,1,80,30,3,67,25,4,71,25,1,74,32,2,66,21,3,76,31,1,68,30,4,66,20,3,78,28,2,70,22,2,77,33,3,63,33,4,63,19,3,78,16,1,83,22,2,76,33,1,66,23,3,74,23,2,75)

Data <- data.frame(Quarter,QuarterInNum,OperatingGroupDesc,RiskTierDesc,Freq,FreqPercent)

UI PART

ui <- dashboardPage(
  dashboardHeader(title = "Basic Dashboard"),

  dashboardSidebar(
    sidebarMenu( selectInput("app", 
                             "Select:", 
                             choices = c("","Risk tier by Count","Risk tier by TR","Risk tier by TCNR"), 
                             selected = NULL, 
                             multiple = F,
                             selectize = F,
                             width = NULL,
                             size = NULL)),
    sliderTextInput("Quarter","Select Quarter:",
                    choices =  c("Fy17Q1","Fy17Q2","Fy17Q3","Fy17Q4","Fy18Q1","Fy18Q2"),
                    selected =  c("Fy17Q2","Fy18Q1")),
    selectInput(inputId="variable", label="OG to show:", 
                choices=c("All","CMT","FS","HPS","PRD","RES"), selected = NULL, multiple = F,selectize = F),    

dashboardBody(
    fluidRow(  

      tableOutput('table')

    )))

服务器部件

server <- function(input, output) {

XX <- reactive(input$app)  
  YY <- reactive(input$variable)


  dataInput <- reactive({
    #dataframe <- GroupFreq
    qfrom <- Data$QuarterInNum[match(input$Quarter[1], as.character(Data$Quarter))]
    qto <- Data$QuarterInNum[match(input$Quarter[2], as.character(Data$Quarter))]
    test <- Data[Data$QuarterInNum %in% seq(from=qfrom,to=qto),]
    print(test)
    #test()
  })



}

KK1 <- reactive({BB <- if((XX() != "") && (YY() != "All")){Filter(dataInput(),((OperatingGroupDesc == as.character(YY()))))}
AA1 <- data.frame(BB1)
return(AA1)})

output$table <- renderTable(KK1()) 
#output$table <- renderTable(dataInput()) # to test the dataInput()
  #output$table <- renderTable(Data) # to test if i am getting the table

} 

runApp(shinyApp(ui, server),launch.browser = TRUE)

需要根据从选择输入中选择的OG进行表格输出

1 个答案:

答案 0 :(得分:0)

您的server函数完全不正确。请参阅本教程(https://shiny.rstudio.com/articles/basics.html)说明了其结构。

第二,函数Filter(带有大写字母F)不是dplyr过滤器(使用小写字母f),并且它的第一个参数很可能是NULL,而不是data.frame。

您的f(在selectInput调用中)不是“ false”。简写布尔是大写的(FT),应避免使用,因为可以重新定义它们