How to rename list objects in self defined function?

how to rename function name in sql
sql user-defined functions tutorial
sql server 2016 user defined function
error handling in sql server user defined functions
how to use function in sql
how to use function in sql select statement
sql server rename table
sql server rename index
quote <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  return(list(namefoo=foo,namebar=bar))
}

From the above function, If I ran quote(test, test1) the name of the two objects in the list remain as namefoo and namebar instead of what I specified in the function call.

If I just ran the code seperately as:

set.seed(3)
foo <- rnorm(n = 5)
bar <- rnorm(n = 5)
obj <- list(test=foo,test1=bar)

Then obj will return foo and bar with the amended names. How do I make my function do this? I've tried several combinations of including quotes as well, from the function call to the function itself but it doesn't seem to work.

One way is this:

quote <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  out <- list(foo, bar)
  names(out) <- c(namefoo, namebar)
  out
}

You can save the list to a variable and then name the elements with names.

# quote('foo', 'bar')
# $namefoo
# [1] -0.9619334 -0.2925257  0.2587882 -1.1521319
# [5]  0.1957828
# 
# $namebar
# [1]  0.03012394  0.08541773  1.11661021
# [4] -1.21885742  1.26736872

Rename User-defined Functions, Renaming a user-defined function will not change the name of the corresponding object name in the definition column of the sys.sql_modules  To rename user-defined functions. In Object Explorer, click the plus sign next to the database that contains the function you wish to rename and then. Click the plus sign next to the Programmability folder. Click the plus sign next to the folder that contains the function you wish to rename: Table-valued Function.

It's a very bad idea to name your function quote, a very important R function is named just like that.

Use setNames :

fun <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  setNames(list(foo,bar),c(namefoo, namebar))
}

fun("hi","there")
# $hi
# [1] -0.9619334 -0.2925257  0.2587882 -1.1521319  0.1957828
# 
# $there
# [1]  0.03012394  0.08541773  1.11661021 -1.21885742  1.26736872

You might also see this kind of code around too, using more advanced features of rlang / tidyverse :

library(tidyverse)
fun2 <- function(namefoo, namebar){
  set.seed(3)
  foo <- rnorm(n = 5)
  bar <- rnorm(n = 5)
  lst(!!namefoo := foo,!!namebar := bar)
}

fun2("hi","there")
# $hi
# [1] -0.9619334 -0.2925257  0.2587882 -1.1521319  0.1957828
# 
# $there
# [1]  0.03012394  0.08541773  1.11661021 -1.21885742  1.26736872

Renaming Objects/Folders, Select the object and right-click to select Rename. The Rename Object dialog is displayed, which is different depending on whether the object you want to rename is used in other objects or not: If the object is not used anywhere else, you simply change its name and click Rename. In order to rename, Please navigate to the function that you want to modify (AverageSale), and right click on the function name will open the context menu. Here, select the Rename option as shown below Once you click on the Rename option, SSMS allows us to rename as per our requirements.

We can do

quotefn <- function(...) {
        nm <- c(...)
        out <- replicate(length(nm), rnorm(n = 5), simplify = FALSE)
        names(out) <- nm
        out}
quotefn("foo", "bar")
#$foo
#[1] -0.5784837 -0.9423007 -0.2037282 -1.6664748 -0.4844551

#$bar
#[1] -0.74107266  1.16061578  1.01206712 -0.07207847 -1.13678230

Rename Object, Tab, and Field Labels, Tip After renaming a tab or object, rename any custom reports, dashboards, profiles, permission sets, custom fields, and list views that contain the original name. > > #For example: > x = c(1:40) > # I wish to use a function to rename x, already created, to y, perhaps by > obj.rename(x,y) > # or > obj.rename("x","y") > > There are numerous examples of renaming > variables (columns) in dataframes, but I have > found no methods for renaming objects.

dplyr rename(), In this post, we will learn to use dplyr rename function to rename What I mean is , if my dataframe has col1, col2, col3 and col4, and I am Arm yourself with the most practical data science knowledge available today. To rename an object: On the SQL Refactor menu click Smart Rename, or in the Object Explorer pane, right-click the object and click Smart Rename. The Smart Rename dialog box is displayed. If you want to change the object owner, select the name of the new owner. If you want to change the object name, type the new name.

rename function, replace. named vector specifying new names. Details. The rename function provide an easy way to rename the columns of a data.frame or the items in a list. Rename an object Details. The rename function provide an easy way to rename the columns of a data.frame or the items in a list.

Smart Rename - Retired products, The Smart Rename refactoring generates a script to rename objects in your tables; views; stored procedures; user-defined functions Referencing Objects lists the objects that will be modified by the script because they  Excel allows you to create custom functions using VBA, called "User Defined Functions" (UDFs) that can be used the same way you would use SUM() or other built-in Excel functions. They can be especially useful for advanced mathematics or special text manipulation or date calculations prior to 1900 .