Is it possible to make a R Shiny reactive function that accepts arguments like a normal function? - r

I have a situation where I have a reactive function that I would like to be used in several different plots, but to be calling/checking different parts of a dataframe stored as reactiveValues.
If I had a normal function I could call it and pass it an argument to index a specific column of the reactive values e.g.,:
my_function <- function(column) {what the function does}
Then when calling the function:
my_function(column = 3)
But for a reactive function I don't see how I can do something like this.
my_function <- reactive({what the function does})
At the moment, I can just make multiple versions of essentially the same function that call different indexes but that is a lot of extra lines of code as the function is quite long.
The situation is also not one where I see a simple way of using another reactive value that can be called inside the reactive function that would appropriately call the right column index in all cases.
Is there a way to define the reactive function so that when it is called, I can pass it an argument?

Related

Insert a Function Name into a Call

I have a long script wherein I have a section at the top which is dedicated to assumptions and other hardcodes. By means of context, it's an asset allocation script wherein one of the hardcodes that is chosen is the method by which momentum is calculated, for which I have various functions. I realize that one way in which I could change the code is simply to change the function used in the body of the code; however, my preference would be to leave the body untouched and only change the name of the function to be used within the assumptions section.
By means of an extremely simple toy example, let's say I have the following two lines of code:
selected_function = "sum"
temp_data = c(2,3,4)
How would I allow whichever function is saved in the variable selected_function to be the calling function on the temp_data data set (i.e., if I want to change it to "mean", etc.) and allow that function to be applied to the data.
If you have a function name as a string, you get can get the function itself with getFunction(). That will return a function and you can then pass your values to that returned function. For example.
selected_function = "sum"
temp_data = c(2,3,4)
getFunction(selected_function)(temp_data)

How to save the plot output and object output of a function call in R

I am making a function call in R. The function returns a list and in it has a call to plot(). From this one call, I need to record the plot as an object and store the list as a separate object. I need to store the plot because I later give it to ggarrange() along with other plots. I must store both outputs in one call of the function because the function runs permutations. As a result, it will produce a slightly different output each time. Therefore, in order for the data in the list to match the plot, the call can only be made once.
The line of code below is what I am currently using that successfully stores the plot as ggplot object. It does not store the list.
my_plot <- as.ggplot(~(my_function(input1,input2, permutations=1000)))
The code below will return the list, but not save the plot.
my_list <- my_function(input1,input2, permutations=1000)
Does anyone know a way of accomplishing what I am trying to do?

R shiny reactive return more than one object

I am making use of the example provided in in the answer to this question: Shiny Reactivity
They make use of two return statements in the reactive function. I believe return(x) is just a mistake. However, I am wondering if I can return more than one object with one reactive statement?
You can think of reactive() as any regular function that returns a value, it just happens to return a reactive value and to always be called automatically when needed. In a regular function, how do you return more than one object? You can return a list, for example. You can do the same with reactive().

How do I remove an object from within a function environment in R?

How do I remove an object from the current function environment?
I'm trying to achieve this:
foo <- function(bar){
x <- bar
rm(bar, envir = environment())
print(c(x, is.null(bar)))
}
Because I want the function to be able to handle multiple inputs.
Specifically I'm trying to pass either a dataframe or a vector to the function, and if I'm passing a dataframe I want to set the vector to NULL for later error handling.
If you want, you can watch my DepthPlotter script, where I want to let the second function check if depth is a dataframe, and if so, assign it to df in stead and remove depth from the environment.
Here is a very brief sketch of how to set this up using S3 method dispatch.
First, you define your generic:
DepthPlotter <- function(depth,...){
UseMethod("DepthPlotter", depth)
}
Then you define methods for specific classes of the argument depth. As a very basic example in your case, you might create only two, a data.frame method and a default method to handle the vector case:
DepthPlotter.default <- function(depth, variable, ...){
#Here you write a function assuming that depth is
# anything but a data frame
}
DepthPlotter.data.frame <- function(depth,...){
#Here you'd write a function that assumes
# that depth is a data frame
}
And then you can call DepthPlotter() using either type of argument and the correct function will be run based upon the result of class(depth).
The example I've sketched out here is a little crude, since I've used a default method to handle the vector case. You could write .numeric and .integer methods to handle numeric or integer vectors more specifically. In my example, the .default method will be called for any case other than data.frame, so if you go this route you'd want to write some code in there that checks for strange cases like depth being a complicated list, or other odd object, if you think there's a chance something like that might be passed to the function.

Naming columns of coefficient matrix in a VAR

I am searching for a fast and simple way to give comprehensible names to the columns of a VAR-coefficient matrix.
What I would like to use is the function VAR.names, which is used in the function VAR.est() in the VAR.etp-package. When I use the function VAR.est(), this works perfectly, but as soon as I modify VAR.est (by adding another element to the list of values which are returned), I receive an error message stating "could not find function VAR.names".
I could not find any information on the function VAR.names.
Example:
library(VAR.etp)
data(dat)
M=VAR.est(dat,p=2,type="const")
M$coef
Another possibility would be to use a loop as in the function VAR() from the vars package, but if VAR.names would actually work, this would be a lot more elegant!

Resources