R/outtable_ms.R

#' Return Malaysiastock.biz table
#'
#' Return table from the url given
#' 
#' @param url url to scrap table from
#' @param tablej jth table to scrap
#' @keywords out table malaysiastock.biz 
#' @export
#' @examples  
#' outtable_ms(url = "http://www.malaysiastock.biz/Latest-Announcement.aspx", tablej = 10)
#'
outtable_ms <- function(url, tablej = 10){
      file <- read_html(url)
      tables <- html_nodes(file, "table")
      table1 <- html_table(tables[tablej], fill = TRUE, header = TRUE)[[1]] #tables[4] for compressed data
      return(table1)
}


#' Write a csv file
#'
#' Specify file name and output directory
#' 
#' @param df dataframe to write to csv
#' @param fname File name
#' @param outdir Output directory
#' @param replace if TRUE, then replace any existing file; else create new file
#' @keywords write csv 0  
#' @export
#' @examples  
#' write.csv0(df = dfout, fname = "outputdf", outdir = "C:/Users/User/Desktop")
#' 
write.csv0 <- function(df, fname, outdir = "~", replace = FALSE, altdir = "~"){
      if( !(substr(outdir, nchar(outdir), nchar(outdir)) == "/") ){
            outdir <- paste0(outdir, "/")
      }
      
      fname.ext <- paste0(outdir, fname, ".csv")
      
      if(!replace){
            i = 1
            while (file.exists(fname.ext)) {
                  fname.ext <- paste0(altdir, fname, "_", i, ".csv")
                  i <- i+1
            }
      }
      
      write.csv(df, fname.ext, row.names = F)
      
      return(fname.ext);
}
junyitt/function0 documentation built on May 20, 2019, 11:16 a.m.