R’ and RStudio are great but sometimes it’s better the just export your data to exploit them elsewhere or just show them to other people. Here is a review of possible techniques:
Export your data into a CSV
assuming your data is store inside df var, fairly simple:
1 2 3 4 |
#setup where to write the file setwd("~/Desktop") # en write the file write.csv(df, "data.csv") |
Export your data into an excel file
A little bit more complex, we’ll use the ‘xlsx’ package
1 2 3 4 5 6 7 8 9 10 11 |
#setup where to write the file setwd("~/Desktop") # if the package is not instal yet, run this # install.packages("xlsx") # Loading the package library(xlsx) # we write the file write.xlsx(df, "data.xlsx") |
A few more tips for you:
I’ll like to use the sheetName option to explicitly name the tab. The default name is “Sheet1”. Quite useful to have a record of when the file has been generated for example. Replace last instruction what follows and you’ll be able to know.
1 |
write.xlsx(df, "data.xlsx", sheetName=format(Sys.Date(), "%d %b %Y")) |
Another good one that I like is to send the excel file to a Shared folder directly. Replace first instruction by
1 |
setwd("/Users/me/Dropbox/Public") |
Of course, replace the file path by yours.
Send your data by email
If the data to send it not to big, another interesting idea is to send it by email using the ‘gmailr’ package.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#install.packages("gmailr") #install.packages("tableHTML") # Packages loading library(gmailr) # This one is usefull to transform a data frame into an HTML <table> library(tableHTML) # This will allow you to connect to gmail # replace the fake value by your key and secret # more info here: https://gargle.r-lib.org/articles/get-api-credentials.html gm_auth_configure("mykey.apps.googleusercontent.com", "mysecret") #transform the data frame 'df' to a html table msg = tableHTML(df) # Construct email test_email <- gm_mime() %>% gm_subject("Email title") %>% gm_html_body(paste("Hi Mate,<br /> Here are the data you requested:<code>", msg,"<br /><br />Kind regards,<br />François")) # end send it gm_send_message(test_email) |