Import Excel data into R using openxlsx: Error in file(con, "r") : invalid 'description' argument - r

I am trying to import the data in my Excel file into R using Openxlsx library:
library(openxlsx)
data <- read.xlsx("datafile.xlsx", sheet = "Sheet1")
However, I get the following error:
Error in file(con, "r") : invalid 'description' argument
In addition: Warning message:
In unzip(xlsxFile, exdir = xmlDir) : error 1 in extracting from zip file

This error is thrown because your Excel file is open.
Save and close the Excel file and try again, it will work.

There's also another possibility: the XLSX file could be password protected. If you delete the password, then this can fix the error.

I think the best way to solve this problem is to reset the pathway of your data source. Please do not include any characters without English in your pathway.
setwd("C:\\Users\\your path way (where you store datafile.xlsx)")
P.S.
Rstudio2021 seem not friendly to non-English user ☺☺☺

Related

R openxlsx error using "loadWorkbook": WARNING: simpleWarning in unzip(file, exdir = xmlDir): error 1 in extracting from zip file

Attempting to load data from an Excel spreadsheet in R using openxlsx, I get the following error/warning,
WARNING: simpleWarning in unzip(file, exdir = xmlDir): error 1 in extracting from zip file. This is not a zipped file.
Why am I getting this error?
This error is not related to zip-issues. Instead, this is probably a file-lock error. Check to be certain that the workbook in question is not currently open. If so, close it and try again.

how to import .rec files in R

I have a .rec file that I want to import into R. I have saved the .rec file to my working directory. This is what I have tried.
library(foreign)
library(RODBC)
data.test <- read.epiinfo("data_in.rec")
I get this error:
Error in if (headerlength <= 0L)
stop("file has zero or fewer variables: probably not an EpiInfo file") :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1:
In readLines(file, 1L, ok = TRUE) :
line 1 appears to contain an embedded nul
2:
In strsplit(line, " ") : input string 1 is invalid in this locale
I have looked online and in the read.epiinfo help package in R. The help package says
Some later versions of Epi Info use the Microsoft Access file format
to store data. That may be readable with the RODBC package.
I have two questions.
1. Is the error I am getting because the .rec file I have is from an Epi Info version later than 6?
2. How do I use the RODBC library to open the .rec file?
The .rec (or .REC) file turned out to be a .EDF (European Data Format) file type. It was easily opened in R using the library edfReader. The edfReader library help file is very useful for opening the file and extracting the time series data. See code below for what I used. Code was adapted from the help file.
install.packages('edfReader')
library(edfReader)
?edfReader
lib.dir <- system.file("data_in.rec",package="edfReader")
Cfile <- paste(lib.dir,'/edfPlusC.edf',sep='')
CHdr <- readEdfHeader("data_in.rec")
CSignals <- readEdfSignals(CHdr)
summary(CSignals)

How do I use strings in functions for R ? (file,rt error)

I am trying to load data into a function to use for analysis later on and there seems to be issues inserting a string (i.e. a filename) into my function. Here is what I am working with.
hist_sep<- function(dex_file,etoh_file,sep_parameter) {
dex<-read.csv("dex_file")$sep_parameter
etoh<-read.csv("etoh_file")$sep_parameter
}
The code below outputs this error
hist_sep(RT_3h_Amp_A.csv , RT_3h_Amp_EtOH_A.csv , FL1.A)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
However, if I input the filenames myself (as below) then everything works great. So everything is in the correct directory.
dex<-read.csv("RT_3h_Amp_A.csv")$FL1.A
etoh<-read.csv("RT_3h_Amp_EtOH_A.csv")$FL1.A
Any ideas how I could get around this problem?
Remove the quotes around the filenames in your function (e.g., should be dex<-read.csv(dex_file)$sep_parameter, not dex<-read.csv("dex_file")$sep_parameter). Otherwise, it's trying to download a file CALLED "dex_file".

error loading csv file for R

when i loading csv file for R, i can see the error
but i don't know why this happening
i wrote following code:
setwd("C:\\Users\\규남\\Desktop\\twitter")
library(KoNLP)
useSejongDic()
txt <- readLines(file("test.csv"))
and, this error appear
txt <- readLines(file("test.csv"))
Error in readLines(file("test.csv")) : cannot open the connection
In addition: Warning message:
In readLines(file("test.csv")) :
cannot open file 'test.csv': No such file or directory
why this happening?
file directory is not wrong, and that file in the folder
[enter image description here][1]
please see this
i restart Rstudio, even notebook power
but error appear again
how to i load that csv file?
and why this happening?
here is result useing getwd() function
[1] "C:/Users/규남/Desktop/twitter"
Warning message:
closing unused connection 3 (test.csv)
[1]: http://i.stack.imgur.com/xkFkt.png
When working through these problems I like to use the file.path() function. Look at the documentation, but it makes certain that the separator characters that are used in the string are what R is expecting.
Try:
path <- file.path("C:", "Users", "규남", "Desktop", "twitter")
setwd(path)
library(KoNLP)
useSejongDic()
txt <- readLines(file("test.csv"))

Cannot read data from an xlsx file in RStudio

I have installed the required packages - gdata and ggplot2 and I have installed perl.
library(gdata)
library(ggplot2)
# Read the data from the excel spreadsheet
df = data.frame(read.xls ("AssignmentData.xlsx", sheet = "Data", header = TRUE, perl = "C:\\Strawberry\\perl\\bin\\perl.exe"))
However when I run this I get the following error:
Error in xls2sep(xls, sheet, verbose = verbose, ..., method = method, :
Intermediate file 'C:\Users\CLAIRE~1\AppData\Local\Temp\RtmpE3UYWA\file8983d8e1efc.csv' missing!
In addition: Warning message:
running command '"C:\STRAWB~1\perl\bin\perl.exe" "C:/Users/Claire1992/Documents/R/win-library/3.1/gdata/perl/xls2csv.pl" "AssignmentData.xlsx" "C:\Users\CLAIRE~1\AppData\Local\Temp\RtmpE3UYWA\file8983d8e1efc.csv" "Data"' had status 2
Error in file.exists(tfn) : invalid 'file' argument
Thanks to #Stibu I realised I had to set my work directory. This is the command you use to run in Rstudio; setwd("C/Documents..."). The file path is where the excel file is located.
I had the issue but I solved it differently.
My problem was because my file was saved as Excel (extension .xls) but it was a txt file.
I corrected the file and I did not meet any other error with the R function.

Resources