How do I update multiple individual Excel files with PivotTables in R? - r

I built a script that pulls from the data warehouse and cleans the data in a format to be export as a csv file. I take the file and paste it into an Excel file that already has a PivotTable with slicers on another tab. The thing is that I am creating a report for every sales rep with their own sales using an Excel macro. There are 300 sales reps that I am sending this unique report to. Right now, I use an Excel VBA macro that creates individual reports and sends them to each sales rep via Outlook which takes a very long time and slows down my laptop.
Is there a way to use R to auto update an Excel file for each sales rep data and refreshes the PivotTable without having to open them individually? I also want to send an automated email which attaches the Excel file and sends to each sales rep via Outlook. I have done it for text and csv files via R but not in a loop manner to over 100 sales rep.

For sending data from R to Excel, I use excel.link package by Gregory Demin (I believe it is a Windows only package). I found it most useful. https://cran.r-project.org/web/packages/excel.link/excel.link.pdf
Here is example workflow on Windows:
Assuming you have a data.frame currently in your r session under object d
and you have an Excel workbook saved under c:\mybook.xlsb
# kill an existing Excel process running (if any)
shell("taskkill /im EXCEL.exe /f /t")
# open a workbook from 'c:\\mybook.xlsb'
xl.workbook.open("c:\\mybook.xlsb")
xl.workbook.activate("mybook.xlsb")
mybook = xl.get.excel()
# paste the data from dataframe object `d` to the active sheet in cell (1,1) which corresponds to cell A1
xl.write(d,row.names=FALSE,xl.rng=mybook[["Activesheet"]]$Cells(1,1))
# activate a different sheet named 'mysheet'
xl.sheet.activate("mysheet")
# do more stuff with 'mysheet' here...
# save your workbook to a different copy once done
xl.workbook.save("c:\mybook_current_version".xlsb"))
# kill Excel process again to quit Excel
shell("taskkill /im EXCEL.exe /f /t")
Check the documentation for more examples.
For sending e-mails with attachments, look at sendmailR package.

Related

How do I append the results of a local job script in R into an Excel file?

I'm running dozens of scripts in R/RStudio as local jobs and I have an Excel File where I'm consolidating the results. Each run produces two values that I copy/paste into Excel. Is there a way to automate the saving of the results directly into the respective column of the Excel file?
For example, my spreadsheet is called FFNN with columns variance and deviance and each time my script runs I would like the respective results to be saved into a cell in those columns.
Thanks!

How to fetch data in R by executing command in excel?

I have to fetch data from Reuters and they provide an excel plugin for that. The problem is that my excel is crashing if I try to fetch too many variables at the same time. I was wondering whether I can do it from R via some excel connection.
In general I want to give the command to excel (from R), fetch data and get the data back in R for analysis. The process has to be repeated a number of times.
=TR("SCREEN(U(IN(Equity(active,public,primary))/*UNV:Public*/))",A1:K10,"PERIOD:FY2015 NULL:-- curn=USD RH=In CH=Fd",A6)
I get the variable name from (A1:K10) and then the output is stored from cell A6 onwards.
The answer https://stackoverflow.com/a/43222477/1389469 here points to RDCOMClient library but I am not able to run the macro from R as described there.
Another guide here https://cran.r-project.org/web/packages/excel.link/excel.link.pdf talks about how to read/write data from an open excel workbook but doesn't tell how to execute commands in excel from R.

Fetch data from an open excel sheet into R?

I am wondering is it possible to read an excel file that is currently open, and capture things you manually test into R?
I have an excel file opened (in Windows). In my excel, I have connected to a SSAS cube. And I do some manipulations using PivotTable Fields (like changing columns, rows, and filters) to understand the data. I would like to import some of the results I see in excel into R to create a report. (I mean without manually copy/paste the results into R or saving excel sheets to read them later). Is this a possible thing to do in R?
UPDATE
I was able to find an answer. Thanks to awesome package created by Andri Signorell.
library(DescTools)
fxls<-GetCurrXL()
tttt<-XLGetRange(header=TRUE)
I was able to find an answer. Thanks to awesome package created by Andri Signorell.
library(DescTools)
fxls<-GetCurrXL()
tttt<-XLGetRange(header=TRUE)
Copy the values you are interested in (in a single spread sheet at a time) to clipboard.
Then
dat = read.table('clipboard', header = TRUE, sep = "\t")
You can save the final excel spreadsheet as a csv file (comma separated).
Then use read.csv("filename") in R and go from there. Alternatively, you can use read.table("filename",sep=",") which is the more general version of read.csv(). For tab separated files, use sep="\t" and so forth.
I will assume this blog post will be useful: http://www.r-bloggers.com/a-million-ways-to-connect-r-and-excel/
In the R console, you can type
?read.table
for more information on the arguments and uses of this function. You can just repeat the same call in R after Excel sheet changes have been saved.

How to open SAS files using Excel?

I have a set of SAS data sets and I want to open it using Excel or R. I don't have a SAS software with me so i can't use the export option in it. Is there any converter that converts from SAS7BDAT to excel?
Thanks
I help develop the Colectica for Excel addin, which opens SAS data files in Excel. No SAS software or ODBC configurations are required. The addin directly reads the SAS file and then inserts the data and metadata into your worksheet.
Imports SAS .sas7bdat data and column names
Imports SAS .sas7bcat formats and value labels when avalaible
The Excel addin is downloadable from http://www.colectica.com/software/colecticaforexcel
Documentation is available in the user manual.
You could use SAS add in for Microsoft office to open the SAS dataset in Excel. Not sure if it is free though.
http://support.sas.com/software/products/addin/
As Reese suggested you can use - SAS Universal Viewer , its free!!
Here is the link :-
https://support.sas.com/downloads/browse.htm?fil=&cat=74
Or you can download SAS University Edition, which is also free, it is more than just a viewer, you can write and execute programs in here.
http://www.sas.com/en_us/software/university-edition/download-software.html
Here a quick-and-dirty python five-liner to convert a .xpt file to .csv
import pandas as pd
FILE_PATH = "(directory containing file)"
FILE = "ABC" # filename itself (without suffix)
# Note: might need to substitute the column name of the index (in quotes) for "None" here
df = pd.read_sas(FILE_PATH + FILE + '.XPT', index=None)
df.to_csv(FILE_PATH + FILE + '.csv')
Hopefully this might help someone
I came across the same "need" and after some research here and there, I found a nice and easy way with R and the latest version of RStudio (as per 2020 June date - the FREE one). Using it, you can open various formats of files and RStudio generates for you the R script it ran behind. You can use this as a starting point, in order to have the .sas7bdat file opened, and then do the conversion step.
Steps to follow in order to import the file using the RStudio "visual" way: Evironment tab -> Import Dataset -> From SAS...
It will ask you to import the haven library. After the installation you will have a tab with the preview of the data within the file and also the R script ran behind which will look like this:
library(haven)
aux <- read_sas("//PATH_ON_YOUR_MACHINE_TO_FILE/actual_file.sas7bdat", NULL)
View(aux)
Notice the NULL there, it has the purpose of converting empty strings to NULL.
But wait, we also need to convert it to a .csv file in order to have the final job done. For this you simply add below those lines from above the following:
write.csv(aux, "actual_file.csv")
Which will produce within the same folder with the original SAS file, the desired .CSV one. If you want to have ";" as separator instead on "," use write.csv2(aux, "actual_file.csv"). Anyway Strings are enclosed by " " so it should be fine.

Importing .csv files with Sys.Dates()

I have a .csv dataset that gets dumped everyday which I use to generate a daily list for tracking participants using a R script. I would like to automate this R script, however in order to do so, I need to read in the .csv using Sys.Date().
The .csv dataset is named: DumpedList_2013-11-27 (The date will always be today's date).
I would like to import this into the script, like I would for .Rdata file.
load(paste('/srv/Data/Baseline2/baseline2_', Sys.Date(), '.Rdata',sep=''))
What is the equivalent of the command above for reading in .csv files?
I have tried load and read.csv commands, but get error messages:
data=read.csv('P:/DirectoryPath/DumpedList_',Sys.Date(),'.csv')
I also attempted to create todaydate=Sys.Date() and then used it to load the data, but error messages again. a=load(paste("P:/DirectoryPath/DumpedList_",todaydate,".csv"))
Any insight?
By default paste will separate with spaces, use paste0 to join strings together seamlessly:
read.csv(paste0('P:/DirectoryPath/DumpedList_',Sys.Date(),'.csv'))

Resources