I have an excel file that I alread wrote code to. I want to add another line of code. Suppose, I want to add "Hello" under the data already there. I tried:
write.xlsx("Hello", "text.xlsx", sheetName = "Sheet1", append = TRUE)
And got the error:
Error in .jcall(wb, "Lorg/apache/poi/ss/usermodel/Sheet;", "createSheet", :
java.lang.IllegalArgumentException: The workbook already contains a sheet of this name
I was wondering how I could solve this problem.
Depending on how complex you want to get here is a simple way to load your text into excel
path = r'ENTER FILE PATH AND DESIRED FOLDER NAME HERE FOLLOWED BY .csv' #plug in the
folder path you want to save this as ending it with the filename.csv of your choice
with open(path, 'w', newline='', encoding='utf-8') as resultfile:
wr = csv.writer(resultfile, dialect='excel')
wr.writerow([x[0] for x in cursor.description])
for record in records:
wr.writerow(record)
Related
I want to read the CSV file "mydata.csv" as an input and create the output in the same directory using R. I have hard-coded for getting csv input(Domain_test.csv) and the output(MyData.csv) path as below. But I will have to share the same Rscript and the corresponding csv files with one of the users so that he/she can execute it and take the results. I want the user should to select his specific path where ever he wants and make it run without hard coding the input/output path in the script.
How it should be done in R?
#reading csv from this current directory
data <- read.csv("C:/Users/Desktop/input_output_directory/Domain_test.csv")
#generating the output In this same directory
write.csv(dataframe,"C:/Users/Desktop/input_output_directory/MyData.csv", row.names = FALSE)
You can use
wd <- choose.dir(default = "", caption = "Select folder")
setwd(wd)
Im reading in a template file, delete rows and then saving the file again but when I try to open up the file excel says it is corrupted.
Here is my code:
template <- loadWorkbook('foo.xlsx')
deleteData(template,sheet=1,cols=1:10000, rows = 6:10005 gridExpand = FALSE)
saveworkbook(template,'foo-test.xlsx',overwrite = TRUE
I have even tried it without the deleteData line and it still corupt the file.
Anyone have ideas son how I can change my code/ not corrupt the file.
I am new to coding and very new to this forum, so I hope my request makes sense.
I am trying to select images listed in a .csv file and to copy them to a new folder. The pictures and the .csv file are both in the folder GRA04. The .csv file contain only one column with the picture names.
I used the following code:
#set working directory
setwd("E:/2019/GRA04")
#create and identify a new folder in R
targetdir <- dir.create("GRA04_age")<br/>
#find the files you want to copy
filestocopy <- read.csv("age.csv", header=FALSE) #read csv as data table (only one column, each raw being a file name)
filestocopy_v <- readLines(filestocopy)#convert data table in character vector
filestocopy_v #shows the character vector
#copy the files to the new folder
file.copy(filestocopy_v, targetdir, recursive = TRUE)
When reaching the line
filestocopy_v <- readLines(filestocopy)
I get this error message:
Error in readLines(filestocopy) : 'con' is not a connection
I looked online for solutions with no luck. I ran this code before (or else something similar... didn't back it up...) and it worked fine, so I am not sure what is happening...
Thanks!
Out of interest, would the following now do what you're trying to achieve?
filestocopy_v <- filestocopy[[1]]
I'm cringing just asking this here... and likely searching by the wrong terms, so apologies if this is a redundant question. How can I have a .R file create an output file and prompt the user for a file name, using an interactive dialog box? Or more simply put a "Save As" prompt?
Basically the reverse of:
str <- "Microsoft Excel Comma Separated Values File (.csv)"
data <- read_csv(choose.files(multi = FALSE, filters = cbind(str, "*.csv")))
I want to use write_csv() and have the user to decide the name and directory.
Solution comes from: How to let user choose output file name in writecsv
str <- "Microsoft Excel Comma Separated Values File (.csv)"
write_csv(data, path = choose.files(caption = "Save As...",
multi = FALSE,
filters = cbind(str, "*.csv")))
This produces the desired output. It prompts the user and filters files by only .csv extension. If a file doesn't exist it will create it as.csv by default.
How do I read excel files just based on the first part of the file name? For example my file is "File_01_01_2019", where "File" is always the same but the date changes often, so I would want to read excel files that start with "File" in this scenario.
This should help you
library(readxl)
sapply(list.files(path = "your_path",
# regex that defines to start with "File" and ends with ".xlsx"
pattern = "^File.*\\.xlsx$",
full.names = TRUE),
read_excel)