TidierOrg / TidierPlots.jl

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

Error displaying inside Pluto #51

Closed Dale-Black closed 9 months ago

Dale-Black commented 9 months ago

I am trying to run the TiderCourse demo

@chain meds_clean begin
    @rename(id = patient)
    @filter(ismissing(stop))
    @group_by(id)
    @summarize(num_meds = n())
    @left_join(patients_clean, _)
    @mutate(num_meds = replace_missing(num_meds, 0))
    @mutate(today = mdy("01-01-2023"))
    @mutate(age = if_else(!ismissing(deathdate) && deathdate < today,
                          deathdate - birthdate,
                          today - birthdate))
    @mutate(age = Dates.days(age)/365.25)
    ggplot(@aes(x = age, y = num_meds, color = gender))
    geom_point(alpha = 1/10)
    geom_smooth()
    labs(title = "Relationship between age and \
                  the number of medications people take",
         subtitle = "Stratified by gender",
         x = "Age (years)",
         y = "Number of medications",
         color = "Gender")
    theme_black()
end

This gives the following error

Failed to show value:

Tuple field type cannot be Union{}

map(::Function, ::Dictionaries.Indices{Union{}})@map.jl:91
unnest(::Vector{@NamedTuple{}}, ::Dictionaries.Indices{Union{}})@layer.jl:81
unnest_dictionaries(::Vector{@NamedTuple{}})@layer.jl:84
map(::AlgebraOfGraphics.var"#182#183"{AlgebraOfGraphics.SmoothAnalysis}, ::AlgebraOfGraphics.ProcessedLayer)@layer.jl:101
(::AlgebraOfGraphics.SmoothAnalysis)(::AlgebraOfGraphics.ProcessedLayer)@smooth.jl:8
process(::AlgebraOfGraphics.Layer)@processing.jl:102
iterate(::Base.Generator, ::Vararg{Any})@generator.jl:47[inlined]
collect_to!(::Vector{AlgebraOfGraphics.ProcessedLayer}, ::Base.Generator{AlgebraOfGraphics.Layers, typeof(AlgebraOfGraphics.process)}, ::Int64, ::Int64)@array.jl:892
collect_to_with_first!(::Vector{AlgebraOfGraphics.ProcessedLayer}, ::AlgebraOfGraphics.ProcessedLayer, ::Base.Generator{AlgebraOfGraphics.Layers, typeof(AlgebraOfGraphics.process)}, ::Int64)@array.jl:870
collect(::Base.Generator{AlgebraOfGraphics.Layers, typeof(AlgebraOfGraphics.process)})@array.jl:844
map@abstractarray.jl:3310[inlined]
AlgebraOfGraphics.ProcessedLayers(::AlgebraOfGraphics.Layers)@layers.jl:41
var"#compute_axes_grid#120"(::@NamedTuple{ylabel::String, height::Int64, xlabel::String, title::String, width::Int64, subtitle::String}, ::@NamedTuple{}, ::typeof(AlgebraOfGraphics.compute_axes_grid), ::AlgebraOfGraphics.Layers)@layers.jl:114
compute_axes_grid@layers.jl:110[inlined]
var"#compute_axes_grid#117"(::@NamedTuple{ylabel::String, height::Int64, xlabel::String, title::String, width::Int64, subtitle::String}, ::@NamedTuple{}, ::typeof(AlgebraOfGraphics.compute_axes_grid), ::Makie.Figure, ::AlgebraOfGraphics.Layers)@layers.jl:100
compute_axes_grid@layers.jl:97[inlined]
#240@draw.jl:21[inlined]
update@draw.jl:10[inlined]
var"#plot!#239"(::@NamedTuple{ylabel::String, height::Int64, xlabel::String, title::String, width::Int64, subtitle::String}, ::@NamedTuple{}, ::typeof(MakieCore.plot!), ::Makie.Figure, ::AlgebraOfGraphics.Layers)@draw.jl:21
plot!@draw.jl:16[inlined]
(::AlgebraOfGraphics.var"#244#245"{@NamedTuple{ylabel::String, height::Int64, xlabel::String, title::String, width::Int64, subtitle::String}, @NamedTuple{}, @NamedTuple{}, @NamedTuple{}, @NamedTuple{}, AlgebraOfGraphics.Layers})(::Makie.Figure)@draw.jl:48
AlgebraOfGraphics@draw.jl:10[inlined]
AlgebraOfGraphics@draw.jl:47[inlined]
draw_ggplot(::TidierPlots.GGPlot)@draw.jl:0
#55@show.jl:86[inlined]
(::Makie.var"#270#271"{Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}}, TidierPlots.var"#55#56"{TidierPlots.GGPlot}, MakieCore.Attributes})()@theming.jl:191
lock(::Makie.var"#270#271"{Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}}, TidierPlots.var"#55#56"{TidierPlots.GGPlot}, MakieCore.Attributes}, ::ReentrantLock)@lock.jl:229
#with_theme#269@theming.jl:187[inlined]
with_theme@theming.jl:186[inlined]
show(::IOContext{IOBuffer}, ::MIME{Symbol("text/html")}, ::TidierPlots.GGPlot)@show.jl:84
show_richest(::IOContext{IOBuffer}, ::Any)@PlutoRunner.jl:1384
show_richest_withreturned@PlutoRunner.jl:1298[inlined]
format_output_default(::Any, ::Any)@PlutoRunner.jl:1020
format_output@PlutoRunner.jl:1037[inlined]
#53@PlutoRunner.jl:922[inlined]
var"#with_io_to_logs#126"(::Bool, ::Base.CoreLogging.LogLevel, ::typeof(PlutoRunner.with_io_to_logs), ::PlutoRunner.var"#53#56"{Base.UUID, Base.UUID, Module, TidierPlots.GGPlot, Dict{Tuple{UInt64, Int64}, Int64}})@PlutoRunner.jl:2670
PlutoRunner@PlutoRunner.jl:2617[inlined]
(::PlutoRunner.var"#130#131"{Bool, Base.CoreLogging.LogLevel, PlutoRunner.var"#53#56"{Base.UUID, Base.UUID, Module, TidierPlots.GGPlot, Dict{Tuple{UInt64, Int64}, Int64}}})()@PlutoRunner.jl:2694
with_logstate(::Function, ::Any)@logging.jl:515
with_logger@logging.jl:627[inlined]
#with_logger_and_io_to_logs#129@PlutoRunner.jl:2693[inlined]
with_logger_and_io_to_logs@PlutoRunner.jl:2692[inlined]
var"#formatted_result_of#50"(::Bool, ::typeof(PlutoRunner.formatted_result_of), ::Base.UUID, ::Base.UUID, ::Bool, ::Vector{String}, ::Nothing, ::Module)@PlutoRunner.jl:921
top-level scope@WorkspaceManager.jl:485

If I comment out a few lines just to get the basic plot

@chain meds_clean begin
    @rename(id = patient)
    @filter(ismissing(stop))
    @group_by(id)
    @summarize(num_meds = n())
    @left_join(patients_clean, _)
    @mutate(num_meds = replace_missing(num_meds, 0))
    @mutate(today = mdy("01-01-2023"))
    @mutate(age = if_else(!ismissing(deathdate) && deathdate < today,
                          deathdate - birthdate,
                          today - birthdate))
    @mutate(age = Dates.days(age)/365.25)
    ggplot(@aes(x = age, y = num_meds, color = gender))
    geom_point(alpha = 1/10)
    # geom_smooth()
    # labs(title = "Relationship between age and \
    #             the number of medications people take",
    #    subtitle = "Stratified by gender",
    #    x = "Age (years)",
    #    y = "Number of medications",
    #    color = "Gender")
    # theme_black()
end

I still get an error specific to Pluto:

Failed to show value:

Unsupported mime: text/html

error(::String)@error.jl:35
mime_to_rendertype(::Symbol)@screen.jl:39
convert(::Type{CairoMakie.RenderType}, ::MIME{Symbol("text/html")})@screen.jl:5
CairoMakie.Screen(::Makie.Scene, ::CairoMakie.ScreenConfig, ::IOContext{IOBuffer}, ::MIME{Symbol("text/html")})@screen.jl:263
var"#getscreen#1198"(::Base.Pairs{Symbol, Bool, Tuple{Symbol}, @NamedTuple{visible::Bool}}, ::typeof(Makie.getscreen), ::Module, ::Makie.Scene, ::IOContext{IOBuffer}, ::Vararg{Any})@display.jl:408
show(::IOContext{IOBuffer}, ::MIME{Symbol("text/html")}, ::Makie.Figure)@display.jl:0
show(::IOContext{IOBuffer}, ::MIME{Symbol("text/html")}, ::AlgebraOfGraphics.FigureGrid)@entries.jl:89
show(::IOContext{IOBuffer}, ::MIME{Symbol("text/html")}, ::TidierPlots.GGPlot)@show.jl:84
show_richest(::IOContext{IOBuffer}, ::Any)@PlutoRunner.jl:1384
show_richest_withreturned@PlutoRunner.jl:1298[inlined]
format_output_default(::Any, ::Any)@PlutoRunner.jl:1020
format_output@PlutoRunner.jl:1037[inlined]
#53@PlutoRunner.jl:922[inlined]
var"#with_io_to_logs#126"(::Bool, ::Base.CoreLogging.LogLevel, ::typeof(PlutoRunner.with_io_to_logs), ::PlutoRunner.var"#53#56"{Base.UUID, Base.UUID, Module, TidierPlots.GGPlot, Dict{Tuple{UInt64, Int64}, Int64}})@PlutoRunner.jl:2670
PlutoRunner@PlutoRunner.jl:2617[inlined]
(::PlutoRunner.var"#130#131"{Bool, Base.CoreLogging.LogLevel, PlutoRunner.var"#53#56"{Base.UUID, Base.UUID, Module, TidierPlots.GGPlot, Dict{Tuple{UInt64, Int64}, Int64}}})()@PlutoRunner.jl:2694
with_logstate(::Function, ::Any)@logging.jl:515
with_logger@logging.jl:627[inlined]
#with_logger_and_io_to_logs#129@PlutoRunner.jl:2693[inlined]
with_logger_and_io_to_logs@PlutoRunner.jl:2692[inlined]
var"#formatted_result_of#50"(::Bool, ::typeof(PlutoRunner.formatted_result_of), ::Base.UUID, ::Base.UUID, ::Bool, ::Vector{String}, ::Nothing, ::Module)@PlutoRunner.jl:921
top-level scope@WorkspaceManager.jl:485
rdboyes commented 9 months ago

The Pluto-specific error can be solved by running WGLMakie.activate!() in an earlier cell, and the Tuple Union{} error is caused by AlgebraOfGraphics.density() in Julia 1.10, but it is fixed in the most recent version of AoG. I have just pushed an update to TidierPlots that specifically requires the newer version of AoG. Once the PR merges, updating it should fix the issue!