document.RMD template - Version 1 - 5/6/2024

Note: to replace the current rstudio templatejust rename your current "document.RMD" and copy this one into "C:\Program Files\RStudio\resources\app\resources\templates" or your install directory

Analysis overview

setup and environment including helpful function fs() to "forwardslash" paths on your clipboard

#helpful environment modifications

  #(optional) set number of columns to display in "View()" function to 1000 - revert to 50 if you don't like the behavior
  if(F) rstudioapi::writeRStudioPreference("data_viewer_max_columns", 1000L)

  #turn off scientific notation 
  options(scipen = 10E6)

  #prevent strings being read as factors
  options(stringsAsFactors = F)

# helpful functions

  #convenience function for more usable version of writeClipboard - overcomes newline issues
  writeClipboard2 = function(x) cat(x, file="clipboard")

  #generic file path editing function - forwards or backwards
  reslash<- function(x=NA, slash=c("back","forward")){
    if(is.na(x)) x = readClipboard()
    if(slash[1]=="back") path <- shQuote(gsub("/","\\\\",gsub("\\", "\\\\", x, fixed = TRUE), fixed = TRUE))
    if(slash[1]=="forward") path <- shQuote(gsub("\\", "/", x, fixed = TRUE))
    writeClipboard2(path)
    return(path)
  }

  #correctly escape back slashes and quote path - a parameterized version o f   
  bs=function(x=NA){reslash(x,slash="back")}

  #correctly escape back slashes and quote path   
  fs=function(x=NA){reslash(x,slash="forward")}

  #function to grab names from data.frame, quote them, place commas between them, and send to clipboard
  nmsVec=function(x){x=paste("c('",paste(names(x),collapse="','"),"')",sep=""); writeClipboard2(x);return(x)}

  #mse and rmse functions
  mse = function(err,na.rm=T){mean(err^2,na.rm=na.rm)} 
  rmse = function(err,na.rm=T){sqrt(mean(err^2,na.rm=na.rm))}   

  #file_tif = RForInvt::file_increment(file.path("c:/temp","some_plots.tif"), increment=T)
  #con_this = DBI::dbConnect(RSQLite::SQLite(), file_db )
  #tiff(file_tif , width = 6, height = 4, units = "in", res = 600)
  #dev.off()

load data - some examples of ways to load various data types.

#examples
if(F){ 

  library(RSQLite)
  library(DBI)

    #Example make some fake data and write to versioned files 

    #create new output file paths for this version - increment = T  
    dir_temp = "c:/temp"
    file_csv = RForInvt::file_increment(file.path(dir_temp,"some_data.csv"), increment=T)
    file_rds = RForInvt::file_increment(file.path(dir_temp,"some_data.RDS"), increment=T)
    file_db = RForInvt::file_increment(file.path(dir_temp,"some_data.sqlite"), increment=T)

    #simulate / write small simulated table of data

        #simulate
        test_dat = data.frame(plot=1:5,some_y=rnorm(5))

        #write csv
        write.csv(test_dat,  file_csv )

        #save R binary format
        saveRDS(test_dat, file_rds)

        #save to database
      con_this = dbConnect(RSQLite::SQLite(), file_db )
        dbWriteTable( con_this , "pldat" , test_dat , overwrite = T )
      dbDisconnect(con_this)  

    #grab latest versions of data - increment = F

      #prep file names
    dir_temp = "c:/temp"
    file_csv = RForInvt::file_increment(file.path(dir_temp,"some_data.csv"), increment=F)
    file_rds = RForInvt::file_increment(file.path(dir_temp,"some_data.RDS"), increment=F)
    file_db = RForInvt::file_increment(file.path(dir_temp,"some_data.sqlite"), increment=F)

    #read data csv/rds
    dat_csv = read.csv(file_csv)
    dat_rds = readRDS(file_rds)

    #read sqlite data
    con_this = dbConnect(RSQLite::SQLite(), file_db )
        dat_db = dbReadTable(con_this ,"pldat" )
    dbDisconnect(con_this)  

    #add sf::st_write example

    #add terra::writeRaster example

}

example of how to save versions figures to tiff and pdf

    #create versioned files names
  dir_temp = "c:/temp"
    file_tif = RForInvt::file_increment(file.path(dir_temp,"some_plots.tif"), increment=T)
    file_pdf = RForInvt::file_increment(file.path(dir_temp,"some_plots.pdf"), increment=T)

    #simulate / write small simulated table of data

        #simulate
        test_dat1 = data.frame( plot=1:5,y=rnorm(5), x=rnorm(5) )

        #save tif - show how to combine exponent with regular text
        tiff(file_tif , width = 6, height = 4, units = "in", res = 600)
          plot(y~x, data = test_dat1 , main = "test_dat1", xlab=expression(paste("bigfoot height ",cm^2)), ylab="ft")
        dev.off()

        #save pdf - show how to save 2 figures in same file
        pdf(file_pdf , width = 6, height = 4, units = "in", res = 600)
          plot(y~x, data = test_dat1 , main = "test_dat1", xlab=expression(paste("bigfoot height ",cm^2)), ylab="ft")
          plot(y~x, data = test_dat1 , main = "test_dat1", xlab=expression(paste("bigfoot height ",cm^2)), ylab="ft")
        dev.off()









jstrunk001/RForInvt documentation built on April 17, 2025, 5:02 p.m.