1

The below section of code works fine for pulling values out of a vertically expanding matrix, reaching down one added row at a time (please pay particular attention to my note in the code of # << ):

plotData <- reactive({
    req(dim(sanitizedMat())[1] >= 1)
    lapply(seq_len(nrow(sanitizedMat())),
           function(i){
             tibble(
               Scenario = rownames(sanitizedMat())[i],
               X = 1:input$periods,
               Y = interpol(input$periods, sanitizedMat()[i, 1:2]) # << note the matrix index [i,1:2]
             )
           }) %>% bind_rows()
  })

However, in my attempt to reorient the matrix reading to horizontal in data pairings of 2, I'd like to make a change to matrix indices as shown below where the interpol() function is applied to Y (emphasis on note # << ):

plotData <- reactive({
    req(dim(sanitizedMat())[1] >= 1)
    lapply(seq_len(ncol(sanitizedMat()))/2,
           function(i){
             tibble(
               Scenario = rownames(sanitizedMat())[i],
               X = 1:input$periods,
               Y = interpol(input$periods, sanitizedMat()[1, i*2-1:i*2]) # << note attempt to use formula for matrix index
             )
           }) %>% bind_cols()
  })

Is it possible to use a formula for matrix indices, like I try above with [1, i*2-1:i*2]? If it is possible, I'm pretty sure my syntax is wrong but this shows the idea.

This would allow me to skip along the matrix horizontally, in leaps of 2 columns as lapply iterates via i.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66

1 Answers1

0

Refer to post How to automatically delete a matrix column in R if there otherwise would be subscript out of bounds error? which reflects dynamic matrix indexing and provides completely resolved code.