Unable to resolve Function argument in quotes

I am writing simple function. Function argument (ds) not resolving with value as dm. What changes expected to run function smoothly.

path <- function(ds){

a1 <- read_sas("~/c/mydoc/myfile/new/data/ds.sas7bdat")

}

path(ds = "dm")

What error are you seeing?

Also, I am suspecting your Reprex might not capture the whole problem? If it does, than I would either:

  • Not write this as a function at all... If you just need a function to call read_sas, then why not just read_sas?
  • Or, if you do need this in a function, I don't see your argument doing any work in the function body; so you could just write your function as:
path <- function() { return(read_sas(<your_path_here>)) }

Thank you for your response!

Due to regulated working environment, I can't share entire scenarios over here. Sorry for that.

Just to explain more about requirement. I want to read multiple datasets on each function call. As I mentioned below,

path <- function(dataset){

a1 <- read_sas("~/c/mydoc/myfile/new/data/dataset.sas7bdat")

}
path(dataset = "dm")
path(dataset = "ex")
path(dataset = "cm")
path(dataset = "ec") and many more

In current function, function argument doesn't resolves at the time of execution hence I am getting an error saying "path doesn't exist" because variable is not getting resolved.

the string "~/c/mydoc/myfile/new/data/dataset.sas7bdat" is a concrete sequence of text characters that R wont attempt to interpret in other ways. If you want to inject some other text inside it somewhere you can take the following approaches:



(nt <- "newtext")

#solution - base
#  the larger phrase needs that larger space broken up, and for the 
# part to inject needs to be unquoted

paste0("start of a phrase - ",nt , " - end of")

#solution - glue, 
# glue lets you mark a part of the text to be interpreted as a variable, so that you
# can use a single quoted string; curly braces {} are used by glue for this purpose
library(glue)
glue("start of a phrase - {nt}  - end of")

Thank you for your response! Function Argument has resolved now within Quotes :slight_smile:

One query, If I want to save object with same function argument then I am getting error :frowning_face:

See below example -

library(haven)
path <- function(dataset){
dataset <- read_sas(glue("start of a phrase - {dataset} - dataset.sas7bdat"))
}
Function call - path(dataset = "dm")
path(dataset = "ex")

Since function call has double inverted commas then while creating object with read_sas function, I am getting an error :frowning:

Any workaround to fix this?

it seems to me you are asking to understand R's scoping rules, which are simply that when you enter a function, you are in a functional scope and things stay there and don't leak out to the global scope. you have an opportunity to return the last value (which could be a list and therefore contain mulitple contents), but assignment to a global object is best done at the level where the function was called.
Consider these simple examples.


x_ <- 1

simplefunc_1 <- function(x){
  cat("got",x,"\n")
  x_ <- x # assigns to a local x_ so has no effect on global x_ ; but it is at least auto returned 
}

simplefunc_1(2) 
x_

x_ <- simplefunc_1(2)
x_ 


simplefunc_2 <- function(x){
  cat("got",x,"\n")
  x  # no need for internal assignment since it useless anyway, just return x
}

#therefore I can assign what is returned to x_ here where I care about it
x_ <- simplefunc_2(3)
x_

Sorry for the confusion. I am not talking here on scoping rules.

Since function call has double inverted commas; for example mentioned below "dm"
same "dm" will be used to make object name return with inverted comma as "dm" then R doesn't output with dm since it has inverted commas.

I have bold the code below -

library(haven)

path <- function(dataset){
dataset <- read_sas(glue("start of a phrase - {dataset} - dataset.sas7bdat"))
}

Function call - path(dataset = "dm")

Hope I am making sense over here :frowning:

oh, the confusion might be caused by not formatting your code as code, but leaving it up to the forum defaults to display. Consider if the code in your script in R studio looks the same as it does on the forum.

In the future please put code that is inline (such as a function name, like mutate or filter) inside of backticks (mutate) and chunks of code (including error messages and code copied from the console) can be put between sets of three backticks:

```
example <- foo %>%
  filter(a == 1)
```

This process can be done automatically by highlighting your code, either inline or in a chunk, and clicking the </> button on the toolbar of the reply window!

This will help keep our community tidy and help you get the help you are looking for!

For more information, please take a look at the community's FAQ on formating code

I would recommend looking into the assign() function. You can use this to create named objects in the global environment from inside a function.

Warning: this is not best practices for the R community, but it gets the job done. With great power comes great responsibility.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.