英文:
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 <- 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...
答案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 <- glue("public.my.package::{x}")
private <- glue("private.my.package::{x}")
That creates two strings, "public.my.package::somename"
and "public.my.package::somename"
where somename
is the contents of all_data
. Comparing those says they are different.
In your second attempt you had
public <- as.name(glue("public.my.package::{x}"))
private <- as.name(glue("private.my.package::{x}"))
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 <- eval(parse(text = glue("public.my.package::{x}")))
private <- eval(parse(text = glue("private.my.package::{x}")))
NB: in my comment, I incorrectly suggested using code like eval(glue("public.my.package::{x}"))
. 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 <- 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")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论