Importing R Database Files - r

Trying to figure out why I can't use read.csv to call a data file into R. I have the latest version of R. Do I need to reset my working directory?
carSpeeds <- read.csv(file = 'data/car-speeds.csv')

Related

How do I download and save an inbuilt library from R in my desktop as a .Rdata file?

everyone! I am kinda new to R and was trying to download the Women data from the inbuilt Data set library in R. I understand that I have to use the save function to do so, but i am uncertain how.
Running:
write.csv(x = data("women"), file = "women.csv")
will save the women.csv in your working directory. You can check location by running getwd() or specify path using the file argument.

importing excel files in r

i am trying to load an excel file in r studio but each time i run it
Error in read_excel("R/win-library/3.6/IMDB_data.xlsx", sheet = "IMDB_data",
:
could not find function "read_excel" this is displayed.
i have tried changing directory
saving the data to load, in the same as working directory
none of the articles resolve my issues concerned yet
tried changing directory
saving the file in the same place as my working directory
importing through choose directory
setwd("~/R/win-library/3.6")
library(readxl)
IMDB_data <- read_excel("R/win-library/3.6/IMDB_data.xlsx",
sheet = "IMDB_data", skip = 2)
Write R code using data “IMDB_data” to
Load CSV in R by skipping second row.
enter image description here
It seems like your readxl library is not loaded.
Do you get any errors when you run library(readxl)?
Your working folder shouldn't matter and you should probably avoid working in the R's library.
The read_excel command should read the file based on the path provided, but your error is not complaining about the missing file. It's complaining about the missing function.
Lastly, if you set the working directory to ~/R/win-library/3.6, then it would be enough to run the following code (provided your readxl library loaded correctly):
IMDB_data <- read_excel("IMDB_data.xlsx", sheet = "IMDB_data", skip = 2)

Reading in xls and xlsx files in R

I am working with loads of xls and xlsx files at the same time with no easy way to convert them to the same file type.
I am facing issue reading them in because read.xlsx() from "xlsx" package works just fine with xls files but I am getting the Java Out of Memory error when trying to read in xlsx files. I tried to use the following line to extend memories with no success:
options(java.parameters = "-Xmx1000m")
As an alternative option I have tried read.xlsx() from "openxlsx" package but it does not read xls files and the aforementioned two packages are not compatible when loaded at the same time. I faced the same difficulty with the "XLConnect" package where again I face java errors when trying to use "xlsx" and "XLConnect" packages loaded at the same time.
I would be interested what people do to solve situations like this?
You can consider the read_excel function in the readxl package:
read_excel(path, sheet = 1, col_names = TRUE, col_types = NULL, na = "", skip = 0)
You can even specify which sheet in the xlsx file you want to import in, whether the first row consists of column names, as well as the missing value used in the excel files.

Unable to write data in Macro enabled file in R using openxlsx package

When I trying to write data in a Macro enabled file, that excel files gets crashed. Is there any way in R to write data in Macro enabled data keeping the existing data intact. Below is my code:
wb <-loadWorkbook("C:/Test/Excel.xlsm")
writeData(wb,df,sheet="sheet1",startRow=2)
saveWorkbook(wb,"C:/Test/Excel.xlsm", overwrite = TRUE)

Remove a file that has been opened within R studio with read.xlsx

I am doing a loop on different file and need to read a file extract the information and then delete it.
In R I use the package openxlsx
datatemp = openxlsx::read.xlsx(xlsxFile = "./source/aFile.xlsx")
However I can't remove the file from windows, it tells me it is open in R studio, I tried to point read.xlsx to another file, and to closeAllConnections but it did not work.
Is there a way to disconnect R from the file without having to close R studio ?
Thanks
Romain
I am on Windows 7 and I used unlink which let me delete the file
datatemp = openxlsx::read.xlsx(xlsxFile = "temp.xlsx")
unlink(datatemp)
Then I was able to manually delete the file without a problem

Resources