“filter()”输入“…1”有问题。带着闪亮的红色

“filter()”输入“…1”有问题。带着闪亮的红色,r,dplyr,shiny,R,Dplyr,Shiny,我正在尝试构建一个闪亮的应用程序,根据用户输入过滤数据帧,但是,我正在努力使用我创建的函数来完成这项任务,“filter()”input“…1”的错误问题。x输入“…1”的大小必须为9或1,而不是0。保持外观。 我发现了一个类似的问题,但答案没有帮助 这是我的密码。这里还有示例数据的和文件 我非常感谢你的帮助 library(shiny) library(dplyr) library(shinythemes) library(shinydashboard) library(shinyjs) u

我正在尝试构建一个闪亮的应用程序,根据用户输入过滤数据帧,但是,我正在努力使用我创建的函数来完成这项任务,“filter()”input“…1”的错误
问题。x输入“…1”的大小必须为9或1,而不是0。
保持外观。 我发现了一个类似的问题,但答案没有帮助

这是我的密码。这里还有示例数据的和文件

我非常感谢你的帮助

library(shiny)
library(dplyr)
library(shinythemes)
library(shinydashboard)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  theme = shinytheme("flatly"),
  tabsetPanel(
    id = "tabs",
    tabPanel("Portafolio",
             sidebarLayout(
               sidebarPanel(
                 titlePanel("Seleccione las variables deseadas"),
                 uiOutput('fondo'),
                 uiOutput('reg'),
                 uiOutput('seguro'),
                 uiOutput('prod_sap'),
                 hr(),
                 actionButton("addbutton","Añadir")
               ),
               mainPanel(
                 titlePanel("Vista previa del portafolio"),
                 tableOutput('courseTable'),
                 actionButton(inputId = "continue", label = "Cotizar")
               )
             )
    ),
    tabPanel("Cotización",
             tableOutput('envio'))
  )
  )


server <- function(input, output, session) {
  fondo_edo <- reactive ({
    read.csv("E:/Input Fondo-Edo-Reg_example.csv") 
  })
  
  output$fondo <- renderUI({
    times <- input$addbutton
    fondos_todos <- as.vector(unique(fondo_edo()$FONDO))
    div(id= letters[(times %% length(letters)) + 1],
        selectInput("fondo_selec","Fondo:", choices=fondos_todos,selectize = T))    
  })
  
  fondo_edo1 <- reactive({
    subset(fondo_edo(), FONDO %in% input$fondo_selec)
  })
  
  output$reg <- renderUI({
    reg_todos <- as.vector( unique(fondo_edo1()$REGIÓN) )
    selectInput("reg_selec","Región:", choices=reg_todos, selectize = F)    
  })

  output$seguro <- renderUI({
    times <- input$addbutton
    div(id=letters[(times %% length(letters))+1],
        selectInput("seguro_selec","Seguro agricultura protegida:", choices=c("","Cosecha_Esp", "Inversión", "Planta"), selectize = F))    
  })
  
  output$prod_sap <- renderUI({
    times <- input$addbutton
    div(id=letters[(times %% length(letters))+1],
        conditionalPanel("input.seguro_selec == 'Inversión'",
                         selectInput("prod_sap_selec","Nombre producto SAP:", choices= "Tradicional")),
        conditionalPanel("input.seguro_selec != 'Inversión'",
                         selectInput("prod_sap_selec2","Nombre producto SAP:",choices = c("","Establecimiento", "Mantenimiento", "Producción"), selectize = F)))
  })

  values <- reactiveValues()
  values$df <- data.frame("Fondo" = numeric(0), "Región"= numeric(0), "Tipo de práctica"= numeric(0),
                          "Seguro agricultura protegida"= numeric(0))
  
  newEntry <- observe({
    if(input$addbutton > 0) {
      
      newLine <- isolate(c(input$fondo_selec, input$reg_selec,
                           "RIEGO", 
                           ifelse(input$seguro_selec=="Planta", paste0(input$seguro_selec,"/",input$prod_sap_selec2),
                                  input$seguro_selec)))
      isolate(values$df[nrow(values$df) + 1,] <-newLine)
    }
  })
  
  output$courseTable <- renderTable({values$df})
  
  observeEvent(input$continue, {
    updateTabsetPanel(session = session, inputId = "tabs", selected = "Cotización")
  })
  
  cotizacion <- reactive({
    isolate(busca_folios(fondo_edo(),values$df$Fondo, values$df$Región,
                         values$df$Seguro.agricultura.protegida))
  })
  output$envio <- renderTable({cotizacion()})
  
  # cotizacion <- reactiveValues()
  # cotizacion$df <-  busca_folios(fondo_edo(),values$df$Fondo, values$df$Región,
  #                                values$df$Sistema.de.producción, values$df$Seguro.agricultura.protegida)
  # 
  # output$envio <- renderTable({cotizacion$df})
}


runApp(shinyApp(ui,server))


#### Funciones ####

busca_folios <- function(tabla_fondos, fondo, reg, cultivo, seguro){
  historico_folios <- readxl::read_xlsx("E:/historico_example.xlsx")
  
  fn <- tabla_fondos[which(tabla_fondos$FONDO == fondo),]$`CLAVE FONDO`
  fond <- ifelse(nchar(fn)==1,paste0("000",fn),ifelse(nchar(fn)==2, paste0("00",fn),
                                                      ifelse(nchar(fn)==3, paste0("0",fn),fn)))
  rg <-tabla_fondos[which(tabla_fondos$REGIÓN == reg),]$CVE_REGION
  region <- ifelse(nchar(rg)==1, paste0("00",rg),ifelse(nchar(rg)==2,paste0("0",rg),rg))
  
  buscada <<- historico_folios %>% 
    dplyr::filter(Fondo==fond,
                  Región == region, Subramo == seguro)
}
库(闪亮)
图书馆(dplyr)
图书馆(shinythemes)
图书馆(shinydashboard)
图书馆(shinyjs)

ui您的代码有几个问题:

  • 您可以检查
    Fondo==fond
    。但是,该参数称为
    fondo
  • 当您用三个参数调用函数时,函数有四个参数:
    busca_folios(fondo_edo(),值$df$fondo,值$df$regionón,值$df$Seguro.agricultura.protegida)
    。因此,缺少参数
    segura
  • 至少在我的机器上,
    Región==region
    给了我一个错误,我通过将
    Región
    放在反勾“`
  • 在过滤器内部,您可以通过
    ==
    检查是否相等,即使在修复了其他问题之后,这也是您出现错误的原因。在%
  • 固定函数如下所示:

    busca_folios <- function(tabla_fondos, fondo, reg, cultivo, seguro){
      historico_folios <- readxl::read_xlsx("historico_example.xlsx")
      
      fn <- tabla_fondos[which(tabla_fondos$FONDO == fondo),]$`CLAVE FONDO`
      fond <- ifelse(nchar(fn)==1,paste0("000",fn),ifelse(nchar(fn)==2, paste0("00",fn),
                                                          ifelse(nchar(fn)==3, paste0("0",fn),fn)))
      rg <-tabla_fondos[which(tabla_fondos$REGIÓN == reg),]$CVE_REGION
      region <- ifelse(nchar(rg)==1, paste0("00",rg),ifelse(nchar(rg)==2,paste0("0",rg),rg))
      
      buscada <<- historico_folios %>% 
        dplyr::filter(Fondo %in% fondo,
                      Región %in% region, Subramo %in% seguro)
    }
    

    busca\u folios非常感谢!!你帮了我很多,我已经为此挣扎了好几个小时
    
    cotizacion <- reactive({
        isolate(busca_folios(fondo_edo(),values$df$Fondo, values$df$Región,
                             seguro = values$df$Seguro.agricultura.protegida))
      })