-1

I have a string with double quotes: For e.g string1<-"URL,username,password"

Now there is one function call: function(string1)..... function works if call function(URl,Username,password) like this.

But it does not work if I call function(string1) like this ,because string1 contains double quotes.

How can i solve this problem?

PKumar
  • 10,971
  • 6
  • 37
  • 52
Rajat
  • 107
  • 7
  • Please include reproducible example, as well as the error message you get. – Dominic Comtois May 19 '18 at 17:51
  • 1
    See this: https://stackoverflow.com/q/6034655/786542 – Tung May 19 '18 at 17:58
  • @DominicComtois it is a "startGraph" function which connects R to Neo4j(the Graph Database). It takes 3 arguments 1. URL 2.username and 3.password. Its actually a inbuilt function of library RNeo4j. So, if i pass URL, username and password separately inside the startGraph function it works but if I save URL,username and password to an object and pass that object to startGraph function , it doesnt work – Rajat May 19 '18 at 19:42

2 Answers2

1

It is not clear about what/how the function responds. Here, is an example that takes two arguments for parameters for the dataset and the column name and get the mean of that column

f1 <- function(dat, col){
     mean(dat[[col]], na.rm = TRUE)

}

df <- data.frame(col1 = 1:5)
f1(df, "col1")
#[1] 3

If we create the function with a single parameter that takes string as an argument, then split the string by the delimiter (,), get the value of the dataframe object, subset the column and apply the mean function

f2 <- function(str1){
      v1 <- strsplit(str1, ",")[[1]]
      mean(get(v1[1])[[v1[2]]], na.rm = TRUE)
  }

string1 <- "df,col1"
f2(string1)
#[1] 3
akrun
  • 874,273
  • 37
  • 540
  • 662
  • it is a "startGraph" function which connects R to Neo4j(the Graph Database). It takes 3 arguments 1. URL 2.username and 3.password. Its actually a inbuilt function of library RNeo4j. So, if i pass URL, username and password separately inside the startGraph function it works but if I save URL,username and password to an object and pass that object to startGraph function , it doesnt work – Rajat May 19 '18 at 19:40
1

I think what you're looking for is do.call:

args <- as.list(strsplit(x = "a,b,c", split = ",")[[1]])
do.call(someFunction, args)
Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61