0
votes

Good day,

I am trying to learn how to easily feed various sets of inputs to a function. Each set of input is like the characteristics of an object. The function should be able to run calculations given the choice of objects and accept other arguments too.

Let's illustrate this with a simplified example about groceries.

The function basket() can accept fruits that have a value and a sugar_amount. I can decide to place any number of fruits in the basket, and I want to return the total value and the sugar amount.

The function basket() should retrieve each fruit's value and sugar_amount from a dataframe or an external CSV file, for example. This data could be structured in this way:

        value  sugar_amount
APPLE      10            20
BANANA      5            10 
CHERRY     15            25

The function can accept several combinations of fruits, allowing the user to pick up to three fruits in our example (the number of available fruits), but can decide to select one or two kinds. The user can also specify how many of each fruit he wants to put in his basket (n1 for the first fruit, n2 for the second and so on). That's because I would like to turn this into a Shiny app where the user can make selections from a dropdown list of fruits.

basket <- function (fruit1, n1,
                    fruit2, n2,
                    fruit3, n3) {
   total_value = (fruit1$value * n1) + (fruit2$value * n2) + (fruit3$value * n3)
   total_sugar = (fruit1$sugar_amount* n1) + (fruit2$sugar_amount* n2) + (fruit3$sugar_amount* n3)
   }

An example calculation would be:

mix1 <- basket(APPLE, n1 = 1, CHERRY, n2 = 2)

      total_value  total_sugar   
mix1           40           70

As you can tell, this would not work as such since it is what I am trying to understand... What should I do to have the possibility of calling any fruit combination I want? Something related with do.call()?

If to go a bit further, I also wondered how to feed several sets of inputs to a function. Is it possible to use a list of mixes and use a sort of lapply() function to achieve this:

      total_value  total_sugar   
mix1           40           70
mix2           60           15
mix3           20           50

(Given that each mix is a different combination and quantity of fruits)

This is all new to me and I have not found an answer online so far. Thanks a lot for your support!

Pass your fruits and quantities in a list. For example basket(list("apple"=1, "cherry"=2)). Then interrogate the list within your function to obtain the required result, perhaps by converting the list to a data frame and then joining the "list df" with the "sugar df". To solve your "several sets" problem, basket could interropgate each element of the list. if the list is itself a list, basket could call itself recursively, with the current element of the list as the argument. - Limey
You use language such as "specifying the number of fruit" but you don't explain how that would be specified - is this a programmer passing an object? A drop down list from Shiny, as you discussed? If the latter, you'll receive the most benefit from framing it in terms of that and specifying in more detail what you think the app should look like and do - sketches of what you mean and pseudo-code are welcome. However, the basic answer will be that all of this should go into lists. - socialscientist