1

I'm trying to avoid hard-coding to print the inputs of 10 text-inputs.

Basically the user feeds in keywords that generates the appropriate SQL code as text.

SERVER.R

Code Block 1:

So the first block of code is creating the ten text inputs – these are created using renderUI and outputted using uiOutput

 output$key1A_depend1 <- renderUI({
      lapply(1:5, function(i){
        textInput(paste0("key1_temp_",i),label=paste0(i),paste0(""))
      })#END lapply 
    })#END renderUI

    output$key1A_depend2 <- renderUI({
      lapply(6:10, function(i){
        textInput(paste0("key1_temp_",i),label=paste0(i),paste0(""))
      })#END lapply
    })#END renderUI

    # THE ABOVE WORKS FINE

Code Block 2:

The second block of code is then assigning text-based outputs based on what is entered in the text inputs. These conform to “” or no text if nothing is inputted in the text inputs.

output$key1A_main <- output$key1A_maincopy <- output$key1A_maincopy2 <- renderUI({ 

  for (i in 1:10){

    xy <- eval(parse(text=paste0("input$key_temp_",i)))

    assign(paste0("key1A_out_",i), 
           if(!is.null(xy)) {
             paste0("Details like '%",xy,"%' or ",br())
           }  
           else paste0(" ")
    )

  }#END for

# HARD CODING THE ABOVE INTO 10 SEPARATE STATEMENTS ALSO WORKS, JUST NOT IN THE FOR LOOP

Code Block 3:

The third block of code is generating the actual output, collating all outputs generated in the second block of code.

HTML(paste0(  key1A_start
                key1A_out_1
                , key1A_out_2
                , key1A_out_3
                , key1A_out_4
                , key1A_out_5
                , key1A_out_6
                , key1A_out_7
                , key1A_out_8
                , key1A_out_9
                , key1A_out_10)) 

})#END renderUI`enter code here`

# THE OUTPUT GENERATED RETURNS BLANKS for Kkey1A_out_1, 2, 3 …. , 10.

However when I hard-code values Code Block 2, it works perfectly

Alternate Code Block 2:

key1A_out_1 <- 
if(nchar(input$key1_temp_1)>0) 
    {paste0("Details like '%",input$key1_temp_1,"%' OR ",br())  } 
else 
{paste0(" ") }

# REPEATED 10 times

Any help would be appreciated, it could either be a scoping issue or just something that assign() doesn't do properly.

Gluck
  • 2,933
  • 16
  • 28
Anes_thetize
  • 229
  • 2
  • 8
  • It would be helpful if you could create a more complete [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Use the `runApp` list syntax (as used in the `?runApp` help page) to make code that we can copy/paste into R to reproduce the problem. Describe the expected behavior (how do we test it works). Simplify as much as possible to make the problem clear. – MrFlick Oct 13 '15 at 05:01
  • Apologies! First post on the forum, but yes I'll be more mindful in future! – Anes_thetize Oct 13 '15 at 22:39

1 Answers1

0

It looks like a simple error where the variable is misnamed. "key" should be "key1" in

xy <- eval(parse(text=paste0("input$key1_temp_",i)))

However, you can simplify code blocks 2 and 3, and get rid of the assign and eval(parse business (both of which should be avoided) with

output$key1A_main <- output$key1A_maincopy <- output$key1A_maincopy2 <- renderUI({
    HTML(
        lapply(1:10, function(i) {
            xy <- input[[sprintf("key1_temp_%d", i)]]
            if (!is.null(xy)) paste0("Details like '%", xy, "%' or ", br())
            else " "
        })
    )
})
Rorschach
  • 31,301
  • 5
  • 78
  • 129