GithubHelp home page GithubHelp logo

Comments (10)

rmendels avatar rmendels commented on September 2, 2024

okay a reproducible example. To run this you will need to have installed the packages 'rerddap' and 'plotdap' and the packages they depend on.

library(rerddap)
library(plotdap)

murSST_west <- griddap(
  'jplMURSST41', 
  latitude = c(22, 51), 
  longitude = c(-140, -105),
  time = c('last', 'last'), 
  fields = 'analysed_sst'
)
p <-  add_griddap(plotdap(),
                  murSST_west, 
                  ~analysed_sst,
                  fill = 'thermal'
)

library(ggnewscale)
p$ggplot <- p$ggplot + new_scale_colour()
Error in get(as.character(FUN), mode = "function", envir = envir) : 
  object 'guide_colourbar' of mode 'function' was not found

library(ggplot2)
p$ggplot <- p$ggplot + new_scale_colour()

This suggests that somewhere guide_colourbar is not properly imported, though I could be wrong.

from ggnewscale.

eliocamp avatar eliocamp commented on September 2, 2024

It's a small wrinkle when geting functions. Thanks for bringing into attention, I'll see what I can do!

from ggnewscale.

rmendels avatar rmendels commented on September 2, 2024

Thanks. I was able to isolate the line in your code where it was occurring, but it was beyond what I know. Let me know if you think you have a fix, I will test it. Otherwise in the examples in my package that use this, i will just note that ggplot2 must be loaded.

Thanks for your work on this. A lot of what we do uses overlays, and these have been difficult because of the restrictions of ggplot2

from ggnewscale.

eliocamp avatar eliocamp commented on September 2, 2024

I think it should be fixed in the dev version. Could you install and test it with

devtools::install_github("eliocamp/ggnewscale@dev")

?

from ggnewscale.

rmendels avatar rmendels commented on September 2, 2024

Just did two quick test, including the overlay which I have as an example which was causing this, and they worked fine. Perfect thank you. When you submit this to CRAN, what will be the version number? I will make certain my imports require that version or higher. If you have the time and feel like it, could you explain to me the issue. As I said, I looked at your code, found the line (it was in match.fun) but couldn't see what was doing it and was hoping to learn both the problem and the fix.

from ggnewscale.

eliocamp avatar eliocamp commented on September 2, 2024

Yes, no problem.

match.fun()/get(mode = "function") searches for functions in the environment they are called. So match.fun("mean") searches the environment for a function called "mean" and returns it, but only if mean is defined in the environment in which match.fun() is being called.

The guide argument in ggplot2 scales can be a function or a character with the name of the function. By default it's "colourbar" or "legend" depending on the type of scale. The relevant functions are guide_colourbar() and guide_legend(), which are defined in the ggplot2 package.
Now, if ggplot2 is not loaded, then neither function is present in the environment, so match.fun("guide_colourbar") returns an error.

match.fun("guide_colourbar")
#> Error in get(as.character(FUN), mode = "function", envir = envir): object 'guide_colourbar' of mode 'function' was not found

library(ggplot2)
match.fun("guide_colourbar")
#> function (title = waiver(), title.position = NULL, title.theme = NULL, 
#>     title.hjust = NULL, title.vjust = NULL, label = TRUE, label.position = NULL, 
#>     label.theme = NULL, label.hjust = NULL, label.vjust = NULL, 
#>     barwidth = NULL, barheight = NULL, nbin = 20, raster = TRUE, 
#>     frame.colour = NULL, frame.linewidth = 0.5, frame.linetype = 1, 
#>     ticks = TRUE, ticks.colour = "white", ticks.linewidth = 0.5, 
#>     draw.ulim = TRUE, draw.llim = TRUE, direction = NULL, default.unit = "line", 
#>     reverse = FALSE, order = 0, available_aes = c("colour", "color", 
#>         "fill"), ...) 
#> {
#>     if (!is.null(barwidth) && !is.unit(barwidth)) 
#>         barwidth <- unit(barwidth, default.unit)
#>     if (!is.null(barheight) && !is.unit(barheight)) 
#>         barheight <- unit(barheight, default.unit)
#>     structure(list(title = title, title.position = title.position, 
#>         title.theme = title.theme, title.hjust = title.hjust, 
#>         title.vjust = title.vjust, label = label, label.position = label.position, 
#>         label.theme = label.theme, label.hjust = label.hjust, 
#>         label.vjust = label.vjust, barwidth = barwidth, barheight = barheight, 
#>         nbin = nbin, raster = raster, frame.colour = frame.colour, 
#>         frame.linewidth = frame.linewidth, frame.linetype = frame.linetype, 
#>         ticks = ticks, ticks.colour = ticks.colour, ticks.linewidth = ticks.linewidth, 
#>         draw.ulim = draw.ulim, draw.llim = draw.llim, direction = direction, 
#>         default.unit = default.unit, reverse = reverse, order = order, 
#>         available_aes = available_aes, ..., name = "colorbar"), 
#>         class = c("guide", "colorbar"))
#> }
#> <bytecode: 0x55f2a3c19680>
#> <environment: namespace:ggplot2>

Created on 2020-02-13 by the reprex package (v0.3.0)

In your example, you call new_scale(), which eventually calls match.fun(). match.fun() then looks for a function called "guide_colourbar". In which environment? Well, first it looks it up in the ggnewscale environment, and when it doesn't find it, goes up a level, and so forth until it reaches the global environment. There it finally finds it, if and only if ggplot2 was loaded (or if you had manually defined a function with that name).

The solution was for me to import both functions into the ggnewscale package here. This makes it so that they are available in the ggnewscale environment, so that match.fun() finds them there. (I also had to change it to get() because for some reason match.fun() worked interactively but failed in CMD R Check 🤷‍♀️️).

I hope this answer is clear enough. The whole environments stuff is rather convoluted and I'm not at all an expert in it.

from ggnewscale.

eliocamp avatar eliocamp commented on September 2, 2024

Ah, sorry. I'll send a patch to CRAN today with the version 0.4.1.

from ggnewscale.

rmendels avatar rmendels commented on September 2, 2024

Thanks for that and the explanation.

from ggnewscale.

eliocamp avatar eliocamp commented on September 2, 2024

No problem :)
Version 0.4.1 is now on CRAN and in the master branch.

from ggnewscale.

rmendels avatar rmendels commented on September 2, 2024

Thanks again.

from ggnewscale.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.