0

I have a clunky block of shiny code on server.R side that I feel R syntax ought to allow me make one or two lines and simultaneously more flexible with some kind of lapply or do.call line

    if(input$parVary == "area" && as.numeric(input$nTraces) > 3 )
    {
        area <- c(input$area, input$area2, input$area3, input$area4)
    } else if(input$parVary == "area" && as.numeric(input$nTraces) > 2 )
    {
        area <- c(input$area, input$area2, input$area3)
    } else if(input$parVary == "area" && as.numeric(input$nTraces) > 1 )
    {
        area <- c(input$area, input$area2)
    } else
    {
        area <- input$area
    }

But I have spent a day and about a billion different combos of lapply, do.calls, reactive, get, c, and observes around

    paste0('input$area', 1:as.numeric(input$nTraces))

I just can't seem to find the right combination or figure out the reactive concept I'm missing. It -seems- to be related to the code not ever including individual input$area1, input$area2, etc... explicit text anywhere in the code?

  • You cant use `paste0('input$area')`. Do it without the single quote? – CuriousBeing Feb 18 '16 at 00:09
  • Do you mean single quotes versus double quotes - or no quotes? Single and double quotes always act the same, here. No quotes bombs completely inside paste0. BTW slight confusion in my original entry. While that is clunky code that worked, for attempts to improve, I added a "1" to input$area in ui.R for input$area1 - so paste0 with 1:n syntax trials made sense. – Ken O'Brien Feb 18 '16 at 01:44
  • you could do : `if(input$parVary == "area" && as.numeric(input$nTraces) > 0) area <- lapply(paste0("area",1:as.numeric(test)),function(x) input[[x]])`, can't use `$` here, http://stackoverflow.com/questions/18222286/select-a-data-frame-column-using-and-the-name-of-the-column-in-a-variable – NicE Feb 18 '16 at 05:56
  • @NicE Many thanks. Dang. In the month since taking up R I'd tussled with that $ versus [[]] addressing 3 times already. Can't believe I forgot to fiddle with that. I got stuck on wrapping it lapplys and do.calls. I'll have to get more familiar with what is going on under the hood with string creation- versus $ and [[]] syntax to avoid mistakes like this in future. I don't even need the conditional part you included to get function I want. Thanks again. – Ken O'Brien Feb 18 '16 at 12:50

1 Answers1

0

I spoke a little too soon in comment above. My specific code ended up needing conditional to handle the list versus single value case. But @NicE answer is one I was looking for. Five sections like

    if(input$parVary == "area" && as.numeric(input$nTraces) > 1 )
    {
        area <- lapply(paste0("area",1:as.numeric(input$nTraces)),function(x) input[[x]])
    } else
    {
        area <- input$area1
    }

teamed with later

    mySolution <- list()
    if(input$nTraces =='1')
    {
        mySolution <- solveCalc(dat=dat,tox=tox,area=area,temp=temp,model=modelIn)
    } else
    {
        mySolution <- switch(input$parVary,
                       "model" = lapply(modelIn,solveCalc,dat=dat,tox=tox,area=area,temp=temp),
                       "temp" = lapply(temp,solveCalc,dat=dat,tox=tox,area=area,model=modelIn),
                       "tox" = lapply(tox,solveCalc,dat=dat,temp=temp,area=area,model=modelIn),
                       "Vt" = lapply(dat,solveCalc,tox=tox,temp=temp,area=area,model=modelIn),
                       "area" = lapply(area,solveCalc,dat=dat,tox=tox,temp=temp,model=modelIn)
                       )
    }

Got me just what I wanted.