R Download.File Issue with Excel Workbook - r

I'm trying to download an Excel workbook using R's download.file function.
When I download the file manually (using Internet Explorer or Chrome, right click & save as) then the file downloads and I can then open it in Excel without any problems.
When I use download.file in R, the file downloads and reports the correct file size. However when I then try to open the downloaded xls file in Excel 2010 I get the following error message:
Excel found unreadable content in 'test.xls'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes.
When I click Yes, nothing happens.
I've also tried accessing the file directly using the R package xlsx, this also fails.

You can try to download your file in binary mode (default for download.file is ASCII mode) with the mode argument. Something like :
download.file(myurl, mydestfile, mode="wb")

Related

How to Read in xls file that requires permission to open in R

I have several xls files I need to read in and combine into one dataframe. I try
df <- readxl::read_excel("file.xls")
or
df <- readxl::read_xls("file.xls")
but neither works. I get the following error
Error:
filepath: /Users/.../.../file.xls
libxls error: Unable to open file
I believe the issue is that every time I open the file in Excel, I am asked if I trust this file before I can open it. Is there anyway around it?
I also am operating on a mac, and I want to avoid library(xlsx) or other packages that have Java dependencies.
UPDATE: I had the idea of just going into each file to click "Save As..." and change the format to xlsx instead of xls, but the default file format that showed was an Excel 2004 XML Spreadsheet (.xml). Does that suggest that my file is actually a xml file even though the extension in the name is .xls?

How do you edit a .rds file in RStudio?

I am trying to run an R script that I've inherited from a colleague. This script references a .rds file called config.rds. It stores some configuration settings. I need to change those settings. However, when I attempt to open the file in the Rstudio editor, a "Load R object" prompt pops up. I cannot figure out how to open the file for editing.
You can't open the file for editing - it is a binary file that stores the internal representation of R data objects.
You can only really read it into R to create a new R object, and then save a modified copy of that R object into a new or (the same) .RDS file. Example:
config = readRDS("config.rds")
config$username = "fnord"
saveRDS(config, "config.rds")

What R command to use to force download files from iCloud

On a mac using iCloud file optimization, large files that are seldom used are uploaded to iCloud and only a small pointer file is left. When I look for the file in Finder, I see the file name and to the left is an icon that indicates that the file is in the cloud. To access the file, I click on the icon and the file is downloaded. With the file.exist command, R returns FALSE for the existence of the file. But after some research I found that the file link is stored in a directory below ~/Library/Mobile\ Documents/com~apple~CloudDocsand the file name is changed to xxx.icloud where xxx is the original file name.
Here's an example of the path to a a directory that holds a .icloud file from a shell in my mac
/Users/gcn/Library/Mobile Documents/com~apple~CloudDocs/Documents/workspace/nutmod/data-raw/NutrientData
I can query for the existence of the file with exists(xxx.icloud). But how do I tell my mac to download the iCloud file and then read it in? Using something like read.table or read.csv doesn't work because the pointer file is not csv.
You can read a csv file directly from a iCloud folder on the Mac by using the path to that folder. Get the path by finding it in the finder. Then right click on the filename at the bottom of the finder window where it shows all the folders leading to the file and choose: Copy "YourFile" as Pathname.
That path will look something like this:
"/Users/NAME/Library/Mobile
Documents/com~apple~CloudDoc/Docs/YourFile.csv"
Use that in your read code:
iCloudDat <- read_csv("/Users/NAME/Library/Mobile Documents/com~apple~CloudDocs/Documents/YourFileName.csv")
That should work.
If the extension isn't .txt or .csv read.table and read.csv won't work.
you have to download the file and extract the tables to a readable format.
you can download the file using download.file() which is is the utils basic library.

R code asking Excel to open a file

Here is a real world setting:
We download *.xls or *.xlsx files from the web.
We open the *.xls or *.xlsx files by double-clicking the files one by one (assuming Excel is installed) in order to eye-ball the file content.
This could become tedious (relatively speaking) if a lot of excel files were downloaded regularly, filed into different directories and need to check the file content one by one.
For example, we downloaded a file as below:
url <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FDATA.gov_NGAP.xlsx"
file01 <- file.path(getwd(), "NGAP.xlsx")
download.file(url, file01, mode = "wb")
What is the R code to instruct Excel to open this file? Of course we could go to download directory and double-click the file to open it. But want to instruct Excel to open the file by running R code. Thanks for any pointers provided!
The base function file.show opens files with the default application registered to handle the given file extension.
u <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2FDATA.gov_NGAP.xlsx"
download.file(u, f <- tempfile(fileext='.xlsx'), mode = "wb")
file.show(f)

LinqToExcel External table is not in the expected format

I've been using LinqToExcel to parse an exel document for a while and suddenly it's stoped working.
I'm getting the following error:
External table is not in the expected format.
Any ideas why this is happening? Or how to fix?
if (File.Exists(filenameFull))
{
var excel = new ExcelQueryFactory(filenameFull);
IList<Row> scanningRangesRows =
excel.Worksheet("B - Scanning Ranges").ToList();
I was using version LinqToExcel 1.6.3, when the problem started happening I updated to the latest version LinqToExcel 1.6.6 to no avail.
I've just noticed that the file I'm downloading is significantly smaller than previous verisons. I opened it in notepad and I can see [Content_Types].xml amongst the binary data. So it appears that the data source is now being saved as an xml represention of the xls file with the same extension. When I open the same file manually in Excel it popup with
The file you are trying to open '', is in a different format
than specified by the file extension. Verify that the file is from a
trusted source before opening the file. Do you want to open the file
now?
On clicking yes the file still opens and looks the same as previous versions.
It's probably something to do with the file.
Maybe it's being saved as an .xlsx type of file. Can you try renaming the file extenstion to .xlsx and see if that works.

Resources