mistersunnyd
mistersunnyd

Reputation: 221

R Shiny error: unused argument but error line is different

I'm writing a Shiny app that selects a dataframe based on the input in Carrier and performs some functions on it. Here's my code:

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)

The error is:

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

Unfortunately, I cannot share the data or the functions for legal reasons, but I have tested the functions outside of shiny which worked just fine. The error says I'm not using an argument in the discountRatesAndFindNewPrems function, but the warning points to line #54 which is the carrier%>% line. I'm new to dplyr and shiny in general and cannot understand whether the "%>%" operator can only be used with dplyr functions because I have written another shiny app that used dplyr and the "%>%" operator, but I only used dplyr functions in that app with "%>%". Is there something else I'm missing, such as making Carrier a reactive or something?

Upvotes: 0

Views: 2376

Answers (1)

divibisan
divibisan

Reputation: 12155

Take a look at these questions. They explain in more detail how the %>% pipe works and how to make sure that it puts your data in the right place when chaining functions: Using `%>%` with `lm` and `rbind`, and Using the %>% pipe, and dot (.) notation

When you use the %>% pipe, the output of the left-hand side of the pipe is sent to the 1st argument on the right-hand side (or to the argument specified with the . operator). If I had to guess, I'd say that that specific error message is happening because discountRatesAndFindNewPrems accepts 3 arguments and you're passing it 4.

  1. The output of the pipe is going into the 1st argument,
  2. carrier goes into the 2nd,
  3. and discount/100 into the 3rd.
  4. fee_name, then, has nowhere to go and it gives the unused argument error.

I think the best strategy for using the pipe with non-dplyr functions is to always specify where the output of the pipe should go with the . operator. In almost all cases, you can just put . as the argument in a function and the pipe will send the output of the left-hand function to that argument.

For example, this code:

carrier %>%
    createRatesComparedToRMin(carrier)

Is actually doing the same thing as this:

createRatesComparedToRMin(carrier, carrier)

Which I assume isn't what you wanted. It's much clearer what's happening if you use the dot .:

carrier %>%
    createRatesComparedToRMin(.)

I see a bunch of other strange issues with your function that could cause problems:

1) You cannot pipe into assignment operators to save intermediate values (See this question: R pipe (%>%) capabilities - storage and partial use?)

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

This section should give an error as we see in this example:

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

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

If you want to save intermediate values, you need to stop the pipe and assign the result like this:

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

[1] 704849.2

Alternately, you could use a custom function that assigns a result as discussed in the linked question.

Upvotes: 1

Related Questions