3

Suppose I have in my shiny ui.R an input:

selectInput("choice","Select Values",choices=c('a','b','c',FALSE),
            selected="FALSE")

And I want to use this input as a parameter to a function in server.R. The default value for this parameter is FALSE, but can take in character values. I want to set it so that, for example, if the user select 'a', the value 'a' would be passed on to the parameter in the function, and if the user select 'FALSE', the default value of FALSE is passed on to the parameter.

The way I tried to do this is by:

choice <- ifelse(input$choice=='FALSE',FALSE,input$choice)

and then use it in the function:

sample_func(param=choice)

However, this gives me an error "param has the wrong format". The error is the result of choice not being a character value or FALSE.

What might be the reason for this?

3
  • This works for me, is it a problem with how sample_func handles its input? Commented May 30, 2015 at 0:41
  • hm okay I had set multiple = T in my program for selectInput, if I delete that it seems to work. How does the multiple argument affect this when I have not selected multiple inputs? Commented May 30, 2015 at 1:07
  • ifelse is tricky with vectors, you will get one value back per comparison Commented May 30, 2015 at 1:18

1 Answer 1

1

The first problem is:

choice <- ifelse(input$choice=='FALSE',FALSE,input$choice)

This must be written in a reactive context as it depends on user input. So you have to wrap your ifelse(...) part in reactive(). Of course I don't know if you had done so already since you didn't post the entire code and I assume you just wrote that code without the wrapping of reactive().

Another thing concerning this same line is not really a bug, but a habit. It's a bad idea to use choice again. The point is, it already existed as in input$choice and now you assign it to a reactive value of the same name. That could be confusing if you have a complex and long code.

So, I would suggest something like this below. fun_choice <- reactive(ifelse(input$choice=='FALSE',FALSE,input$choice))

The next problem is in:

sample_func(param=fun_choice) ## I changed the argument name to fun_choice

If you had treated fun_choice as reactive, That should be replaced by fun_choice(). Also, that would mean that this function result should also be in reactive context. So the line should become:

variable_name <- reactive( sample_func(param=fun_choice()) )

Of course, it could be wrapped in observe if what you want is the side effect and some other things but not the function return.

Hope this help.

One last thing, if fun_choice is of no other use elsewhere but just passing into sample_func, I would suggest you put them in one reactive(). i.e.

variable_name <- reactive({
  fun_choice <- ifelse(input$choice == 'FALSE', FALSE, input$choice)
  sample_func(param = fun_choice)
})

Note that in this method, fun_choice, within the same reactive(), need not be expressed as fun_choice(), but simply fun_choice.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.