在R中,我如何测试两个包的数据集是否相同

huangapple go评论74阅读模式
英文:

In R how can I test if two package datasets are the same

问题

I have a private package that I want to release to the public but before I do I want to compare the datasets in the two packages. I can't figure out how to write a function to tell the identical() function to look inside two packages. Calls like this are fine:

identical(public.my.package::thingy, private.my.package::thingy)
identical(public.my.package::whatsit, private.my.package::whatsit)

but I can't figure out how to wrap them into something like this:

check <- function(x) {
  public <- glue("public.my.package::{x}")
  private <- glue("private.my.package::{x}")
  identical(
    public, private 
  )  
}

Doing check(all_data) gives me this:

Error in eval(parse(text = text, keep.source = FALSE), envir) :
object 'all_data' not found

which makes some sense but if I quote the dataset name I end up with a string instead of a name and of course the strings are not the same. If I try to tell it these are names like this:

check <- function(x) {
  browser()
  public <- as.name(glue("public.my.package::{x}"))
  private <- as.name(glue("private.my.package::{x}"))
  identical(
    public, private 
  )  
}

It says the objects are not identical when they are.

Help...

英文:

I have a private package that I want to release to the public but before I do I want to compare the datasets in the two packages. I can't figure out how to write a function to tell the identical() function to look inside two packages. Calls like this are fine:

identical(public.my.package::thingy, private.my.package::thingy)  
identical(public.my.package::whatsit, private.my.package::whatsit)

but I can't figure out how to wrap them into something like this:

check &lt;- function(x) {
  public &lt;- glue(&quot;public.my.package::{x}&quot;)
  private &lt;- glue(&quot;private.my.package::{x}&quot;)
  identical(
    public, private 
  )  
}

Doing check(all_data) gives me this:

Error in eval(parse(text = text, keep.source = FALSE), envir) :
object &#39;all_data&#39; not found

which makes some sense but if I quote the dataset name I end up with a string instead of a name and of course the strings are not the same. If I try to tell it these are names like this:

check &lt;- function(x) {
  browser()
  public &lt;- as.name(glue(&quot;public.my.package::{x}&quot;))
  private &lt;- as.name(glue(&quot;private.my.package::{x}&quot;))
  identical(
    public, private 
  )  
}

It says the objects are not identical when they are.

Help...

答案1

得分: 2

public <- eval(parse(text = glue("public.my.package::{x}")))
private <- eval(parse(text = glue("private.my.package::{x}")))

英文:

In your first attempt you had

public &lt;- glue(&quot;public.my.package::{x}&quot;)
private &lt;- glue(&quot;private.my.package::{x}&quot;)

That creates two strings, &quot;public.my.package::somename&quot; and &quot;public.my.package::somename&quot; where somename is the contents of all_data. Comparing those says they are different.

In your second attempt you had

public &lt;- as.name(glue(&quot;public.my.package::{x}&quot;))
private &lt;- as.name(glue(&quot;private.my.package::{x}&quot;))

That creates two object names from the strings. But object names are objects in R, so comparing them says they are different names.

In fact, what you want to do is to evaluate the two strings. To do that, you need to parse them first, then evaluate the expression. (The :: operator means that public.my.package::all_data isn't a name, it's an expression.) This will do that:

public &lt;- eval(parse(text = glue(&quot;public.my.package::{x}&quot;)))
private &lt;- eval(parse(text = glue(&quot;private.my.package::{x}&quot;)))

NB: in my comment, I incorrectly suggested using code like eval(glue(&quot;public.my.package::{x}&quot;)). That's wrong: evaluating a string just gives you the string. You need to parse it to get a language object which can be evaluated.

答案2

得分: 0

在@user2554330的帮助下,我能够使其以以下方式工作:

check <- function(x) {
    public <- 
        eval(
            parse(
                text = glue("public.my.package::{x}")
            )
        )
    private <- 
        eval(
            parse(
                text = glue("my.package::{x}")
            )
        )
    identical(
        public, private 
    )  
}
check("the_data")
英文:

With help from @user2554330 I was able to get it to work like this:

check &lt;- function(x) {
    public &lt;- 
        eval(
            parse(
                text = glue(&quot;public.my.package::{x}&quot;)
            )
        )
    private &lt;- 
        eval(
            parse(
                text = glue(&quot;my.package::{x}&quot;)
            )
        )
    identical(
        public, private 
    )  
}
check(&quot;the_data&quot;)

huangapple
  • 本文由 发表于 2023年5月13日 22:37:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定