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)
Related
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?
I can't get write.csv or write.table to work in the r editor of BlueSky Statistics.
I usually just use this format in RStudio and it works perfectly.
write.csv(df, "zzz.csv")
Any hints?"
The default install location for BlueSky Statistics is 'C:\Program Files', where by default, there is no write permission (for creating or deleting files). Also, saving a file in the install location is not safe, as the file may get lost/deleted when the application is uninstalled. So it is always good to save your file(s) in your own folder(s) where you also have write permission.
In short, try to provide a writable location/path in write.csv() or other similar functions/commands.
See example below:
To save your file to the Desktop folder.
write.csv(df, "C:/Users/<YourUsername>/Desktop/zzz.csv")
Note: use forward slash(/) as a path separator.
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.
I am running some R code on a Windows computer using RStudio and my code generates Excel files and netCDF files periodically (dozens of them eventually). I don't want them to clutter my working directory. Is there a way to save the files to a directory called "Output" (ex: C:/.../original file path/Output) in the parent directory? I would like a way to change my current working directory to a different directory. I understand there is getwd() and setwd() but how do I set the path to the output directory without typing out the entire windows path (for example: setwd(current source file path for windows or Mac/output). My collaborator uses a Mac and he would have his output stored there as well.
You have a file argument in your write* function. If your Output directory is in your working directory, it works like this:
write.xlsx(df, file = "Output/table.xlsx")
write.csv(df, file = "Output/table.csv")
You can specify an argument in your write.csv function and other similar write functions which specifies your path.
#Output path
OutPath<- "C:/blah/blahblah/op/"
#Table to dump as output
OutTbl <- iris
write.csv(OutTbl, file = OutPath)
Source: https://stat.ethz.ch/R-manual/R-devel/library/utils/html/write.table.html
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")