Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User input file name in function - R

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:

  1. 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
    
  2. 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!

like image 268
Qeshet Avatar asked Dec 13 '25 17:12

Qeshet


2 Answers

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)
like image 84
Moody_Mudskipper Avatar answered Dec 15 '25 11:12

Moody_Mudskipper


Answer for both question is yes.

  1. You want to analize file, not filename. So you have to create new funcion for filename like (if for example your file is csv file):

so your average looks like

    readFile <- function(filename) {
      file <- read.csv(file = paste0(filename, ".csv")
      return(file)
    }
averageFun <- function(filename){
      summary(readFile(filename)$Concentration)
    }
  1. 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)
        }
    
like image 40
M. Siwik Avatar answered Dec 15 '25 11:12

M. Siwik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!