R Notebook

This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code. Here is an inline note.1 Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter. 2

library(plotly)
Caricamento del pacchetto richiesto: ggplot2

Caricamento pacchetto: 'plotly'
Il seguente oggetto è mascherato da 'package:ggplot2':

    last_plot
Il seguente oggetto è mascherato da 'package:stats':

    filter
Il seguente oggetto è mascherato da 'package:graphics':

    layout
library(shiny)


mandelbrot_set <- function(xmin, xmax, ymin, ymax, nx, ny, iter_max) {
  x <- seq(xmin, xmax, length.out = nx)
  y <- seq(ymin, ymax, length.out = ny)
  C <- outer(x, 1i * y, "+")
  Z <- matrix(0.0, nrow = length(x), ncol = length(y))
  K <- matrix(0.0, nrow = length(x), ncol = length(y))
  for (k in 1:iter_max) {
    mask <- Mod(Z) <= 2
    Z[mask] <- Z[mask] + C[mask]  # Sostituzione del quadrato con una somma iterativa (approssimazione di un integrale)
    K[mask & K == 0 & Mod(Z) > 2] <- k
  }
  K[K == 0] <- iter_max
  return(K)
}

shinyApp(
  ui = fluidPage(
    titlePanel("Insieme di P"),
    sidebarLayout(
      sidebarPanel(
        sliderInput("xmin", "xmin", min = -2.5, max = 1.5, value = -2.0, step = 0.01),
        sliderInput("xmax", "xmax", min = -2.5, max = 1.5, value = 1.0, step = 0.01),
        sliderInput("ymin", "ymin", min = -2.0, max = 2.0, value = -1.5, step = 0.01),
        sliderInput("ymax", "ymax", min = -2.0, max = 2.0, value = 1.5, step = 0.01),
        numericInput("nx", "nx", value = 800, min = 100, max = 2000),
        numericInput("ny", "ny", value = 800, min = 100, max = 2000),
        numericInput("iter_max", "Iterazioni Massime", value = 100, min = 10, max = 1000)
      ),
      mainPanel(
        plotlyOutput("plot")
      )
    )
  ),
  server = function(input, output) {
    output$plot <- renderPlotly({
      K <- mandelbrot_set(input$xmin, input$xmax, input$ymin, input$ymax, input$nx, input$ny, input$iter_max)
      plot_ly(
        z = ~K,
        type = "heatmap",
        colorscale = list(
          c(0, "black"), 
          c(0.1, "purple"), 
          c(0.2, "blue"), 
          c(0.3, "cyan"), 
          c(0.4, "green"), 
          c(0.5, "yellow"), 
          c(0.6, "orange"), 
          c(0.7, "red"), 
          c(0.8, "pink"), 
          c(0.9, "brown"), 
          c(1, "white")
        )
      ) %>%
        layout(
          xaxis = list(title = "Re(z)"),
          yaxis = list(title = "Im(z)"),
          title = "Insieme di P"
        )
    })
  }
)

Shiny applications not supported in static R Markdown documents

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

Note

  1. Inlines notes are easier to write, since you don’t have to pick an identifier and move down to type the note.↩︎

  2. Questa è una nota a margine.↩︎