【问题标题】:R Shiny error: unused argument but error line is differentR Shiny错误:未使用的参数但错误行不同
【发布时间】:2018-08-29 18:18:53
【问题描述】:

我正在编写一个 Shiny 应用程序,该应用程序根据 Carrier 中的输入选择数据帧并对其执行一些功能。这是我的代码:

library(shiny)
library(ggplot2)
library(dplyr)
library(rlang)
library(shinyWidgets)
library(rstudioapi)

ui <- fluidPage(


  # Give the page a title
  titlePanel("Rate Analysis"),

  # Generate a row with a sidebar
  sidebarLayout(      

    # Define the sidebar with one input
    sidebarPanel(
      selectInput("ProductType","Product Type: (Choose One)",choices=c("t","r")),
      selectInput("inSelect","Carrier: (Choose One)",choices=unique(c(t_rates_names,r_rates_names))),
      selectInput("Category","Category: (Choose One)",choices=c("Age","Term","Band","Class","Gender")),
      sliderInput("discount","% Discount:",min=0,max=100,value=0),
      width=3
    ),

    # Create a spot for the barplot
    mainPanel(
      plotOutput("Minimum_Rates")  
    )

  )
)

server <- function(input, output, session) {
  observe({
    req(input$ProductType)
    x<-sym(input$ProductType)
    updateSelectInput(session,"inSelect",label=paste("test"),choices=eval(parse(text=paste0(x,"_rates_names"))))
  })

  output$Minimum_Rates<-renderPlot({
    req(input$inSelect)
    req(input$Category)
    req(input$discount)
    req(input$ProductType)
    carrier_name<-(input$inSelect)
    carrier<-eval(parse(text=input$inSelect))
    type<-input$ProductType
    category<-sym(input$Category)
    discount<-input$discount
    fee_name=eval(parse(text=paste0(substr(carrier_name,1,nchar(carrier_name)-6),"_fees"))) #gets actual list of fees
    if(type=="t"){
      tMinTable<-removeCarrierRatesAndFindMinimumRates(t_list_rates)
      carrier%>%
      createRatesComparedToTMin(carrier)%>%
      original_percent_higher<-carrier$Percent_Higher%>%
      findRatesFromPrems(carrier,fee_name)%>%
      original_rates<-carrier$`Rates per Thousand`%>%
      discountRatesAndFindNewPrems(carrier,discount/100,fee_name)%>%
      group_by(!!!category) %>%
      summarise(Current_Comparison=sum(original_percent_higher)) %>%
      ggplot(aes_string(input$category,"Current_Comparison"))+geom_bar(stat="identity",position="dodge") + geom_text(aes(label=scales::percent(Current_Comparison)),size=3.2,vjust=-0.5,position=position_dodge(width=1))
    }
    else{
      rMinTable<-removeCarrierRatesAndFindMinimumRates(r_list_rates)
      carrier%>%
      createRatesComparedToRMin(carrier)%>%
      original_percent_higher<-carrier$Percent_Higher%>%
      findRatesFromPrems(carrier,fee_name)%>%
      original_rates<-carrier$`Rates per Thousand`%>%
      discountRatesAndFindNewPrems(carrier,discount/100,fee_name)%>%
      group_by(!!!category) %>%
      summarise(Current_Comparison=sum(original_percent_higher),Hypothetical_Comparison=sum(New_Percent_Higher)) %>%
      ggplot(aes_string(input$category,"Current_Comparison"))+geom_bar(stat="identity",position="dodge") + geom_text(aes(label=scales::percent(Current_Comparison)),size=3.2,vjust=-0.5,position=position_dodge(width=1))
    }
  })
}
shinyApp(ui=ui,server=server)

错误是:

Warning: Error in discountRatesAndFindNewPrems: unused argument (fee_name)
  170: function_list[[i]]
  169: freduce
  168: _fseq
  167: eval
  166: eval
  164: %>%
  163: renderPlot [.../app.R#54]
  161: func
  121: drawPlot
  107: <reactive:plotObj>
   91: drawReactive
   78: origRenderFunc
   77: output$Minimum_Rates
    1: runApp

不幸的是,出于法律原因,我无法共享数据或功能,但我已经测试了闪亮之外的功能,效果很好。该错误表明我没有在 discountRatesAndFindNewPrems 函数中使用参数,但警告指向第 54 行,即运营商%>% 行。我是 dplyr 的新手并且一般来说是闪亮的,无法理解“%>%”运算符是否只能与 dplyr 函数一起使用,因为我编写了另一个使用 dplyr 和“%>%”运算符的闪亮应用程序,但我只在该应用程序中使用“%>%”的 dplyr 函数。我还缺少什么其他东西,例如使 Carrier 成为响应式或其他什么?

【问题讨论】:

  • 看看这些问题。他们更详细地解释了%&gt;% 管道的工作原理以及如何确保在链接函数时将数据放在正确的位置:stackoverflow.com/questions/49402641/…stackoverflow.com/questions/42385010/…
  • 您的问题可能是因为discountRatesAndFindNewPrems 接受3 个参数:管道进入第一个,carrier 进入第二个,discount/100 进入第三个。 fee_name,那么,无处可去。

标签: r shiny


【解决方案1】:

看看这些问题。他们更详细地解释了 %>% 管道的工作原理以及如何确保在链接函数时将数据放在正确的位置:Using `%>%` with `lm` and `rbind`Using the %>% pipe, and dot (.) notation

当您使用%&gt;% 管道时,管道左侧的输出将发送到右侧的第一个参数(或使用. 运算符指定的参数)。如果我不得不猜测,我会说该特定错误消息正在发生,因为 discountRatesAndFindNewPrems 接受 3 个参数,而您传递了 4 个参数。

  1. 管道的输出进入第一个参数,
  2. carrier进入2号,
  3. discount/100 进入第三个。
  4. 然后,fee_name 无处可去,它给出了unused argument 错误。

我认为将管道与非 dplyr 函数一起使用的最佳策略是始终使用 . 运算符指定管道输出的位置。在几乎所有情况下,您都可以将 . 作为参数放入函数中,管道会将左侧函数的输出发送到该参数。

例如这段代码:

carrier %>%
    createRatesComparedToRMin(carrier)

实际上是在做同样的事情:

createRatesComparedToRMin(carrier, carrier)

我认为这不是您想要的。如果您使用点 .,会更清楚发生了什么:

carrier %>%
    createRatesComparedToRMin(.)

我发现您的函数存在许多其他奇怪的问题,这些问题可能会导致问题:

1) 您不能通过管道传递给赋值运算符来保存中间值(请参阅此问题:R pipe (%>%) capabilities - storage and partial use?

...
createRatesComparedToRMin(carrier) %>%
original_percent_higher <- carrier$Percent_Higher %>%
...

正如我们在这个例子中看到的那样,这部分应该给出一个错误:

mtcars %>%
    dog <- sum() %>%
    sum(volcano)

Error in mtcars %>% dog <- sum() %>% sum(volcano) : 
  could not find function "%>%<-"

如果要保存中间值,则需要停止管道并分配结果,如下所示:

dog <- mtcars %>%
    sum()
sum(dog, volcano)

[1] 704849.2

或者,您可以使用自定义函数来分配链接问题中讨论的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2017-06-03
    相关资源
    最近更新 更多