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))
Related
I am trying to write an R script (to run from Rstudio by my students) that can accept a file of the phyloseq object type (is several tables connected to each other stored into one object) so I can use that object to run the code on. Since it seems like I cannot accept any phyloseq file directly, I decided to strip the file into 3 tables (of which are stored in the input file) like so
input <- as.data.frame(readline(prompt = "Enter phyloseq object name "))
input.taxtab <- as.data.frame(tax_table(input)) #tax_table is a ps-function that takes out the taxonomy table
input.otutab <- as.data.frame(input#otu_tab)
input.samdat <- as.data.frame(input#sam_data)
however, the obvious issues I see with my code are that the input file cannot store all the information, but it also (at the moment only seems to accept characters, i.e. it only takes the filename literally, not the file itself).
I have tried to get the data frames separately (by having 3 times the prompt input) but that doesn't coerce the file itself either. Here is a snippet of that:
input.taxtab <- as.data.frame(tax_table(readline(prompt = "Enter phyloseq object name ")))
Hope that someone can help me evolve my code to accept an actual file instead of its name
is there a way to load a csv file in R and define the variable automatically from the filename? So, if you have a csv file called 'hello', can I load it in R and create the df/var. without defining it?
So, rather than define hello in the load procedure: hello=read("filepath/hello"); instead we have read("filepath/hello") but include a command to create and name a variable that is the same name of the file name (hello in this example?)
Depending on why you would like to do this I would offer you another solution:
I suppose your problem is that you have a big folder with a lot of csv files and you would like to load them all and give the variables the name of the csv file without typing everything manually.
Then you can run
> setwd("C:/Users/Testuser/testfiles")
> file_names <- list.files()
> file_names
[1] "rest" "test1.txt" "test2.csv" "test3.csv"
where as path you use the path where all your csv files are stored.
Then if there are stored any other files and you only would like to have the csv files we have to grep them with regex
> file_names_csv <- file_names[grepl(".csv",file_names)]
> file_names_csv
[1] "test2.csv" "test3.csv"
Now we load them with a for loop and assign them to a variable that is named as the corresponding csv file
for( name in file_names_csv){
assign(paste(name, sep=""), read.csv(file = paste(name, sep="")))
}
And we have
> test2.csv
test
1 1234
> test3.csv
test
1 2323
You can also gsub the .csv away before you load the data by
> file_names_csv <- gsub(".csv","",file_names_csv )
> file_names_csv
[1] "test2" "test3"
So basically you have exactly what you have asked for without using global variables.
I have advised you not to do this in any real world scenario, but if it helps understanding the concepts, this is not a complete solution but the important ingredients.
<<- the superassignement operator in the enclosing environment, which in the following case is the global namespace:
rm(hello) # just in case, ignore warning if there is any
dont <- function(){
hello <<- 42
}
print(hello)
dont()
print(hello)
So you can define values in the enclosing environment within a function without a return value.
The name of that variable does not have to be fixed (as hello in the example above) but can depend on an argument to that function as in
dontdothis <- function(name){
eval(parse(text = paste0(name, " <<- 42")))
}
dontdothis("frederik")
print(frederik * 2)
You will need to add the file operations and some small detail but that is how one could do it. You may want to google for namespaces and environments and assignment operators in R to get a better understanding of the details in there.
Worthwhile short read to distinguish between global environment and enclosing environment: Why is using `<<-` frowned upon and how can I avoid it?
Sorry, everyone. First time using R. The company switched to it recently and I am trying to customize a script I was given.
The purpose of the script is to:
Open a CSV file
Filter the results by a code
Save the results as a new CSV file named as the code
Because of this, I have to provide the code three times and the location path twice. I am trying to streamline this so I only need to enter the code and path once, by assigning them to variables, and then the script would use those variables for everything else.
Here's what I have so far, but I'm getting an error "Error in paste(FINAL) : object 'FINAL' not found"
CODE <- '1234'
LOC <- 'C:/Users/myname/Documents/Raw Files/'
FINAL <- paste0(LOC,CODE,'.csv')
RawFile <- read_csv(paste0(LOC,'Raw File MERGED_Raw.csv'))
CODEofInterest <- RawFile %>% filter(ID == CODE)
write_csv(CODEofInterest,paste0(FINAL))
It was user error, I was not running the entire script, just the last line.
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.
guys, thanks for read this. This is my first time writing a program so pardon me if I make stupid questions.
I have bunch of .csv files named like: 001-XXX.csv;002-XXX.csv...150-XXX.csv. Here XXX is a very long name tag. So it's a little annoying that every time I need to type read.csv("001-xxx.csv"). I want to make a function called "newread" that only ask me for the first three digits, the real id number, to read the .csv files. I thought "newread" should be like this:
newread <- function(id){
as.character(id)
a <- paste(id,"-XXX.csv",sep="")
read.csv(a)
}
BUt R shows Error: unexpected '}' in "}" What's going wrong? It looks logical.
I am running Rstudio on Windows 8.
as.character(id) will not change id into a character string. Change it to:
id = as.character(id)
Edit: According to comments, you should call newread() with a character paramter, and there is no difference between newread(001) and newread(1).
This is not specifically an answer to your question (others have covered that), but rather some advice that may be helpful for accomplishing your task in a different way.
First, some of the GUI's for R have file name completion. You can type the first part: read.csv("001- and then hit a key or combination of keys (In the windows GUI you press TAB) and the rest of the filename will be filled in for you (as long as it is unique).
You can use the file.choose or choose.files functions to open a dialog box to choose your file using the mouse: read.csv(file.choose()).
If you want to read in all the above files then you can do this in one step using lapply and either sprintf or list.files (or others):
mycsvlist <- lapply( 1:150, function(x) read.csv( sprintf("%03d-XXX.csv", x) ) )
or
mvcsvlist <- lapply( list.files(pattern="\\.csv$"), read.csv )
You could also use list.files to get a list of all the files matching a pattern and then pass one of the returned values to read.csv:
tmp <- list.files(pattern="001.*csv$")
read.csv(tmp[1])