I've been trying to code a shiny app that would let you crop an image (only height & with magick package) and being able to dipslay the height at the same time you move the crop slider button.
But as far as I have been working on it (one week ) and tried many things (read many things as well), I still couldn't get better than the code below.
It renders the size but only when the top right hand corner expand/diminish button of the window is pressed.
My code comes from the example from Mr.Jeroen here https://github.com/jeroen/shinymagick/blob/master/app.R
library(shiny)
# Minimal example of Shiny widget using 'magick' images
ui <- fluidPage(
titlePanel("Magick Shiny Demo"),
sidebarLayout(
sidebarPanel(
fileInput("upload", "Upload new image", accept = c('image/png', 'image/jpeg')),
textInput("size", "Size", value = "500x500!"),
h4("Height"),
verbatimTextOutput("clientdataText"),
sliderInput("rotation", "Rotation", 0, 360, 0),
sliderInput("cropw", "Crop width", -1000, 1000, 1, 1),
sliderInput("croph", "Crop height", -1000, +1000, 1, 1)
),
mainPanel(
imageOutput("img" ,width = "100%", height = "100%", fill =FALSE)
)
)
)
server <- function(input, output, session) {
library(magick)
# Start with placeholder img
image <- image_read("https://raw.githubusercontent.com/ThinkR-open/collage/master/inst/tigrou/tigrou.jpg")
# When uploading new image
observeEvent(input$upload, {
if (length(input$upload$datapath))
image <<- image_convert(image_read(input$upload$datapath), "jpeg")
info <- image_info(image)
updateTextInput(session, "size", value = paste0(info$width, "x", info$height, "!"))
})
# A plot of fixed size
output$img <- renderImage({
# Numeric operators
tmpfile <- image %>%
image_resize(input$size) %>%
image_rotate(input$rotation) %>%
image_crop(geometry = geometry_point(input$cropw, input$croph), gravity = NULL, repage = TRUE) %>%
image_write(tempfile(fileext='jpg'), format = 'jpg')
# Return a list
list(src = tmpfile, contentType = "image/jpeg")
},deleteFile = TRUE)
# Store in a convenience variable
valueh<-reactive(session$clientData$output_img_height)
# Values from cdata returned as text
output$clientdataText <- renderText(
valueh()
)
}
shinyApp(ui, server)