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

7
  • try eval (parse (text = paste0("input$group_" ,i))) Commented May 18, 2015 at 14:14
  • Thanks , but it doesn't work : I have a new error : invalid first argument Commented May 18, 2015 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))) }) ? Commented May 18, 2015 at 14:29
  • I don't really understand what you did but It returns : argument "env" is missing, with no default Commented May 18, 2015 at 14:32
  • you don't need to use get. see previous comment Commented May 18, 2015 at 14:36

1 Answer 1

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)
})
})
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.