I'm working on an R script that is supposed to open an excel file from a folder in the current user computer using read_excel from readxl library.
The path will have a personal folder (C:/Users/Username....).
I'm trying to accomplish that as follows:
string <- getwd()
name <- strsplit(strsplit(x = string, split = "C:/Users/")[[1]][2], split = "/")[[1]][1]
path_crivo <- paste0("C:/Users/", name, "/some_folders/excel_file.xlsx")
So path_crivo stores the string: C:/Users/João Anselmo/some_folders/excel_file.xlsx"
When I run the read_excel function with this path I get the error:
read_excel(path_crivo)
"Error in read_fun(path = path, sheet_i = sheet, limits = limits, shim = shim, :
Evaluation error: zip file 'C:/Users/João Anselmo/some_folders/excel_file.xlsx' cannot be opened."
If I set path_crivo directly as follows:
path_crivo <- "C:/Users/João Anselmo/some_folders/excel_file.xlsx"
It works perfectly.
Anyone have faced a similar problem?
I can't rename the folders, nor set path_crivo directly, it is supposed to be a personal path.
Thanks for your help
Try
Encoding(path_crivo)<-"latin1"
(or, possibly, change the encoding of string before you create path_crivo)
Related
Received this error message from R when using openxlsx package, not sure where to look.
Error in zip_internal(zipfile, files, recurse, compression_level, append = FALSE, :
Some files do not exist
Does anyone have any suggestions? thanks.
The code is simple:
library(openxlsx)
df1 <- cars
write.xlsx(df1, file = 'cars.xlsx')
The error resolves itself if you add a TMP environment variable. As I suspected this is because the saveWorkbook function uses the function tempdir which creates a temporary directory and uses the TMP variable (among a list of other options) to do this (see documentation for more info).
The relevant lines of code are on line 223 here. Note that tempdir is not called directly, but through the function tempfile which uses tempdir() as default value for its tmpdir argument.
Related Github issue
the file which is downloaded by writeBin() is saved at the current location. but I want to save it at another location either in sub-directories in the current location or somewhere else.
writeBin(downlaod$response$content, "Inventory.csv") This is line of code. suppose i want to save the inventory.csv at location "current_directory/download_folder".
or here, I am trying to download CSV file using the following script:
url <- "https://lgloz050.lss.emc.com:58443/APG/"
dn_url <- "https://lgloz050.lss.emc.com:58443/APG/lookup/Report%20Library/Amazon%20S3/Inventory/Accounts/report.csv"
session <- html_session(url)
form <- html_form(session)[[1]]
fl_fm <- set_values(form,
j_username = "***",
j_password = "***")
main_page <- submit_form(session, fl_fm)
downlaod <- jump_to(main_page,cfig$dn_url)
writeBin(downlaod$response$content, "Inventory.csv" )
Can I use writeBin() if not then, Is there any alternative method of writeBin() or some another way to download the CSV file from https site which requires a login?
Thanks in advance for suggestions!!!
You can access the help file of a function using ?writeBin or help(writeBin). In here you see that the argument you are looking for is con:
writeBin(object, con, size = NA_integer_,
endian = .Platform$endian, useBytes = FALSE)
con A connection object or a character string naming a file or a raw vector.
Now you can just supply any location to the con argument, pretty much like you already did:
writeBin(object = downlaod$response$content,
con = "./download_folder/Inventory.csv" )
The only thing you have to keep in mind is that R is expecting absolute paths here, which means your path will look like this:
/home/user/current_directory/download_folder/Inventory.csv
On a Linux machine or like this:
C:/user/Documents/current_directory/download_folder/Inventory.csv
On Windows.
You can also use paths relative to your current working directory (and I assumed you did above) by substituting current_directory/ with ./:
./download_folder/Inventory.csv
Or even go one folder up from your directory:
../current_directory/download_folder/Inventory.csv
Or two:
../../current_directory/download_folder/Inventory.csv
I have a simple syntax question: Is there a way to specify the path in which to write a csv file within the .csv function itself?
I always do the following:
setwd("C:/Users/user/Desktop")
write.csv(dt, "my_file.csv", row.names = F)
However, I would like to skip the setwd() line and include it directly in the write.csv() function. I can't find a path setting in the write.csv documentation file. Is it possible to do this exclusively in write.csv without using write.table() or having to download any packages?
I am writing around 300 .csv files in a script that runs auomatically everyday. The loop runs slower when using write.table() than when using write.csv(). The whole reason I want to include the path in the write.csv() function is to see if I can decrease the time it takes to execute any further.
I typically set my "out" path in the beginning and then just use paste() to create the full filename to save to.
path_out = 'C:\\Users\\user\\Desktop\\'
fileName = paste(path_out, 'my_file.csv',sep = '')
write.csv(dt,fileName)
or all within write.csv()
path_out = 'C:\\Users\\user\\Desktop\\'
write.csv(dt,paste(path_out,'my_file.csv',sep = ''))
There is a specialized function for this: file.path:
path <- "C:/Users/user/Desktop"
write.csv(dt, file.path(path, "my_file.csv"), row.names=FALSE)
Quoting from ?file.path, its purpose is:
Construct the path to a file from components in a platform-independent way.
Some of the few things it does automatically (and paste doesn't):
Using a platform-specific path separator
Adding the path separator between path and filename (if it's not already there)
Another way might be to build a wrapper function around the write.csv function and pass the arguments of the write.csv function in your wrapper function.
write_csv_path <- function(dt,filename,sep,path){
write.csv(dt,paste0(path,filename,sep = sep))
}
Example
write_csv_path(dt = mtcars,filename = "file.csv",sep = "",path = ".\\")
In my case this works fine,
create a folder -> mmult.datas
copy its directory-> C:/Users/seyma/TP/tp.R/tp.R5 - Copy
give a name of your .csv -> df.Bench.csv
do not forget the write your data.frame -> df
write.csv(df, file ="C:/Users/seyma/TP/tp.R/tp.R5 - Copy/mmult.datas/df.Bench.csv")
for more you can check the link
This seems like a very basic issue. The file path is valid and I can open the file using other means in R, but I am looking to use tm library.
docs <- Corpus(DirSource("C:/Users/xyz/Work/test.corpus.txt"), encoding = "UTF-8"))
Throws an error of:
Error in inherits(x, "Source") : empty directory
EDIT:
This works with the original method:
docs <- Corpus(DirSource("C:/Users/xyz/Work/"), encoding = "UTF-8"))
Apparently you cannot specify an individual file name. The solution is to to read the file via another method and then use another source type such as VectorSource.
You can specify a pattern so that DirSource only picks the files with that pattern. pattern = ".txt" for all txt files. Or if you want, pattern = "test.corpus.txt". Something like below.
docs <- Corpus(DirSource("C:/Users/xyz/Work/", pattern = "test.corpus.txt", encoding = "UTF-8")
I have trying many methogs including using "file.paths()" functions etc. but am unable to. It always says that the file "CHCC" for example is not found (even though my file's complete name is CHCC.xlsx)
importData <- function(stockName){
path <- paste("~/Individual Technical Indicator's Results/", stockName, ".xlsx", sep = "")
dataFrame <- read_excel(path)
}
Use shQuote to properly delimit the path/file name.