【问题标题】:AzureRMR object not found when set_tag() inside a function函数内 set_tag() 时未找到 AzureRMR 对象
【发布时间】:2021-02-04 18:12:32
【问题描述】:

运行set_my_tag(resources_to_tag) 出现错误

eval(expr, p) 中的错误:找不到对象“资源”

library(tidyverse)
library(AzureRMR)
#> Warning: package 'AzureRMR' was built under R version 3.6.3
#> 
#> Attaching package: 'AzureRMR'
#> The following object is masked from 'package:purrr':
#> 
#>     is_empty

set_my_tag <- function(resources_to_tag) {
  
  for (i in 1:nrow(resources_to_tag)) {
    resource <- resources_to_tag[i, ]
    
    az$
      get_subscription(resource$subscriptionid)$
      get_resource_group(resource$resourcegroup)$
      get_resource(type = resource$type, name = resource$name)$
      set_tags(MYTAG = resource$new_tag)
  }
  
}
resources_to_tag <-
  tribble(
    ~name, ~resourcegroup, ~subscriptionid, ~type, ~new_tag,
    "resource name", "resource group", "subscription id", "resource type", "new tag"
  )

reprex package (v0.3.0) 于 2021-02-04 创建

虽然我执行以下操作(不将我的代码放入函数中),但一切正常。

    resource <- resources_to_tag[1, ]

    az$
      get_subscription(resource$subscriptionid)$
      get_resource_group(resource$resourcegroup)$
      get_resource(type = resource$type, name = resource$name)$
      set_tags(MYTAG = resource$new_tag)

此外,这是最奇怪的部分,如果我从 set_my_tag() 函数中删除 set_tag() 它可以正常工作:

set_my_tag <- function(resources_to_tag) {
  
  for (i in 1:nrow(resources_to_tag)) {
    resource <- resources_to_tag[i, ]
    
    az$
      get_subscription(resource$subscriptionid)$
      get_resource_group(resource$resourcegroup)$
      get_resource(type = resource$type, name = resource$name)
  }
}

set_my_tag(resources_to_tag)

你知道出了什么问题吗?

【问题讨论】:

    标签: r azure


    【解决方案1】:

    这是因为set_tags 使用match.call 来获取未评估的标记名。当您向它传递一个表达式时,必须评估该表达式以获取实际标记。这会在 set_tags 内部遇到问题,因为必须同时处理父框架。

    一种解决方法是在调用set_tags 之前使用eval(substitute(*)) 强制计算表达式:

    set_my_tag2 <- function(resources_to_tag)
    {
        for(i in seq_len(nrow(resources_to_tag)))
        {
            resource <- resources_to_tag[i, ]
            tag <- resource$newtag
            obj <- az$
                get_subscription(resource$subscriptionid)$
                get_resource_group(resource$resourcegroup)$
                get_resource(type = resource$type, name = resource$name)
            eval(substitute(obj$set_tags(my_tag=.value), list(.value=resource$new_tag)))
        }
    }
    

    我会在下一个 AzureRMR 版本中解决这个问题。事后看来,最好避免所有这些 NSE 业务......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 2016-07-17
      • 1970-01-01
      • 2020-02-12
      • 2011-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多