Reading only one Excel book into R - r

I am working with an Excel file. It has 3 books. I need help with extracting only one of the books into R. I did a Google search and could not glean the solution from the information. I am working on a MacBook; I am running the latest version of R.
More specifically, here is the question.
The data set has three workbooks "Sales", "Resources", and "Supplies". How do you read in only the items from the "Sales" workbook?
Thank you.

I think the best way, is to save the worksheet you need as csv, and use read.csv in R. but if you prefer to read directly the excel file:
Use the package XLConnect
df <- readWorksheetFromFile("excel_file.xlsx", sheet = "Sales")

Related

Read From/Write To an .xlxb spreadsheet

I am trying to read a .xlsb spreadsheet in to a data frame, create a new data frame after performing some manipulations on the data frame previously read and then write the new data frame on a newly added worksheet in the same workbook and then close the workbook saving the file.
I have looked around and found a lot of packages to do so for all formats of workbooks except for .xlsb format. I found excel.link packages but couldn't get it to work.
I also read about a package called 'xlsb' but seemingly its not available for R version 4.1.2. If anyone knows if it works with an older or newer version of R, please advise and I will see if I can get that version installed at my workplace.
Any help/suggestions/pointers shall be greatly appreciated.
Best regards
Deepak

Is there a way to accelerate formatted table writing from R to excel?

I have a 174603 rows and 178 column dataframe, which I'm importing to Excel using openxlsx::saveWorkbook, (Using this package to obtain the aforementioned format of cells, with colors, header styles and so on). But the process is extremely slow, (depending on the amount of memory used by the machine it can take from 7 to 17 minutes!!) and I need a way to reduce this significantly (Doesn't need to be seconds, but anything bellow 5 min would be OK)
I've already searched other questions but they all seem to focus either in exporting to R (I have no problem with this) or writing non-formatted files to R (using write.csv and other options of the like)
Apparently I can't use xlsx package because of the settings on my computer (industrial computer, Check comments on This question)
Any suggestions regarding packages or other functionalities inside this package to make this run faster would be highly appreciated.
This question has some time ,but I had the same problem as you and came up with a solution worth mentioning.
There is package called writexl that has implemented a way to export a data frame to Excel using the C library libxlsxwriter. You can export to excel using the next code:
library(writexl)
writexl::write_xlsx(df, "Excel.xlsx",format_headers = TRUE)
The parameter format_headers only apply centered and bold titles, but I had edited the C code of the its source in github writexl library made by ropensci.
You can download it or clone it. Inside src folder you can edit write_xlsx.c file.
For example in the part that he is inserting the header format
//how to format headers (bold + center)
lxw_format * title = workbook_add_format(workbook);
format_set_bold(title);
format_set_align(title, LXW_ALIGN_CENTER);
you can add this lines to add background color to the header
format_set_pattern (title, LXW_PATTERN_SOLID);
format_set_bg_color(title, 0x8DC4E4);
There are lots of formating you can do searching in the libxlsxwriter library
When you have finished editing that file and given you have the source code in a folder called writexl, you can build and install the edited package by
shell("R CMD build writexl")
install.packages("writexl_1.2.tar.gz", repos = NULL)
Exporting again using the first chunk of code will generate the Excel with formats and faster than any other library I know about.
Hope this helps.
Have you tried ;
write.table(GroupsAlldata, file = 'Groupsalldata.txt')
in order to obtain it in txt format.
Then on Excel, you can simply transfer you can 'text to column' to put your data into a table
good luck

Import R example dataset into excel

I am using the topmodel package in R with the huagrahuma dataset that comes with the package.
I would like to bring all these variables into excel, edit those as per my requirement & then use the base in R.
package & data: https://rdrr.io/cran/topmodel/man/huagrahuma.html
You can save datasets using a command like write.csv() or one of the writing functions from the readr, readxl or xlsx packages (for example). Using:
?write.csv()
Will show you how to use the function. Once you have it saved as a .csv file on your computer, you can open it with Excel and do what you need with it.
Edit: Following G5W's comment below, you could try extracting elements of this list and saving those, depending on what you actually want to change. To be honest, with a list structure, you are better off changing the data in R, using any of the apply family of functions, or the purrr package. R is much better than Excel for transforming/tidying data, so why not use it? :-)

How to read specific excel table into R

I have 2 tables, Table_1 and Table_2, on one sheet (sheet_5) in my excel workbook myWorkbook.xlsx. I know there are plenty of packages that allow you to specify which sheet to load in, but is there a way to load in only the table(s) you want? In this case, I want to load Table_2 only.
Thanks
The most recent version of readxl has the ability to set the range. IMHO this is BY FAR the best excel read in package for R.
See the part of this post entitled "Specifying the data rectangle": https://blog.rstudio.org/2017/04/19/readxl-1-0-0/
the syntax should be very familiar to an excel user.
Also please see this post for asking questions on SO:
How to make a great R reproducible example?

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.

Resources