Search code examples
rshinyradio-buttonflexdashboard

Input from radio buttons or free text in shiny/flexdashboard


I am trying to allow users to input a variable using radio button while giving the option for free text (if none of the listed objects fits the desired input). I could not find a way through searching online. I am still new to flexdashboard/shiny.

I created this reprex below . what I am trying to do is to add a fourth radio button that has a free text option. Any suggestion?

thank you

---
title: "example2"
runtime: shiny
output: 
  flexdashboard::flex_dashboard: 
    orientation: rows
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
```

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}

radioButtons("nm", "What is your name??",
               c("jo" = "John",
                 "pe" = "Peter",
                 "da" = "David"))

actionButton("execute", "enter")
```

Column {data-width=350}
-----------------------------------------------------------------------

### Chart B

```{r}
phrase =  eventReactive(input$execute, {
  
  paste("Hi", input$nm, sep=" ")
  
})


 

 renderText({

   req(phrase())
   
phrase()
  
 })
  
```
 

Solution

  • You can use updateRadioButtons to update the radio buttons choices:

    choices = c( "jo"="John",
                     "pe"="Peter",
                     "da"="David")
    radioButtons("nm", "What is your name??", choices)
    textInput("other", "other")
    observeEvent(input$other, {
      if(!is.null(input$other) && input$other != "")
           updateRadioButtons(session, "nm", choices = c(choices, input$other), 
                              selected = input$other)})
    actionButton("execute", "enter")
    

    result