Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: object 'subset_colors' not found - at Step 8 of your example code #1

Open
ericmelse opened this issue Jun 2, 2024 · 7 comments

Comments

@ericmelse
Copy link

Dear Niley,

With great interest I am reading your Medium post How to Create a 3D Population Density Map in R and exercise your code example as to create the Bangladesh tilted map.

My issue now is that at Step 8, when I run your code:

# Step 8: Define Color Palette
# Select a color palette from the MetBrewer library and customize it for your map.
color <- MetBrewer::met.brewer(name="Benedictus", direction = -1)
tx <- grDevices::colorRampPalette(subset_colors, bias = 4.5)(256)
swatchplot(tx)
swatchplot(subset_colors)

I get the following error message immediately after the line tx ...:
Error: object 'subset_colors' not found
I admit that I am not a savvy R user and only can guess that something has first to be declared as to get the object subset_colors in memory so that the tx object can be created.

Time permitting, I appreciate your helping hand as to solve this Error.

@niloy-biswas
Copy link
Owner

I believe just using color instead of subset_colors would solve your problem.

tx <- grDevices::colorRampPalette(color, bias = 4.5)(256)
swatchplot(tx)
swatchplot(color)

@ericmelse
Copy link
Author

ericmelse commented Jun 3, 2024

Dear Niley,

Indeed, changing subset_colors into colors solved the issue and I am now able to run the example code.
Another minor suggestion, I have edited the code of Step 1 and Step 2 to accommodate the installation of all required packages for those users who start with a basic R configuration (as to be able to run your code example), like:

# Step 1: Install Required Packages
install.packages("tidyverse", dependencies=TRUE)
install.packages("ggplot2", dependencies=TRUE)
install.packages("sf", dependencies=TRUE)
install.packages("tmap", dependencies=TRUE)
install.packages("mapview", dependencies=TRUE)
install.packages("stars", dependencies=TRUE)
install.packages("rayshader", dependencies=TRUE)
install.packages("rayrender", dependencies=TRUE)
install.packages("MetBrewer", dependencies=TRUE)
install.packages("colorspace", dependencies=TRUE)
install.packages("extrafont", dependencies=TRUE)
install.packages("magick", dependencies=TRUE)

# Step 2: Load Packages and Set Options
# Load the required libraries and set the RGL options:
options(rgl.useNULL = FALSE)

require(tidyverse)
require(ggplot2)
require(sf)
require(tmap)
require(mapview)
require(stars)
require(rayshader)
require(rayrender)
require(MetBrewer)
require(colorspace)
require(extrafont)
require(magick)

I proceed to Step 9 and render the 3D representation of the population density using Rayshader.
After some rotation, the result is like:
Code result rgl_rglwidget()
Next, I proceed to Step 10 and save to the file Dhaka_Benedictus_4.png without changing anything in your code.
But, the file that is saved is an 1x1 pixel image:
Code_result_Dhaka_Benedictus_4
Do you have an explanation why the image is not saved correctly and a possible solution to get it to work as it should?

Further news on this issue
Unaware that I was that R was still running, the following is reported by R:

Warning messages:
1: In render_highquality(filename = outfile, interactive = F, lightdirection = 55,  :
  When `sample_method = "sobol_blue"`, `samples` must be less than or equal to 256. Setting `sample_method` to `"sobol"`.
2: In UseMethod("depth") :
  no applicable method for 'depth' applied to an object of class "NULL"
3: In UseMethod("depth") :
  no applicable method for 'depth' applied to an object of class "NULL"

So, I try the export again with samples set to 256.
And, this time the code does complete running without any error prompt.
However, the result image (png file) is not what we expect it to be:
Dhaka_Benedictus_4
Do you have a suggestion what I now should do with the code?

  render_highquality(
    filename = outfile,
    interactive = F,
    lightdirection = 55, #Degree
    lightaltitude = c(30, 80),
    lightcolor = c("white", "white"),  # Set both lights to white
    lightintensity = c(600, 100),
    width = 1400,
    height = 1580,
    samples = 256
  )

@niloy-biswas
Copy link
Owner

Did you wait until the rendering was completed?

The rendering time actually varies from device to device. In my case, my MacBook M1 takes around 15-20 minutes to complete a single rendering and save the actual picture. Before that, the file is saved as a 1x1 pixel image.

@ericmelse
Copy link
Author

Yes, Niloy, I learned as much. Your code, after using your suggestion, does complete and after the saving of the above result image (png file), in the RStudio console your timers are reported:

2024-06-03 10:16:40.77362 
8.30475066900253 

So, my assumption now is that 'something' is either wrong or missing that leads to the rather black 3D image of my previous post instead of the color image in the Rayshader window.
Any suggestion you can think of is appreciated.

@niloy-biswas
Copy link
Owner

Sorry Man, Couldn't help without being able to reproduce the error actually.

@ericmelse
Copy link
Author

Dear Niloy, thank you for taking care. Yes, of course, when you cannot replicate my problem then it is rather difficult to solve it. Next, I ran your Github example file SriLanka-Population-Density.R and like above, the plot is just fine displayed in the 3D plot viewer, like:
Code result Sri_Lanka_BuPu2
But, sadly, the image is saved just as black, like:
Sri_Lanka_BuPu2
Something disagrees between the code and deep down in the package that is used as no errors are reported.
At this point I can only let it go.

@jsonkao
Copy link

jsonkao commented Jul 24, 2024

Hi @ericmelse , I was getting a black iamge on all the scripts as well. After rendering the XQuartz plot with plot_3d, I could run render_highquality("filename.png") successfully. Seemingly only after running that first render_highquality without any arguments, I slowly added arguments about light and width, and subsequent calls have worked. Will edit this answer if I find a better reason/cause/solution.

It possibly has to do with removing the interactive = F option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants