Skip to content

Instantly share code, notes, and snippets.

@jnolis
Created April 13, 2020 17:47
Show Gist options
  • Save jnolis/045403c4aa5e9b28e82d1c83b3dfe9ef to your computer and use it in GitHub Desktop.
Save jnolis/045403c4aa5e9b28e82d1c83b3dfe9ef to your computer and use it in GitHub Desktop.
Connecting many R packages at once
library(flametree)
library(brickr)
library(ggplot2)
library(png)
library(magrittr)
library(rayshader)
library(rtweet)
# CREATE TREE ------------------------------
temp_file <- tempfile(fileext=".png")
dat <- flametree_grow(seed = 4, time = 5, angle = c(-20,10,30), split = 3) # data structure
flametree_img <- flametree_plot(tree = dat,
background = "#FFFFFF",
palette = "scico::berlin") # ggplot object
ggsave(temp_file, plot = flametree_img, width = 4, height = 4, dpi = 64)
# TURN IT INTO LEGO ------------------------
brickr_img <-
temp_file %>%
readPNG() %>%
image_to_mosaic(img_size = 32) %>%
build_mosaic()
ggsave(temp_file, plot = brickr_img, width = 4, height = 4, dpi = 128)
# RENDER IT IN 3D --------------------------
grayscale_raster <-
temp_file %>%
readPNG() %>%
apply(c(1,2),mean) %>%
{. * 255}
grayscale_raster %>%
sphere_shade(texture = "imhof1",zscale=3) %>%
add_water((grayscale_raster > 192) * 1, color = "desert") %>%
add_shadow(ray_shade(rayshader:::flipud(grayscale_raster), zscale = 3), 0.5) %>%
add_shadow(ambient_shade(rayshader:::flipud(grayscale_raster)), 0.5) %>%
plot_3d(rayshader:::flipud(grayscale_raster), zscale = 10, fov = 0, theta = 293, zoom = 0.75, phi = 55, windowsize = c(1000, 800))
Sys.sleep(0.2)
render_snapshot(filename = temp_file, clear=TRUE)
# TWEET IT ----------------------------------
rtweet::post_tweet(status="I used @djnavarro's flametree R package to make a tree, @ryantimpe's brickr package to turn it into LEGO, @tylermorganwall's rayshader package to make it 3D, then @kearneymw rtweet package to tweet it. #quarantine", media=temp_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment