R thinks argument is a function but actually is a variable - r

In the RShiny package (for web applications) you have reactive variables (update automatically when something in the UI changes). When you use these variables you need to use this syntax: variable() with brackets at the end of the variable name.
I need this reactive variable as input for one of my functions. I call this function in the server.R part. There I use function(infile = fa_archive(), ...) but R thinks that fa_archive() is a function instead of a variable. Probably because of the parentheses, but it isn't. I am 100% sure the variable fa_archive is defined. Beforehand I only needed fa_archive[[1]]() as input and then my function worked fine. But now I needed to "upgrade" my function as to include more files at once and when I use fa_archive() as input variable in the function it recognizes it as a function and returns
Error: Could not find function "fa_archive()"
I have not really tried solving this, because it just seems so weird that using the full variable instead of a part of it ([[1]]) should change this so drastically. Here is the relevant piece of my code
fa_archive <- list(
reactive({ fa_archive_function(input$model1, as.integer(input$lag1)) }),
reactive({ fa_archive_function(input$model2, as.integer(input$lag2)) }),
reactive({ fa_archive_function(input$model3, as.integer(input$lag3)) }),
reactive({ fa_archive_function(input$model4, as.integer(input$lag4)) }))
output$plotSounding1Da <- renderPlot({ plot_profile(infile= fa_archive() , fcdate=fcdate(), ldt=ldt(), prm=input$prmSounding, location=input$location, recent=recent(), add=FALSE)}, width=400, height=500)

When you use [reactive] variables you need to use this syntax: variable() with brackets at the end of the variable name.
Correct. But this only works on a reactive variable. And your fa_archive() isn’t a reactive variable, it’s a normal R lits. That’s why R complains.
Just surrounding the list call with reactive(…) probably won’t work though, because your code has other issues that need fixing. Without knowing what exactly plot_profile does, it’s unlikely that it expects a list of reactive components as an argument.

Related

How to save a function containing parameters into an RDS file?

Suppose that after some calculations I obtain a (floating) number, stored in the variable a, for example
a <- sqrt(2)
Now, I want to define a function that uses that parameter, for example:
myfunction <- function(x){x-a}
How can I save myfunction into an RDS file, in such a way that it can be loaded and used in a new R session, where the variable a is not defined?
Or from a different perspective: How to define the function, but substituting for a its actual numerical value in the function definition? That is, I'd like R to actually define the function
myfunction <- function(x){x - 2.1415.....}
where the actual value of a has been substituted in the definition.
Simply trying saveRDS(myfunction, 'myfunction.rds') does not work: if I start a new R session and do
myfunction <- readRDS('myfunction.rds')
myfunction(1)
then R complains that a is not defined.
Please note that I'm here giving a minimal working example of the problem. Obviously, in the case above I could just define myfunction <- function(x){x-sqrt(2)} and save that in an RDS file; it could be loaded in a new session and used without problems.
However, in my case I have many parameters like a, not just one, obtained from long calculations. I'm not interested in saving their values, I only want to save the function that uses them in its definition, and be able to use that function in a new R session.
An RDS file won't save the global environment, but if you create a closure, it will preserve the values in that environment. One such way to do what would be
myfunction <- {function(a) function(x){x-a}}(a)
And then you can call this function like a regular function
myfunction(1)

R Shiny select input unselected

Hello How can i get info that Select input is still unselected i try something like
server.R
output$Country<-renderUI({selectInput(inputId ="inputCountry",label ="change options",choices=data$Country) })
if(input$inputCountry=="")
or
if(is.null(input$inputCountry))
is.null should do it (depending on quite how you initialise the input)
As doctorG said, is.null is the correct choice if you want to check whether an input variable has already been "activated". There is however a better option in case you want to perform this check in a reactive environment, which is
req(input$inputCountry)
This will automatically stop the current observe or renderXXX function if the input is either "", NULL or FALSE. req can also be used inline in the following manner
output$plot <- renderPlot({hist(rnorm(req(input$n)))})
The req function also has an optional argument cancelOutput which can be quite handy in certain usecases. See ?req.

Using delayed assignment in a function: How do I send the promise back to the parent environment?

I would like to used delayedAssign to load a series of data from a set of files only when the data is needed. But since these files will always be in the same directory (which may be moved around), instead of hard coding the location of each file (which would be tedious to change later on if the directory was moved), I would like to simply make a function that accepts the filepath for the directory.
loadLayers <- function(filepath) {
delayedAssign("dataset1", readRDS(file.path(filepath, "experiment1.rds")))
delayedAssign("dataset2", readRDS(file.path(filepath, "experiment2.rds")))
delayedAssign("dataset3", readRDS(file.path(filepath,"experiment3.rds")))
return (list <- (setOne = dataset1, setTwo = dataset2, setThree = dataset3)
}
So instead of loading all the data sets at the start, I'd like to have each data set loaded only when needed (which speeds up the shiny app).
However, I'm having trouble when doing this in a function. It works when the delayedAssign is not in a function, but when I put them in a function, all the objects in the list simply return null, and the "promise" to evaluate them when needed doesn't seem to be fulfilled.
What would be the correct way to achieve this?
Thanks.
Your example code doesn't work in R, but even conceptually, you're using delayedAssign and then you immediately resolve it by referencing it in return() so you end up loading everything anyway. To make it clear, assignments are binding a symbol to a value in an enviroment. So in order for it to make any sense your function must return the environment, not a list. Or, you can simply use the global environment and the function doesn't need to return anything as you use it for its side-effect.
loadLayers <- function(filepath, where=.GlobalEnv) {
delayedAssign("dataset1", readRDS(file.path(filepath, "experiment1.rds")),
assign.env=where)
delayedAssign("dataset2", readRDS(file.path(filepath, "experiment2.rds")),
assign.env=where)
delayedAssign("dataset3", readRDS(file.path(filepath, "experiment3.rds")),
assign.env=where)
where
}

using values from a reactive input to directly input into a custom function

The following function fails:
get_num <- function(input$num){ans <-input$num
return(ans)}
since R is confused by the $ symbol.
Although this is a toy example, I would like to write a function that directly takes reactive input values (in this case, a number) and does something meaningful with them, without having to preempt the situation with
num <- input$num
get_num <- function(num){ans <-num
return(ans)}
Is this even possible?
There are three points here:
when you are dealing with reactive values, you use reactive() instead of function() in your script.
Here is example:
num_square = reactive({input$num^2})
observe(print(num_square()))
The first line defines a new reactive values base on input$num and second lines print it as soon as it changes. Note that reactive values are same as function, and you should call them with () in front of them.
when you want to save a value to outside environment (other that internal use of function or reactive) you should use <<- instead of = or <- notation.
Here is an example:
reactive1 <- reactive({num_square <<- input$num^2
print(num_square) })
The above line changes the value of num_square as soon as you run reactive1() some place in your code. note that without running reactive1() the value of num_square wont change. This is the BIG DIFFERENCE between reactive() (lazy evaluation) and observe() (eager evaluation).
observe() is another method to use reactive values in a function. It seems to me that you are looking for this one.
Here is an example. The value of get_num will change as soon as you change input$num in your program.
observe({get_num <<- input$num
print(get_tmp)})
Note that above script should be in middle of shinyServer(function(input, output) { ... }).
Difference between reactive() and observe(): [refer to: http://shiny.rstudio.com/reference/shiny/latest/observe.html ]
An observer is like a reactive expression in that it can read reactive
values and call reactive expressions, and will automatically
re-execute when those dependencies change. But unlike reactive
expressions, it doesn't yield a result and can't be used as an input
to other reactive expressions. Thus, observers are only useful for
their side effects (for example, performing I/O).
Another contrast between reactive expressions and observers is their
execution strategy. Reactive expressions use lazy evaluation; that is,
when their dependencies change, they don't re-execute right away but
rather wait until they are called by someone else. Indeed, if they are
not called then they will never re-execute. In contrast, observers use
eager evaluation; as soon as their dependencies change, they schedule
themselves to re-execute.
Try:
get_num <- function(ans = input$num) {
out <- seq(ans:ans*2)
return(out)
}
get_num()
or:
get_num <- function(ans) {
out <- seq(ans:ans*2)
return(out)
}
get_num(input$num)
I think a more natural way to do this in shiny is to use reactive like so:
get_num<-reactive({ ans<-input$num)}
Obviously you could do more, like:
get_num<-reactive({ans<-input$num*20
ans<-ans/pi
})
or whatever. Then refer to your value as get_num() as it is now a function.

R and Shiny: Using output of a reactive function

Currently I have a function [degtest] which is created in the shiny server, that returns a list,
return(list(datatable=datatable, predicttable=predicttable, esttable=esttable)
I want this list to be accessible after the function has run so that I can use different parts of the list to be rendered separately.
outlist <- reactive({
if(is.null(input$file2)){return(NULL)}
if(input$d2 == 0){return(NULL)}
with(data = reactdata$degdata, degtest(reactdata$degdata[,input$selectTemp], reactdata$degdata[,input$selectPot],reactdata$degdata[,input$selectWeight], reactdata$degdata[,input$selectTime], input$Temp0))
})
input$file2 is my reactdata (reactdata$degdata and input$d2 is an action button.
I thought i'd be able to reference outlist$datatable but R says ' object of type 'closure' is not subsettable'
When you are making an object reactive, you are actually making it into a sort of function (closure), so you have to use it as outlist() rather than outlist. See this similar question. It's hard to answer your question considering you did not provide a reproducible example, but I think your solution will be something like outlist()$ObjectYouAreTryingToAccess.

Resources