R Shiny select input unselected - r

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.

Related

R thinks argument is a function but actually is a variable

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.

R Shiny create input using renderUI and function results

I am new to R Shiny and I am trying to create an app which I need it to be as interactive as possible. One problem that I am dealing with is this. In the ui.R I have the following:
helpText("Select a feature"),
uiOutput("sliders")
And in the server.R:
output$sliders <- renderUI({
selectInput('feature_name',
'Feature name', #Description
choice = c("F1"='1', "F2"='2'))
})
My question is that is it possible to change something in renderUI so that instead of setting c("F1"='1', "F2"='2')) statically I can pass the results of a function to it so it can be more dynamic (a function which generates a feature list based on something that user does and passes the list to renderUI to create the selectInput). Something like following:
output$sliders <- renderUI({
selectInput('feature_name',
'Feature name', #Description
choice = c(feature_creator(method)))
})
Where "feature_creator" is a function and it returns: "F1"='1', "F2"='2' based on the method selected by user (variable "method" is defined and I have the value). My question to be more specific is what should "feature_creator" return as output?
Hope my question is clear enough. Let me know if I should add anything to the problem description.
Any help will be much appreciated. Thanks
Assuming that all you really need is the method argument, this is not hard.
1) Make a radioButtons() input in your ui that will let the user select a method, lets give it inputId="MethodChoice".
2) In your choice argument in the selectInput you should use choice=c(feature_creator(input$MethodChoice))
Then feature_creator will get a text value based on the method the user chooses.
In order to work in choice, feature_creator should return a named list, similar in format to what you hard-coded.

R shiny updateCheckboxInput

I have a R shiny application in which I have drop down box defined in ui.R as lib whose values are c("X","Y","Z") and also a check box called spcheck which is defined as
checkboxInput("spcheck","label1",value = FALSE))
Other conditions makes this checkbox available only when input$lib=Z and checking it would make few things appear.
When users select a different library, say X, I want to make the spcheck value FALSE, So I added the following code to server.R
observe({
if (input$lib %in% c("X","Y") )
{cat("uncheck called 1 : ",input$spcheck,'\n')
updateCheckboxInput(session,"spcheck","label1,value = FALSE)
cat("uncheck called 2 : ",input$spcheck,'\n')
}
else return()
})
The text displayed at console is :
uncheck called 1 : TRUE
uncheck called 2 : TRUE
Why is it not making the spcheck value FALSE?
May be I'm missing something very trivial, but I couldn't figure it out.
Any help??
Because it takes a few milliseconds since you tell shiny to update the input until it actually happens. When you call the update method, shiny has to send a message to JavaScript to change the value of the input, and once that's done, JavaScript sends a message back to R saying that the input has a new value. But the input variable does not change instantly when you make the call, it'll only have an updated value next time a reactive statement uses the input$spcheck variable

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