TidierOrg / TidierPlots.jl

Tidier data visualization in Julia, modeled after the ggplot2 R package.
MIT License
214 stars 7 forks source link

Error executing examples #23

Closed tobi-lipede-mappa closed 11 months ago

tobi-lipede-mappa commented 1 year ago

Not sure whether I'm doing something wrong, but I can't seem to generate any plots. I get an error saying the DataFrame isn't defined, e.g.:

using TidierPlots
using DataFrames
using PalmerPenguins

penguins = dropmissing(DataFrame(PalmerPenguins.load()))

@ggplot(data = penguins) + 
    @geom_bar(aes(x = species)) +
    @labs(x = "Species")

# UndefVarError: `penguins` not defined
rdboyes commented 1 year ago

This code works as written for me. Are you sure you're executing the line penguins = dropmissing(DataFrame(PalmerPenguins.load()))? This line should create the penguins dataset

tobi-lipede-mappa commented 1 year ago

I am. If it helps at all, I was running Julia 1.10-beta2 on Pluto. I can access the dataframe outside of the macro so it should definitely be defined

rdboyes commented 1 year ago

Can you try running the code directly in the REPL? I haven't tested the package in Pluto

tobi-lipede-mappa commented 1 year ago

Confirmed it does work in the REPL. But in Pluto it gives the undefined error

rdboyes commented 1 year ago

ok - the issue is likely to do with how the environment is chosen for the call to Base.eval inside of pluto. I will take a look and see if I can fix it - in the meantime, a work-around if you want it is to use the AoG compatibility shown in the final example in the readme

tobi-lipede-mappa commented 1 year ago

Thank you for your help! Will do

rdboyes commented 11 months ago

I looked into this and was able to reproduce the error, but I can't figure out what's causing it. @kdpsingh any ideas?

kdpsingh commented 11 months ago

Thanks for tagging me. I'll look into it also.

rdboyes commented 11 months ago

The weird thing is that if I create new macros inside the notebook - like define @ggplot_pluto and @geom_point_pluto without changing anything about them, they work as expected. The issue might be something to do with how Pluto manages modules?

kdpsingh commented 11 months ago

This is probably a scoping issue related to the use of eval() in the package, but I haven't yet had a chance to actually try it out to see what the error is.

rdboyes commented 11 months ago

It might be possible to refactor a bit and remove the eval calls - I had intended to try to do that eventually anyway

rdboyes commented 11 months ago

I realized as I was trying to refactor the eval calls out that the package actually barely needs macros at all. I have a working version in this branch https://github.com/TidierOrg/TidierPlots.jl/tree/no-eval that only uses one macro (@aes) and even that can be replaced with a function if you're willing to use strings for column selection ...

rdboyes commented 11 months ago

This issue is resolved as of version 0.4.0! Screenshot 2023-09-28 175940

kdpsingh commented 11 months ago

If it works without macros, that's great! Even if you're using macros, there's no need to do any evaluation. If you return a quoted expression from a macro, the resulting expression automatically gets evaluated in the user's scope.

So if for any reason we need to go back to using macros, happy to help remove the evaluation piece.

kdpsingh commented 11 months ago

Does this work in Pluto now?

kdpsingh commented 11 months ago

Nevermind, I see from the screenshot that it does!