1 Text to File

Go to the RMD, R, PDF, or HTML version of this file. Go back to fan’s REconTools Package, R Code Examples Repository (bookdown site), or Intro Stats with R Repository (bookdown site).

1.1 Latex Table to File

Tabular outputs, text outputs, etc are saved as variables, which could be printed in console. They can also be saved to file for future re-used. For example, latex outputs need to be saved to file.

# Load Data
dt <- mtcars[1:4, 1:6]
# Generate latex string variable
st_out_tex <- kable(dt, "latex")
print(st_out_tex)
# File out
# fileConn <- file("./../../_file/tex/tex_sample_a_tab.tex")
fileConn <- file("_file/tex/tex_sample_a_tab.tex")
writeLines(st_out_tex, fileConn)
close(fileConn)

1.2 Create a Text File from Strings

st_file <- "\\documentclass[12pt,english]{article}
\\usepackage[bottom]{footmisc}
\\usepackage[urlcolor=blue]{hyperref}
\\begin{document}
\\title{A Latex Testing File}
\\author{\\href{http://fanwangecon.github.io/}{Fan Wang} \\thanks{See information \\href{https://fanwangecon.github.io/Tex4Econ/}{Tex4Econ} for more.}}
\\maketitle
Ipsum information dolor sit amet, consectetur adipiscing elit. Integer Latex placerat nunc orci.
\\paragraph{\\href{https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3140132}{Data}}
Village closure information is taken from a village head survey.\\footnote{Generally students went to schools.}
output:
  pdf_document:
    pandoc_args: '..//..//_output_kniti_pdf.yaml'
    includes:
      in_header: '..//..//preamble.tex'
  html_document:
    toc: true
    number_sections: true
    toc_float:
      collapsed: false
      smooth_scroll: false
      toc_depth: 3
    pandoc_args: '..//..//_output_kniti_html.yaml'
    includes:
      in_header: '..//..//hdga.html'
\\end{document}"

print(st_file)
## [1] "\\documentclass[12pt,english]{article}\n\\usepackage[bottom]{footmisc}\n\\usepackage[urlcolor=blue]{hyperref}\n\\begin{document}\n\\title{A Latex Testing File}\n\\author{\\href{http://fanwangecon.github.io/}{Fan Wang} \\thanks{See information \\href{https://fanwangecon.github.io/Tex4Econ/}{Tex4Econ} for more.}}\n\\maketitle\nIpsum information dolor sit amet, consectetur adipiscing elit. Integer Latex placerat nunc orci.\n\\paragraph{\\href{https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3140132}{Data}}\nVillage closure information is taken from a village head survey.\\footnote{Generally students went to schools.}\noutput:\n  pdf_document:\n    pandoc_args: '..//..//_output_kniti_pdf.yaml'\n    includes:\n      in_header: '..//..//preamble.tex'\n  html_document:\n    toc: true\n    number_sections: true\n    toc_float:\n      collapsed: false\n      smooth_scroll: false\n      toc_depth: 3\n    pandoc_args: '..//..//_output_kniti_html.yaml'\n    includes:\n      in_header: '..//..//hdga.html'\n\\end{document}"
fl_test_tex <- "_file/tex/test_fan.tex"
fileConn <- file(fl_test_tex)
writeLines(st_file, fileConn)
close(fileConn)

1.3 Open A File and Read Lines

Open and Replace Text in File:

fileConn <- file(fl_test_tex, "r")
st_file_read <- readLines(fileConn)
print(st_file_read)
##  [1] "\\documentclass[12pt,english]{article}"                                                                                                                 
##  [2] "\\usepackage[bottom]{footmisc}"                                                                                                                         
##  [3] "\\usepackage[urlcolor=blue]{hyperref}"                                                                                                                  
##  [4] "\\begin{document}"                                                                                                                                      
##  [5] "\\title{A Latex Testing File}"                                                                                                                          
##  [6] "\\author{\\href{http://fanwangecon.github.io/}{Fan Wang} \\thanks{See information \\href{https://fanwangecon.github.io/Tex4Econ/}{Tex4Econ} for more.}}"
##  [7] "\\maketitle"                                                                                                                                            
##  [8] "Ipsum information dolor sit amet, consectetur adipiscing elit. Integer Latex placerat nunc orci."                                                       
##  [9] "\\paragraph{\\href{https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3140132}{Data}}"                                                                 
## [10] "Village closure information is taken from a village head survey.\\footnote{Generally students went to schools.}"                                        
## [11] "output:"                                                                                                                                                
## [12] "  pdf_document:"                                                                                                                                        
## [13] "    pandoc_args: '..//..//_output_kniti_pdf.yaml'"                                                                                                      
## [14] "    includes:"                                                                                                                                          
## [15] "      in_header: '..//..//preamble.tex'"                                                                                                                
## [16] "  html_document:"                                                                                                                                       
## [17] "    toc: true"                                                                                                                                          
## [18] "    number_sections: true"                                                                                                                              
## [19] "    toc_float:"                                                                                                                                         
## [20] "      collapsed: false"                                                                                                                                 
## [21] "      smooth_scroll: false"                                                                                                                             
## [22] "      toc_depth: 3"                                                                                                                                     
## [23] "    pandoc_args: '..//..//_output_kniti_html.yaml'"                                                                                                     
## [24] "    includes:"                                                                                                                                          
## [25] "      in_header: '..//..//hdga.html'"                                                                                                                   
## [26] "\\end{document}"
close(fileConn)

1.4 Open a File and Replace Some Lines

Append additional strings into the file after html_document with proper spacings:

# Read in Lines from existing file
fileConn <- file(fl_test_tex, "r")
st_file_read <- readLines(fileConn)
close(fileConn)

# Search and Replace String
st_search <- "html_document:
    toc: true
    number_sections: true
    toc_float:
      collapsed: false
      smooth_scroll: false
      toc_depth: 3"
st_replace <- paste0("html_document:
    toc: true
    number_sections: true
    toc_float:
      collapsed: false
      smooth_scroll: false
      toc_depth: 3\r\n",
                     "    toc: true\r\n",
                     "    number_sections: true\r\n",
                     "    toc_float:\r\n",
                     "      collapsed: false\r\n",
                     "      smooth_scroll: false\r\n",
                     "      toc_depth: 3")

# Search and Replace
st_file_updated <- gsub(x = st_file_read,
                        pattern = st_search,
                        replacement = st_replace)

# Print
print(st_file_updated)
##  [1] "\\documentclass[12pt,english]{article}"                                                                                                                 
##  [2] "\\usepackage[bottom]{footmisc}"                                                                                                                         
##  [3] "\\usepackage[urlcolor=blue]{hyperref}"                                                                                                                  
##  [4] "\\begin{document}"                                                                                                                                      
##  [5] "\\title{A Latex Testing File}"                                                                                                                          
##  [6] "\\author{\\href{http://fanwangecon.github.io/}{Fan Wang} \\thanks{See information \\href{https://fanwangecon.github.io/Tex4Econ/}{Tex4Econ} for more.}}"
##  [7] "\\maketitle"                                                                                                                                            
##  [8] "Ipsum information dolor sit amet, consectetur adipiscing elit. Integer Latex placerat nunc orci."                                                       
##  [9] "\\paragraph{\\href{https://papers.ssrn.com/sol3/papers.cfm?abstract_id=3140132}{Data}}"                                                                 
## [10] "Village closure information is taken from a village head survey.\\footnote{Generally students went to schools.}"                                        
## [11] "output:"                                                                                                                                                
## [12] "  pdf_document:"                                                                                                                                        
## [13] "    pandoc_args: '..//..//_output_kniti_pdf.yaml'"                                                                                                      
## [14] "    includes:"                                                                                                                                          
## [15] "      in_header: '..//..//preamble.tex'"                                                                                                                
## [16] "  html_document:"                                                                                                                                       
## [17] "    toc: true"                                                                                                                                          
## [18] "    number_sections: true"                                                                                                                              
## [19] "    toc_float:"                                                                                                                                         
## [20] "      collapsed: false"                                                                                                                                 
## [21] "      smooth_scroll: false"                                                                                                                             
## [22] "      toc_depth: 3"                                                                                                                                     
## [23] "    pandoc_args: '..//..//_output_kniti_html.yaml'"                                                                                                     
## [24] "    includes:"                                                                                                                                          
## [25] "      in_header: '..//..//hdga.html'"                                                                                                                   
## [26] "\\end{document}"
# Save Updated File
fl_srcrep_tex <- "_file/tex/test_fan_search_replace.tex"
fileConn_sr <- file(fl_srcrep_tex)
writeLines(st_file_updated, fileConn_sr)
close(fileConn_sr)