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

A better answer for question 1 in chapter 9 #39

Open
arashHaratian opened this issue Dec 23, 2020 · 3 comments
Open

A better answer for question 1 in chapter 9 #39

arashHaratian opened this issue Dec 23, 2020 · 3 comments

Comments

@arashHaratian
Copy link

library(shiny)
library(ambient)
ui <- fluidPage(
  imageOutput("image_output"),
  actionButton("generate", "Generate an image"),
  downloadButton("download", "downlaod the image")
)

server <- function(input, output, session) {
  noise <- NULL
  grid <- NULL
  
  image <- eventReactive(input$generate,{
    noise <<- noise_worley(c(100, 100))
    grid <<- long_grid(seq(1, 10, length.out = 1000), seq(1, 10, length.out = 1000))
    grid$noise <<- gen_worley(grid$x, grid$y, value = 'distance')
    dir <- paste0(tempdir(),"/noise.png")
    png(dir)
    plot(grid, noise)
    dev.off()
    
    dir
  })
  
  output$image_output <- renderImage({

    list(src = image())
  })
  
  output$download <- downloadHandler(
    filename = "noise.png",
    content = function(file){
      png(file)
      plot(grid, noise)
      dev.off()
    }
  )
}

shinyApp(ui, server)
@MayaGans
Copy link
Contributor

MayaGans commented Jan 2, 2021

Hi @arashHaratian you mentioned this was for question 1 but I think the solution we provide, outside of Shiny, may be more in line with the prompt?

Use the ambient package by Thomas Lin Pedersen to generate worley noise and download a PNG of it.

Is this maybe referring to another question in the chapter?

@arashHaratian
Copy link
Author

no.
I think the question asks us to make an app to download the generated noise (to use download functionality in shiny), not to open a graphics device and save it manually.

@MayaGans
Copy link
Contributor

MayaGans commented Jan 2, 2021

Hmm that does make sense to me, what do you think, @marlycormar ?

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

2 participants