spgarbet / tangram

Table Grammar package for R
66 stars 3 forks source link

Style Environment "get" fails in jamovi #64

Closed spgarbet closed 4 years ago

spgarbet commented 4 years ago

See tail of issue #38.

spgarbet commented 4 years ago

@sbalci

The issue you're having is caused by hmisc being assumed to be in the global context. In jamovi it's not. I need to understand the environment contexts in jamovi and the way libraries / code are handled, and more about this error. What tangram is allowing the user to do is override in their global enviroment style definitions and these are used in preference to the ones in the library itself. jamovi is doing something a little different with environments.

So there are two issues here. The first I can fix easily, in that if the "get()" call fails it keeps executing to it's fall back. So a try block is needed. But in jamovi this will not result in the intended behavior.

If I put this R code into jamovi:

x <- 3 get("x", envir = globalenv())

Does this fail? If not, then if the 'get' call is inside a library it would fail. Can you describe how the environments are loaded?

spgarbet commented 4 years ago

I tried to get jamovi installed on Ubuntu 20 and I couldn't get that to work because of flatpak. They have a ticket open discussing that issue.

sbalci commented 4 years ago

Hi, I get following error when using tangram@0.3.2

Debug

Error in get("x", envir = globalenv()): 'x' nesnesi bulunamadı

private$.run()
get("x", envir = globalenv())
Ekran Resmi 2020-05-05 20 47 47

The tangram function works as expected when using tangram@0.3.2 see below gif:

jamovi-and-tangram-crosstable

spgarbet commented 4 years ago

The request for "x" has nothing to do with tangram, but how environments are structured inside jamovi.

spgarbet commented 4 years ago

tangram needs to be able to find the variable that defines the styles. That variable could be in the user's current environment.

x <- 3

creates a variable in the current environment in R.

get("x", envir=globalenv())

Is a meta call that says, give me the object named "x", start in the current environment and search all parents up to the global env.

Normally if tangram is loaded, it pulls from that environment. Otherwise it pulls from the local environment. I can force it to load from the package environment as a fall back. If a user copies that 'hmisc' variable and overrides it's style functions then it will not be found. So to fix this properly I need to understand what jamovi is doing with environments. Why does the "get" call fail?

https://www.r-bloggers.com/environments-in-r/

What does this print out (no libraries loaded)

g <- globalenv()
while (environmentName(g) != 'R_EmptyEnv') {
  g <- parent.env(g); cat(str(g, give.attr=F))
}
spgarbet commented 4 years ago

Actually, here's a better thing to try (no libraries)

x <-  3
get("x", inherits=TRUE)
sbalci commented 4 years ago

Hi, Thank you for your time.

[1] 3

Ekran Resmi 2020-05-06 04 07 37
g <- globalenv()
while (environmentName(g) != 'R_EmptyEnv') {
  g <- parent.env(g); cat(str(g, give.attr=F))
}

I get no result. Probably stuck in while loop??

Best wishes

spgarbet commented 4 years ago

I think I understand now. I will have a fix that requires some testing.

spgarbet commented 4 years ago

Is it possible for you to pull the github version and test?

sbalci commented 4 years ago

Hi, I get following error:

Debug

Error in get(style, envir = globalenv()): 'hmisc' nesnesi bulunamadı

private$.run()
tangram::html5(tangram::tangram(formula, self$data), fragment = TRUE, inline = style, caption = paste0("Cross Table for Dependent ", self$options$group), id = "tbl3")
tangram::tangram(formula, self$data)
tangram.formula(formula, self$data)
tryCatch(get(style, envir = globalenv()), finally = get(style))
tryCatchList(expr, classes, parentenv, handlers)
get(style, envir = globalenv())
spgarbet commented 4 years ago

I will get the hang of this yet. I pushed a new patch that might fix it.

sbalci commented 4 years ago

Hi, Thank you very much for your efforts. I think the hmisc issue is resolved 👍

Although now I get attempt to apply non-function error.

I assume I am having problem in converting inputs into a formula (A~ B +C). I am trying to get over it.

Debug

Error in value[[3L]](cond): fonksiyon olmayana uygulama denemesi

private$.run()
tangram::html5(tangram::tangram(formula, self$data), fragment = TRUE, inline = self$options$sty, caption = paste0("Cross Table for Dependent ", self$options$group), id = "tbl3")
tangram::tangram(formula, self$data)
tangram.formula(formula, self$data)
tryCatch(get(style, envir = globalenv()), error = get(style))
tryCatchList(expr, classes, parentenv, handlers)
tryCatchOne(expr, names, parentenv, handlers[[1]])
spgarbet commented 4 years ago

Change the call to the following:

tangram::html5(tangram::tangram(formula, self$data), style="hmisc", caption = paste0("Cross Table for Dependent ", self$options$group), id = "tbl3")

spgarbet commented 4 years ago

Did that work?

sbalci commented 4 years ago

I am trying with iris now. Code works in R. But when I call it within jamovi I get following error attempt to apply non-function

Debug

Error in value[[3L]](cond): fonksiyon olmayana uygulama denemesi

private$.run()
tangram::tangram("Species ~ Sepal.Length + Sepal.Width + Petal.Length", iris, style = "nejm", caption = paste0("Cross Table for Dependent "), id = "tbl3")
tangram.character("Species ~ Sepal.Length + Sepal.Width + Petal.Length", iris, style = "nejm", caption = paste0("Cross Table for Dependent "), id = "tbl3")
tangram.formula(trimws(x), ...)
tryCatch(get(style, envir = globalenv()), error = get(style))
tryCatchList(expr, classes, parentenv, handlers)
tryCatchOne(expr, names, parentenv, handlers[[1]])
spgarbet commented 4 years ago

Just saw this. There's one more thing I can try.

spgarbet commented 4 years ago

I've changed this to getAnywhere which searches all loaded environments.

sbalci commented 4 years ago

Hi @spgarbet, Thank you for your time.

This one works in R but inside jamovi I am still getting the same attempt to apply non-function error.

I am trying to figure out why. I tried to call tangram::tangram or tangram:::tangram.formula inside jamovi it returns the function texts correctly. So is the problem in calling from environment or recognizing the text as formula?

What is Error in value[[3L]](cond) error?

spgarbet commented 4 years ago

There is an R meta call, get. This is used to get the style from a string. Local environment overrides package environment. I don't know what that error is. I have a windows loaner box I will play with on this.

spgarbet commented 4 years ago

I have it installed on a Windows box. I can't figure out how to get at executing R code inside Jamovi. What are the steps to debug/triage this in the Jamovi environment?

sbalci commented 4 years ago

Let me know how it goes :)

spgarbet commented 4 years ago

jmvtools::check("C://Program Files//jamovi//bin")

jamovi compiler

jamovi 1.2.19 found at C:\Program Files\jamovi\bin\jamovi.exe

jmvtools::install()

jamovi compiler

jamovi could not be found!

sbalci commented 4 years ago

Please try this:

jmvtools::install(home = "C://Program Files//jamovi//bin")
spgarbet commented 4 years ago

I got the compile working, still working through the developer material for Jamovi.

spgarbet commented 4 years ago

I tried just R6 to see if that was the issue it was not. I think I have a workaround that would extend the functionality of the whole package. I'm still working on understanding how Jamovi modules compile.

spgarbet commented 4 years ago

Try this:

tangram( ..., transform=tangram::hmisc, ...)

sbalci commented 4 years ago

Hi, It seems to work. This is the code:

                tabletangram <-
                    tangram::html5(
                        tangram::tangram(
                            formula,
                            mydata,
                            transform=tangram::hmisc
                        ),
                        fragment = TRUE,
                        # inline = sty,
                        caption = paste0(
                            "Cross Table for Dependent ",
                            self$options$group),
                        id = "tbl3")

And this is the output:

Ekran Resmi 2020-06-05 21 33 13
spgarbet commented 4 years ago

Excellent. I can close this now.