0

I'm new to Shiny, and I'm trying to convert an existent code that works as an .R script into Shiny app.

Original code - link

Sample data - link

The point is to have a fileinput, where a person selects a pdf file. Than the pdf is processed.

This is my code:

server <- function(input, output) {
  
  output$contents <- renderTable({
    
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.
    
    req(input$file1)
    
    test <- pdf_text(input$file1$datapath)
    
    ### Two Sided
    
    test <- gsub("[\r\n]", " ", test)
    list <- strsplit(test, " {2,}") #split anywhere where there are 2 or more consecutive spaces - hopefully only between two paragraphs (if not the output wont make much sense) 
    
    resi <- lapply(list, function(x) {
      unl <- unlist(x)
      len <- length(x)
      uneven <- seq(from = 1, to = len , by = 2)
      even <- seq(from = 2, to = len , by = 2)
      
      uneven <- unl[uneven]
      even <- unl[even]
      uneven <- paste(uneven, collapse = " ")
      even <- paste(even, collapse = " ") #intentionally leave a space between them, one could even use something that is not expected to occur in the document like "frafrafra" and use that in the gsub call later as gsub("(\\d)-frafrafra(\\d)", "\\1\\2", resi)
      return(cbind(uneven, even))
    }) #separate even from uneven rows
    
    resi <- unlist(resi)
    
    resi <- gsub("(\\d)- (\\d)", "\\1\\2", resi) #clean numbers
    resi <- gsub("(\\b)- (\\b)", "\\1\\2", resi) #clean words
    
    resi <- data_frame(line = 1:length(resi), text = resi) #change class/type - vector to dataframe
    
    count <- resi %>% 
      unnest_tokens(word, text)  %>% #split columns into word like elements (tokens)
      count(word, sort = TRUE)       #count frequency and sort in desc order
    
    count$word <- gsub("[^0-9]", NA, count$word) 
    count$num_char <- nchar(count$word)
    
    two_cols <- count %>%
      filter(!is.na(word)) %>%
      filter(n == 1) %>%
      filter(num_char == 7 | num_char == 13 | num_char == 15)
    
   
    if(input$disp == "head") {
      return(head(test))
    }
    else {
      return(two_cols)
    }
    
  })
  
}

The code seems to work until test <- pdf_text(input$file...

However, I'm getting an error: wrong sign in 'by' argument

Any clue what is wrong?

Edit:

I am testing with the second part of the original code, it seems to be an issue with the scoping rules. That is, defining a function within the server function.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Prometheus
  • 1,977
  • 3
  • 30
  • 57
  • `seq(from = 2, to = 1 , by = 2)` will give that same error. Do you have a case where `length(x)=1` in your `lapply`? You really should provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It sounds like there is a problem with your input data or your assumptions about it. – MrFlick Oct 03 '17 at 15:17
  • @MrFlick thank you for the feedback. I made an edited the OP and upload link to data and original script. – Prometheus Oct 03 '17 at 15:39
  • @MrFlick there shouldn't be an issue here. The pdf is either two sided or one sided. It works in the original code.. – Prometheus Oct 03 '17 at 15:43

0 Answers0