Skip to content

Instantly share code, notes, and snippets.

@shabbychef
Created March 30, 2021 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shabbychef/ba80bb8b749a078fe6b714c9edd9d11b to your computer and use it in GitHub Desktop.
Save shabbychef/ba80bb8b749a078fe6b714c9edd9d11b to your computer and use it in GitHub Desktop.
suppressMessages({
library(shiny)
library(ggplot2)
library(dplyr)
library(tidyr)
library(viridis)
library(Rcpp)
})
init_x <<- c(-1,1)
init_y <<- c(-1,1)
cppFunction('
int mandelfoo(double x, double y,int maxit) {
int it=0;
double x2,y2,modulus;
double myx,myy;
myx=x;myy=y;
while (it < maxit) {
x2 = myx*myx;
y2 = myy*myy;
modulus = x2 + y2;
if (modulus > 4) { return(it); }
it++;
myy = 2 * myx * myy + y;
myx = x2 - y2 + x;
}
return(it);
}')
mandeldeez <- function(x,y,maxit=100) {
retv <- mapply(mandelfoo,x,y,MoreArgs=list(maxit=maxit))
}
server <- function(input, output, session) {
viewport <- reactiveValues(xmin=min(init_x),
xmax=max(init_x),
ymin=min(init_y),
ymax=max(init_y))
# When a double-click happens, check if there's a brush on the plot.
# If so, zoom to the brush bounds; if not, reset the zoom.
observeEvent(input$plot_click, {
brush <- input$plot_brush
if (!is.null(brush)) {
viewport$xmin <- brush$xmin
viewport$xmax <- brush$xmax
viewport$ymin <- brush$ymin
viewport$ymax <- brush$ymax
} else {
viewport$xmin <- min(init_x)
viewport$xmax <- max(init_x)
viewport$ymin <- min(init_y)
viewport$ymax <- max(init_y)
}
})
xyzs <- reactive({
parm <- tidyr::crossing(tibble(x=seq(viewport$xmin,viewport$xmax,length.out=input$dpi)),
tibble(y=seq(viewport$ymin,viewport$ymax,length.out=input$dpi))) %>%
mutate(mi=mandeldeez(x,y,input$resolution))
})
output$theplot <- renderPlot({
ph <- xyzs() %>%
ggplot(aes(x=x,y=y,fill=mi)) +
scale_fill_viridis(option='B',direction=-1) +
geom_tile(size=0) +
coord_equal() +
theme_void() +
geom_raster() +
guides(color=FALSE,fill=FALSE)
ph
},
height=700,width=1400)
# should have coords in bookmark. rats.
setBookmarkExclude(c('bookmark'))
observeEvent(input$bookmark,{
session$doBookmark()
})
}
ui <- shinyUI(
fluidPage(theme=shinythemes::shinytheme("spacelab"),
tags$head(
tags$script(src='test.js'),
tags$style(".table .alignRight {color: black; text-align:right;}"),
tags$link(rel="stylesheet", type="text/css", href="style.css")
),
titlePanel("Mandelbroit"),
sidebarLayout(position="left",
sidebarPanel(sliderInput("resolution","Depth: ",min=10,max=300,value=100,step=5),
sliderInput("dpi", "DPI: ",min=90,max=900,value=300,step=10),
bookmarkButton(id='bookmark'),
hr(),
width=2),
mainPanel(
width=10,
plotOutput('theplot',
dblclick="plot_click",
brush=brushOpts(id="plot_brush",resetOnNew=TRUE),
height='100%',width='100%')
) # mainPanel
) # sidebarLayout
,title="Mandelbroit Thingy"))
enableBookmarking(store='url')
foo <- shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment