0

I am writing a server program in R where I upload PDF files and which later extracts data from the tables inside the pdf.

If required table and data is there, it works fine. But if not, it gives me error for files[i][[1]][[3]] and files[i][[1]][[4]].

error: subscript out of bounds

I want to default the value of buy and sell price at NA, if the table is not there.

  all_data <- eventReactive(input$done, {
    req(input$files)
    files = {}
    cost_price_list = {}
    sell_price_list = {}
    
    df <- data.frame(cost_price_list = character(), sell_price_list = character())  
    files <- lapply(input$files$datapath, extract_tables)
    
    for (i in 1:length(input$files$datapath))
    {       
        tryCatch(
        {
            cost_price_list <- files[i][[1]][[3]]
            sell_price_list <- files[i][[1]][[4]]
            
        },
        error=function(cond) {
            cost_price_list[i] = NA
            sell_price_list[i] = NA
        }
        )           
        df[nrow(df) + 1,] <- c(cost_price_list[i],sell_price_list[i])
    }
    #return dataframe as table
    df

  })

But the above code doesn't work for me if the table is not there in pdf.

What am i doing wrong?

Please help.

Thinker
  • 6,820
  • 9
  • 27
  • 54
  • Can you define exactly what you mean by "doesn't work for me"? What does happen? What do you want to happen? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It doesn't seem like shiny really has anything to do with this particular out of bounds error so perhaps you can simplify your example not to use shiny. – MrFlick Jan 27 '23 at 15:19
  • It's worth mentioning that error handling in R is **vastly more powerful** than error handling in Java. The Java `try…catch` mechanism implements a small subset of what `tryCatch` and the R condition system can do. – Konrad Rudolph Jan 27 '23 at 15:30
  • @MrFlick I have provided the reason for the error. It is just that i don't completely understand how to handle exception in R. If the pdf doesn't have table, my cost/sell price at index files[i][[1]][[3]] and files[i][[1]][[4]] gives subscript out of bounds exception. – Thinker Jan 27 '23 at 15:37
  • @MrFlick I want the value to NA, if this index give error . – Thinker Jan 27 '23 at 15:43

1 Answers1

1

There are multiple problems with your code.

First of, files = {} works but almost certainly doesn’t do what you intended. Did you mean files = list()? Otherwise, the idiomatic way of expressing what your code does is to write files = NULL in R. A more idiomatic way would be not to assign an empty object at all. In fact, your code overrides this initial value of files anyway, so files = {} is entirely redundant; and so are the next two assignments.

Next, since files is a list, you need to use double brackets to extract single elements from it: files[[i]], not files[i].

Another problem with your code is that assignment in R is always local. So cost_price_list[i] = NA creates a local variable inside the error handler function, even though cost_price_list exists in an outer scope.

If you want to assign to the outer variable, you need to explicitly specify the scope (or use <<-, but I recommend against this practice):

    …
    outer = environment()

    for (i in 1:length(input$files$datapath))
    {       
        tryCatch(
        {
            cost_price_list <- files[i][[1]][[3]]
            sell_price_list <- files[i][[1]][[4]]
            
        },
        error=function(cond) {
            outer$cost_price_list[i] = NA
            outer$sell_price_list[i] = NA
        }
        )           
        df[nrow(df) + 1,] <- c(cost_price_list[i],sell_price_list[i])
    }
    …

But this still won’t work, because these variables do not have a value that can be meaningfully subset into (and it is not entirely clear what your code is attempting to do). Still, I hope the above gives you a foundation to work with.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214