SHARE
TWEET

[R] MongoDB Connection Example with export data

rodrigosantosbr Aug 14th, 2019 (edited) 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. library(devtools)
  2. library("mongolite")
  3. library(xlsx)
  4. library(xtable)
  5.  
  6. library(gridExtra)
  7. library(grid)
  8.  
  9. # ---------------------------------------------
  10. # connect to your local mongodb
  11. # ---------------------------------------------
  12. country_data <- mongo(collection = "country",
  13.                       db = "auditor",
  14.                       url = "mongodb://localhost")
  15.  
  16. # ---------------------------------------------
  17. # count data
  18. # ---------------------------------------------
  19. total <- country_data$count(
  20.   query='{}'
  21. )
  22. # print(total)
  23.  
  24. # ---------------------------------------------
  25. # get all data
  26. # ---------------------------------------------
  27. data_all_1 <- country_data$find('{}')
  28.  
  29. # ---------------------------------------------
  30. # get specific columns
  31. # ---------------------------------------------
  32. data_all_2 <- country_data$find(
  33.   query='{}',
  34.   fields = '{"_id" : true, "name_br" : true}',
  35.   limit=0
  36. )
  37. #print(data_all_2)
  38.  
  39. # ---------------------------------------------
  40. # export data frame to xlsx
  41. # ---------------------------------------------
  42. write.xlsx(data_all_2, "D:/mydata2019.xlsx")
  43.  
  44. # ---------------------------------------------
  45. # latex table code
  46. # ---------------------------------------------
  47. my_table <- xtable(data_all_2)
  48. print(my_table)
  49.  
  50. # ---------------------------------------------
  51. # html table code
  52. # ---------------------------------------------
  53. print(my_table, type = "html")
  54.  
  55. # ---------------------------------------------
  56. # data iteraction with limit
  57. # ---------------------------------------------
  58. data_all_3 <- country_data$iterate(
  59.   '{}',
  60.   fields = '{"_id" : true, "name_br" : true}',
  61.   limit = 7
  62. )
  63. while(!is.null(x <- data_all_3$one())){
  64.   output = paste("id =",x$"_id", "| name_br =", x$name_br, sep = " ")
  65.   print(output)
  66. }
  67.  
  68. # ---------------------------------------------
  69. # export data frame to pdf
  70. # ---------------------------------------------
  71. maxrow=35
  72. npages=ceiling(nrow(data_all_2)/maxrow)
  73. pdf(file='D:/data_in_pdf.pdf',height=11,width = 8.5)
  74. idx=seq(1, maxrow)
  75. grid.table(data_all_2[idx, ],rows=NULL)
  76. for(i in 2:npages){
  77.   grid.newpage()
  78.   if(i*maxrow <= nrow(data_all_2)){
  79.     idx = seq(1+((i-1)*maxrow), i*maxrow)
  80.   }
  81.   else{
  82.     idx=seq(1+((i-1)*maxrow), nrow(data_all_2))
  83.   }
  84.   grid.table(data_all_2[idx, ],rows=NULL)
  85. }
  86. dev.off()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top