Fabrice
Fabrice

Reputation: 387

Rshiny : selectInput in modalDialog

I want to open a modalDialog containing a selectInput list and a textAreaInput. This modalDialog is inside an observeEvent and is well displayed when the event is triggered.

When the modalDialog pop, the user choose an item in the list (a month's name). I want the choosed month to be displayed in the textAreaInput.

I tested this piece of code but the value input$dateList of the selectInput is not displayed in the textAreaInput.

Any clue ? Thanx a lot !

dates = c("january","february","march") 
showModal(modalDialog(
    output$date_input <- renderUI({ 
        selectInput("dateList",  "History:",choices=c(dates))
    }),  
    textAreaInput(width=600, "test", "test", value=input$dateList),
    footer = tagList(modalButton("Cancel"), actionButton("Ok", "OK))
))

EDIT (by @SL)

Reproductible code (does not work):

library(shiny)

ui <- fluidPage(
  actionButton("open", "Modal")
)

server <- function(input, output, session) {
  
  observeEvent(input[["open"]], {
    showModal(modalDialog(
      tagList(
        selectInput(
          "dateList", "History:", 
          choices = c("january", "february", "march")
        ),
        textAreaInput(width = 600, "test", "test", value = input[["dateList"]])
      ),
      footer = tagList(modalButton("Cancel"), actionButton("Ok", "OK"))
    ))
  })
  
}

shinyApp(ui, server)

Upvotes: 1

Views: 280

Answers (1)

jpdugo17
jpdugo17

Reputation: 7116

We can use updateTextAreaInput inside observeEvent.

  observeEvent(input[["dateList"]], {
    updateTextAreaInput(inputId = "test", value = input$dateList)
  })

App Thanks @StéphaneLaurent for providing reproducible code

library(shiny)

ui <- fluidPage(
  actionButton("open", "Modal")
)

server <- function(input, output, session) {
  observeEvent(input[["open"]], {
    showModal(modalDialog(
      tagList(
        selectInput(
          "dateList", "History:",
          choices = c("january", "february", "march")
        ),
        textAreaInput(width = 600, "test", "test")
      ),
      footer = tagList(modalButton("Cancel"), actionButton("Ok", "OK"))
    ))
  })

  observeEvent(input[["dateList"]], {
    updateTextAreaInput(inputId = "test", value = input$dateList)
  })
}

shinyApp(ui, server)

Upvotes: 2

Related Questions