1

I've a problem with my shiny code which is somewhat related with the two questions I linked below.

So i've made a dynamic UI :

output$hmgroupsmean <-renderUI({
numGroups <- as.integer(input$HMgroups)

lapply(1:numGroups, function(i) {

  numericInput(paste0("group_", i),
               label= paste("Mean", i),
               min=0, 
               max=1000,
               value= 10)
})
}) 

that display N numericInput depending on how many groups the user has chosen. Then I just want to retrieve all the mean and there begin the problem : I tried what is explained here but it doesnt work :

   output$PowerAnalysisANOVA <- renderPlot({

   allmean = c()

   lapply(1:numGroups, function(i){
     allmean[i] <- input[[paste0("group_", i)]]
   }) 

qplot(allname)

})

it returns : Error : argument "env" is missing, with no default

then i tried something a tad more exotic :

 output$PowerAnalysisANOVA <- renderPlot({

   allname = c()
   allmean = c()
   lapply(1:numGroups, function(i){
     allname[i] <- paste0("input$group_" ,i)
     allmean[i] <- get(allname[i])
   }) 

qplot(allmean)
})

But it doesn't work : Error :object 'input$group_1' not found

Edit after AndriyTkach's comment :

output$PowerAnalysisANOVA <- renderPlot({

   allname = c()
   allmean = c()
   lapply(1:numGroups, function(i){
     allname[i] <- eval(parse (text = paste0("input$group_" ,i))) 
     allmean[i] <- get(allname[i])
   }) 

qplot(allmean)
})

It returns a new Error : invalid first argument

AndriyTkach proposed that :

for (i in 1:numGroups) 
eval (parse (text = paste0("allmean[", i, "] <- input$group_" ,i))) 

Which work a lot better : no Error message but it only work for 2 and 3 groups , the fourth and up is not taken into account Create dynamic number of input elements with R/Shiny accessing inputs created in renderUI in Shiny

Community
  • 1
  • 1
Boo
  • 155
  • 1
  • 12
  • try `eval (parse (text = paste0("input$group_" ,i)))` – Andriy T. May 18 '15 at 14:14
  • Thanks , but it doesn't work : I have a new error : invalid first argument – Boo May 18 '15 at 14:17
  • substitute `lapply(1:numGroups, function(i){ allname[i] <- paste0("input$group_" ,i) allmean[i] <- get(allname[i]) })` with `lapply(1:numGroups, function(i){ eval(parse(text = paste0("allmean[", i, "] <- input$group_", i))) })` ? – Andriy T. May 18 '15 at 14:29
  • I don't really understand what you did but It returns : argument "env" is missing, with no default – Boo May 18 '15 at 14:32
  • you don't need to use `get`. see previous comment – Andriy T. May 18 '15 at 14:36
  • Oh ok so that : lapply(1:numGroups, function(i){ allmean[i] = eval (parse (text = paste0("input$group_" ,i))) }) but it also returns : argument "env" is missing – Boo May 18 '15 at 14:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78097/discussion-between-boo-and-andriytkach). – Boo May 18 '15 at 14:40

1 Answers1

0

After some discussion with AndriyTkach : I have a working program :

output$PowerAnalysisANOVA <- renderPlot({
allmean = c()

for (i in 1:values$numGroups) 
eval (parse (text = paste0("allmean[", i, "] <- input$group_" ,i))) 

qplot(allmean)
})

And I had forgotten to make a reactive variable :

values <- reactiveValues()

output$hmgroupsmean <-renderUI({
values$numGroups <- as.integer(input$HMgroups)
apply(1:values$numGroups, function(i) {

  numericInput(paste0("group_", i),
               label= paste("Condition", i),
               min=0, 
               max=1000,
               value= 10)
})
})
Boo
  • 155
  • 1
  • 12