I want to analyze 75 data files separately, but all in the same way. I wrote functions for every step of the analysis. The data sets look basically like this:
df = data.frame(
Time = c(1, 2, 3, 4),
Concentration = c(2983, 9848, 2894, 8384))
I ask the user for the file name and I would like to enter the chosen file name in the functions.
enterFileName <- function(){
filename <- readline(prompt="Enter file name: ")
return(filename)}
I have two questions about this:
Is it possible to write the code so that the file name entered by the user will automatically be used in the function when I run it (using the variable "filename" that I created before)? So I will not have to repeat typing the file name every time. I tried this, but it doesn't work:
averageFun <- function(){
summary(filename$Concentration)}
enterFileName()
averageFun()
Error in summary(filename$Concentration) : object 'filename' not found
Can I use the file name entered by the user for the main title in a ggplot graph? Something like this...
plotFirst <- function(df){
ggplot(data = df, aes(x = Time, y = Number)) + geom_line() +
ggtitle("UFP concentrations raw data" + filename)
}
This just returns a graph without main title.
Can somebody help me with this? Thanks in advance!
enterFileName() doesn't have side effects, you're not creating in your workspace any variable named filename.
It seems like you're refering to a variable, calling it a filename, it's confusing, but you should find your way from here:
solution 1, using a variable
df = data.frame(
Time = c(1, 2, 3, 4),
Concentration = c(2983, 9848, 2894, 8384))
enterFileName <- function(){
filename <- readline(prompt="Enter file name: ")
return(filename)}
averageFun <- function(filename){
summary(get(filename)$Concentration)}
filename <- enterFileName() # we enter 'df' (without quotes)
averageFun(filename)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 2894 2961 5684 6027 8750 9848
plotFirst <- function(df, filename){
library(ggplot2)
ggplot(data = df, aes(x = Time, y = Concentration)) + geom_line() +
ggtitle(paste("UFP concentrations raw data" ,filename))
}
plotFirst(df,filename)
solution 2, using options
enterFileName <- function(){
options(myproject.filename = readline(prompt="Enter file name: "))
}
averageFun <- function(){
summary(get(getOption("myproject.filename"))$Concentration)}
filename <- enterFileName() # we enter 'df' (without quotes)
averageFun()
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 2894 2961 5684 6027 8750 9848
plotFirst <- function(df){
library(ggplot2)
ggplot(data = df, aes(x = Time, y = Concentration)) + geom_line() +
ggtitle(paste("UFP concentrations raw data" ,getOption("myproject.filename")))
}
plotFirst(df,filename)
Answer for both question is yes.
so your average looks like
readFile <- function(filename) {
file <- read.csv(file = paste0(filename, ".csv")
return(file)
}
averageFun <- function(filename){
summary(readFile(filename)$Concentration)
}
You have to add filename argument to your funcion
plotFirst <- function(df, filename){
ggplot(data = df, aes(x = Time, y = Number)) + geom_line() +
ggtitle("UFP concentrations raw data" + filename)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With