Sys.glob Directory Error - r

I'm attempting to load in multiple tripdata csv files from a directory that I defined in R as my current directory. I keep getting this dirmark error, and I'm sure that it's a syntax error in the way I'm defining my path.
data_dir <- '.'
Error in Sys.glob("%s/*-tripdata.csv", data_dir) :
invalid 'dirmark' argument

It appears that you are trying to use sprintf formats (i.e. %s) without actually calling sprintf
Something like
Sys.glob('./*-tripdata.csv')
should work
or
Sys.glob(sprintf('%s/*-tripdata.csv',data_dir))

Related

R error in fwrite: name of file to write is an invalid argument?

I've got a long code in R where I want to write a csv file multiple times. I use fwrite for this. The code has worked in the past, but now gives an error:
fwrite(IDs, "IDS.csv")
Error in fwrite(IDs, "IDS.csv") : Invalid argument: 'IDS.csv'
IDs.csv is supposed to be the name of the csv file written, I don't see how this can be an invalid argument? Adding file= doesn't solve the problem.
I can use write.csv instead but the fwrite function is used a lot in this code and I don't want to have to change all of them.

How to code data files from excel into Rstudio

I am trying to import data from excel into R (stats class), but we are not allowed to use the import function. We have to code it in. I got help last week from a graduate student, but I think because I got a new laptop over the weekend, the Rmd file cannot be used as my reference markdown anymore. Here is what I've tried inputting:
library(tidyverse)
data.frame(readxl::read_excel(path = "./data/thedataweprovide.xlsx", sheet = "Problem 1"))
Error: `path` does not exist: ‘./data/thedataweprovide.xlsx’
> setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1")
Error in setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1") :
cannot change working directory
> pone
Error: object 'pone' not found
> setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1")
Error in setwd("C:/Users/Jasmine Ferrell/Documents/BME_Statistical_Methods/Project_1") :
cannot change working directory
> setwd("C:\Users\ferre\OneDrive\Documents\BME_Statistical_Methods\Project_1")
Error: '\U' used without hex digits in character string starting ""C:\U"
> setwd(C:/Users/ferre/OneDrive\Documents/BME_Statistical_Methods/Project_1)
Error: unexpected '/' in "setwd(C:/"
It looks like your paths are the problem. You could first try to get the working directory you are currently in with the function getwd() this may help you to figure out how you could correct your paths. Do not use single Backslashes (\) in paths, only use foreward-slashes (/).
As saae mentioned its a problem with path. Try these and should be able to get it working.
Type in your Rstudio
getwd()
This gives the current path which is currently set. If you are files are somewhere else, then you won't find it.
To change the path
go to your file location and copy the path. For example in windows, you go to the folder and click on properties, there you will see location. Copy the location and add it to the below code.
setwd("//abcdefg/somerandomguy/Profile/Desktop/My files/R_project")
Then you can run your code to read the files.

Open a file with special characters in the name with R function read.dbf

I'm trying to use the read.dbf function in R, but the name of the file contains a special character 'ñ' from spanish.
I changed the name of the file, removing the special character. The thing is that there are a lot of files with similar names and maybe this won't be the best to do.
read.dbf('Soporte_Plantilla_Vigor_Total_Daños_201806_Inicial_Vigor.dbf')
I'm getting the following error:
Error in read.dbf("Soporte_Plantilla_Vigor_Total_Daños_201806_Inicial_Vigor.dbf") :
unable to open DBF file
I want to be able to read this type of file names without changing them.

r function unzip error 1 in extracting from zip file

Environment:
Windows 7 OS
RStudio Version 0.99.491
I have been programming in R for about 4 months via the Coursera Data Science curriculum, but I have NEVER been successful in using the unzip function.
I've looked at the forums for hours for potential solutions, syntax problems, undefined arguments, etc., but to no avail. I eventually unzip the contents manually and proceed with the assignment, but I am tired of not knowing why it is not working.
Here are a few examples of the error:
fileName <- "StormData.zip"
unzip(fileName, exdir = mainDir,subDir)
Warning message: In unzip(fileName, exdir = mainDir, subDir) : error
1 in extracting from zip file
unzip(fileName)
Warning message: In unzip(fileName) : error 1 in extracting from zip
file
unzip(fileName, "stormdata.csv")
Warning message: In unzip(fileName, "stormdata.csv") : error 1 in
extracting from zip file
unzip(fileName, "stormdata.csv", list = TRUE)
Error in unzip(fileName, "stormdata.csv", list = TRUE) : zip file
'StormData.zip' cannot be opened
Any suggestions would be greatly appreciated.
I was getting the same error.
I changed the path --
from :
uzp <- "C:\\Users\\Sharvari\\Downloads\\rprog%2Fdata%2Fspecdata"
to
uzp <- "C:\\Users\\Sharvari\\Downloads\\rprog%2Fdata%2Fspecdata.zip"
and it works fine!
setwd("C:\\Users\\Sharvari\\Downloads")
uzp <- "C:\\Users\\Sharvari\\Downloads\\rprog%2Fdata%2Fspecdata.zip"
unzip(uzp, exdir = "C:\\Users\\Sharvari\\Desktop\\specdata")
I too was getting that error 1 message when trying to unzipping a zip file. Glitch in my case was the conflict between working directory and zip file path.
My case was:
My working directory was like "C:/Users/SCOTT/Desktop/Training"
While my zip file was located in "C:/Users/SCOTT/Desktop/Training/house_consumption_data"
When I was trying to execute this:
unzip("house_data.zip")
Possibly your file is in a different folder.
I have had the same problem trying to download and unzip the same file, for the same course. And I have had problems with unzip in the past and was determined to solve it this time too.
Eventually the extension of the file turned out to be csv.bz2. And than this Extract bz2 file in R post solved my problem.
After downloading the file I was able to read it directly with
stormdata <- read.csv("stormdata.zip")
without using unzip.
This error seems to appear whenever openXLS is unable to open the specified file.
It could be a wrong name, wrong directory or the file might be encrypted or password protected
change your zip file format this error will appear while the zip format problems occur, look at your zip file it should be "rar" change it to "zip". the function works only for "zip" format files.
I faced the same issue. Make sure that, you specify the correct name of the file(get it from the properties of .zip file) in the following code.
file = read.table(unzip("file_name.csv.zip"), sep = ",", header = TRUE)
In my case, Was just mentioning file_name.zip and R was throwing the error.
Also, there are two functions for unzipping files in R
1) unz - to extract single element from zip file/s
2) unzip - to extract all the present elements from the .zip file
I usually prefer unzip.
If you will use unz in the above code, R will throw error again.
I encountered the same error using install_course_zip' with a zip file. I followed all the instructions for the command faithfully but kept getting errors relating to the 'exdir'. I moved the zip file to various directories without success.
I finally used getwd() to get the working directory and then placed the zip file in that directory. I then was able to use the zip file name without having to use any folder structure and this worked. I still have no idea why R would not accept a different directory.
I had list of files to be unzipped and processed; I was facing same error
"error 1 in extracting from zip file"
used full directory and set working directory code worked
files <- list.files(path="C:\\Users\\Tejas naik\\Documents\\land", pattern=".zip$")
out_dir<- "C:\\Users\\Tejas naik\\Documents\\input"
setwd("C:\\Users\\Tejas naik\\Documents\\land")
for (i in files) {
#nzip(paste(out_dir,i), exdir=out_dir)
unzip(i,exdir=out_dir)
}
This error was happening bit differently in my case . As there was no zip file ,the issue was file was open in excel so this error was poping up .
It's crucial to give the full name (including the path) of the zip-file to the unzip function.
So instead of file.zip, it should be C:\user\name\file.zip.
In case you're using the list.files function, one should set the full.names option to TRUE.
For me the error is fixed after I add \ backslash character to the filepath.
Example:
from
unzip("abc\aaa.zip")
to
unzip("abc\\aaa.zip")

Reading an Excel file into an R dataframe from a zipped folder

I have an Excel file (.xls extension) that is inside a zipped folder that I would like to read as a dataframe into R. I loaded the gdata library and set up my working directory to the folder that houses the zipped folder.
When I type in the following syntax:
data_frame1 <- read.xls( unz("./Data/Project1.zip","schools.xls"))
I get the following error messages:
Error in path.expand(xls) : invalid 'path' argument
Error in file.exists(tfn) : invalid 'file' argument
I'm guessing that I'm missing some arguments in the syntax, but I'm not entirely sure what else needs to be included.
Thanks for your help! This R newbie really appreciates it!
Unfortunately, after a quick survey of all the xls functions I know, there is no xls reading function that can recognize the unz output (I would love to be proven wrong here). If it were a 'csv' it would work fine. As it stands, until such a function is written, you must do the loading in two steps extraction and then loading.
To give you a little more control, you can specify which file to unzip as well as the directory to place the files with unzip.
# default exdir is current directory
unzip(zipfile="./Data/Project1.zip", files = "schools.xls", exdir=".")
dataframe_1 <- read.xls("schools.xls")
Sadly, this also means that you must do cleanup afterwards if you don't want the 'xls' file hanging around.

Resources