File Manipulation: output file and prompt user for name on Windows - r

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.

Related

How can I read values from a text file with values that have an apostrophe separating the thousands?

I have a dataset with values that are separated with an apostrophe at the thousands, f.e. 3'203.12. I can read those values with read.table, but when I want to plot them, the values above 1000 are converted to NAs, because of the apostrophe. How can I prevent this, or alternatively how can I remove all apostrophes in a text file?
Open the file in a text editor (e.g. to open with Notepad on Windows: right-click on the file and then choose Open With and select Notepad) and replace all apostrophes by nothing (Ctrl-H in Notepad, then put ' under Find What and leave Replace With empty; then click on Replace All). Save this file under a different name (e.g. if the file was called dummy.csv save as dummy_mod.csv) and then use read.table to upload dummy_mod.csv.
If this does not help you then please edit your answer and provide a sample of the file you try to upload and the R code that you wrote to upload the file.
if you want to remove the apostrophes from within R:
infile <- file('name-of-original-file.csv')
outfile <- file('apostrophes-gone.csv')
readLines(infile) |>
(\(line_in) gsub("'", "", line_in))() |>
(\(line_out) writeLines(line_out, outfile))()
close(infile)
close(outfile)
Then, read in the cleaned data file with the tool of your choice. I find import of package {rio} very convenient: df <- import('apostrophes-gone.csv')

Creating the user's specific directory in R

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)

R program open file directly based on name assigned in code

I have code in which initially it ask's the user to type the data file name and assign it for variable nm and select the reference file name. I want to convert it into arguments that will directly assigned the data file to variable nm and automatically select the reference file based on the name that is entered in the argument.
i've tried this so far but it still feels manual.
library("openxlsx")
nm=readline("Enter data file name:tdd_data4.xlsx")
readline("Enter input file name: (Press Enter)")
input_file=read.xlsx(file.choose(tdd_rinput2.xlsx))
I think you're looking for something like this. But this requires the user to type in the whole file path. I assumed one in the solution below.
nm <- readline("Enter data file name: (press enter to continue)")
input_file <- read.csv(file.path("C:/Users/Desktop", nm))

Append text to an Excel file

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)

How do I specify the output file name when using pander's live report generation?

I can create a report object, but when I export() it, there is a generic file name containing a random number. I want to be able to specify the file name.
for (report in c("file1", "file2", "file3")) {
myReport <- Pandoc$new(author="Jerubaal",title="What's my file name?", format="docx")
myReport$add.paragraph(paste("This is", report))
myReport$export(open = FALSE)
}
This gives me files with names such as
pander-1c246b334f70.docx
pander-1c246b334f70.md
I have tried adding these to Pandoc$new() and also to myReport$export(), but to no avail:
output=report
file=report
filename=report
I will be looping through a lot of things, each of which needs its own report file, so I can't manually rename the files efficiently.
How do I specify the output file name?
Thanks!
Use the first or f argument based on the sources. E.g.:
myReport <- Pandoc$new(author="Jerubaal",title="What's my file name?", format="docx")
myReport$add.paragraph("This is it.")
myReport$export('this_is_the_file_name.docx', open = FALSE)
Please feel free to send a pull request or suggestion for an updated documentation.

Resources