Search code examples
rreadxl

specify input in function


How to specify the input to be only numeric class in a function in R?. For example,

read_data <- function(file_name, sheet, sample_list = stop("'sample_list' must be provided")){
    ibrary(readxl)
    data = read_excel(file_name, sheet = 2)
    meantritc = sapply(samplelist, function(x)data[grep(x, pattern = data$Source),]$Mean)
    names(data) = sample_list
    return(data)
    }

How do if sheetname is mentioned instead as read.xlsx() requires, it should be able to print a message to provide sheet number intread?


Solution

  • You can accomplish this by placing the following within your fucntion:

    if (is.character(sheet))
    {
       stop("Sheet must be a number!")
    }