简体   繁体   中英

How to call functions using the character string of the function name in R while using the ggplot package?

I've been trying to write an R code which reads data from an excel file and plot different graph charts (like: bar, line, point or pie) based on the user input, using the ggplot package.

For this, I am using a 3-4 different functions:

1) function for Bar plotting:

bar <- function() {
  plot <- ggplot (data= df, aes(x= Dates, y= Values))
  barPlot <- plot + geom_bar(stat="identity", fill="red", width=1)
  print(barPlot)
}

2)Similarly, for line plot :

line <- function() {
plot <- ggplot (data= df, aes(x= Dates, y= Values))
linePlot <- plot + geom_line(aes(group=1), colour="blue", size=0.5)
print(linePlot)
}

3) The main function, which declares all the libraries and reading the excel workbook as a data frame.

In the main function I am trying to call the different plot functions using "if else" as follows:

    main <- function() { 
library(XLConnect)
library(ggplot2)

wk <- loadWorkbook("D:\\....xlsx")
dataFrame = readWorksheet (wk, sheet="sheet1", header=TRUE)
df <- data.frame(Dates = dataFrame[,1],
                 Values =dataFrame[,2])

    name <- scan(what = " ")
    if (name == "bar")
    { bar() }

    else if (name == "line")
        { line() }
}

But it throws back the error: " ggplot2 doesn't know how to deal with data of class function".

Simplified Version of the Data:

Dates Values
Jan   46
Feb   54
Mar   32

How can I modify my code to accommodate this requirement of being able to plot different graphs as per user input?

Your problem is related to variable scope : a dataframe defined within main() is only available within main() . It won't be visible to other functions such as bar() or line() , even if they are called from within main() .

The preferred solution is to pass all the data that a function needs explicitly. So, define your functions as eg:

bar <- function(df) {
  plot <- ggplot (data= df, aes(x= Dates, y= Values))
  barPlot <- plot + geom_bar(stat="identity", fill="red", width=1)
  print(barPlot)
}

and call it from main() like so:

main <- function() {
  library(ggplot2)
  df <- data.frame(Dates = c("Jan", "Feb", "Mar"),
                   Values =c(46, 54, 52))
  name <- scan(what = " ")
  if (name == "bar")
  { bar(df) }
}

There are other ways to deal with this problem, which I'll mention for completeness, though they aren't as nice as the one above. First, you could nest your function definitions, eg:

main <- function(){
    bar <- function(){
        ...
    }
    ...
    bar()
}

This works because functions defined inside of other functions have access to all variables defined within that function.

Finally, all functions have access to global varaibles, so you can either define your variables outside of main() or define them as globals using the <<- operator.

It seems ggplot2 package cannot handle data of type functions. Instead of calling the function name in main() , you can pass the entire function body.

main <- function() {

...
...

name <- scan(what = " ")

    if (name == "bar")
    {  plot <- ggplot (data= df, aes(x= Dates, y= Values))
  barPlot <- plot + geom_bar(stat="identity", fill="red", width=1)
  print(barPlot)
    }
}

This displays the graph when main() is called on giving bar as input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM