How to store/retrieve input field values to/from different reactive based upon master input field

29 views
Skip to first unread message

Sebastien Bihorel

unread,
May 23, 2016, 5:25:46 PM5/23/16
to Shiny - Web Framework for R

Hi,

I want to create a UI with several slave input fields (mostly textInput and selectInput fields) and store their value in different reactive objects depending on the value of a master selectInput field. Practically, if the value of the master selectInput field is, let's say, 'a', I want to store all other slave field values to the slot "a" of a list of reactives; if the master selectInput field is "b", the slave field values are store in the slot "b" of the same list. The overall goal is to allow users to switch from one selection to another in the master selectInput field and retrieve the information they provided earlier, rather than resetting all slave fields to default.

I thought using eventReactive would be the way to go, but either this is not the right approach or my event conditions are wrong as illustrated in the code below (using a single slave field). Theoretically, I could create as many set of slave inputs as I have possible values in my master selectInput and show/hide them using conditionalPanel functions. However, this is practically not possible in my real app, as the choices in my master selectInput are also defined by a reactive with a priori unknown number of elements and unknown values.

Any suggestions to achieve the overall goal would be greatly appreciated.

Thank you


server
<- function(input, output, session){

    output$selector
<- renderUI({
        selectInput
('dataSelector',
          label
= 'Select a data',
          choices
= letters[1:3])
     
})
   
    dfs
<- letters[1:3]
    names
(dfs) <- dfs
    storeText
<- sapply(dfs,
     
function(x) {
        eventReactive
(input$dataSelector==x,
         
{input$text})
     
},
      USE
.NAMES = TRUE
   
)
   
   
    output$text
<- renderUI({
        textInput
('text',
          label
= 'Enter some text',
          value
= storeText[[input$dataSelector]]())
     
})
   
    output$check
<- renderPrint({
        cat
('Value of input$text')
       
print(input$text)
        cat
('Value of storeText$a')
       
print(storeText$a())
        cat
('Value of storeText$b')
       
print(storeText$b())
        cat
('Value of storeText$c')
       
print(storeText$c())
       
print(dfs)
       
print(storeText)
     
})
   
 
})

ui
.R <- fluidPage(
    uiOutput
('selector'),
    uiOutput
('text'),
    verbatimTextOutput
('check')
 
))

shinyApp
(ui = ui, server = server)





Reply all
Reply to author
Forward
0 new messages