Search code examples
rrstudiotesseract

How to save R output data to a text file


Hope your doing well.

My question might have answered by someone else but as I couldn't able to find related answer so I'm writing a post here.

Question

How to save R output to a text file.

I've extracted data from scanned PDF using R but my out put is in console and I want to save that as a text file. If you have any suggestions please let me know.

My code as follows

library (tesseract)
filename= 'my_file.pdf'
file <- pdftools::pdf_convert(filename, dpi =600)
text<- tesseract::ocr(file)
Cat(text)

How to save cat(text) to a text file

Thanks 😊

how to split below data any idea

  Xxxx xxxx xxxx xxxx x [2 XXXX $123.45 10.1000 $10.20

First split has to be [2 and then spaces there after


Solution

  • As Stéphane Laurent noted, you can save the output with

    cat(text, file = "myfile.txt")
    

    If you want to create a dataframe, you could use the text object directly. For example:

    df <- data.frame("text" = text)
    

    Alternatively, if you want to save your file and then read it in, I would propose you choose a file format which is more suited for storing tables. I.e. csv.

    write.csv(text, file = "text.csv", row.names = FALSE)
    
    df <- read.csv("text.csv")