Guest User

Untitled

a guest
Aug 10th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #
  2. # Author: Cristian E. Nuno
  3. # Purpose: Import worksheets within an Excel workbook as a list of data frames
  4. # Date: August 10, 2018
  5.  
  6. ImportXLSX <- function( name.of.file, na.strings = c("", "NA", "n/a", "N/A") ){
  7.  
  8. # Input:
  9. # 1. name.of.file: the file path to the desired Excel workbook
  10. # 2. na.strings: a character vector of strings which are are
  11. # intended
  12. #
  13. # Output:
  14. # 1. A list of data.frames, all of which are named according to their
  15. # worksheet name.
  16.  
  17.  
  18. # Build in checks to ensure that users
  19. # contain the 'openxlsx' package from the CRAN
  20. if( require( openxlsx ) == FALSE ){
  21. install.packages( pkgs = "openxlsx" )
  22. }
  23.  
  24. # load necessary package
  25. require( openxlsx )
  26.  
  27. # store the names of the sheets from the
  28. # workbook specified in name.of.fil
  29. sheet.data <- names( openxlsx::loadWorkbook( file = name.of.file ) )
  30.  
  31. # store each worksheet as its own data frame
  32. list.df <- lapply( X = sheet.data
  33. , FUN = function( i )
  34. openxlsx::read.xlsx( xlsxFile = name.of.file
  35. , sheet = i
  36. , na.strings = na.strings ) )
  37.  
  38. # name each data frame according to its worksheet name
  39. names( list.df ) <- sheet.data
  40.  
  41. # return list.df to the Global Environment
  42. return( list.df )
  43.  
  44. } # end of ImportXLSX() function
  45.  
  46. # end of script #
Add Comment
Please, Sign In to add comment