using switch in reactive() shiny, and avoiding reupdating object

14 views
Skip to first unread message

benjamin lane

unread,
Apr 28, 2020, 1:51:26 PM4/28/20
to Shiny - Web Framework for R
I am quite new to shiny and reactive programming and can't get my head around how to do what I want. I have a simple app that reads an image from file and perform some transformation. It then plot the original versus the transformed image. I used a tabsetPanel() to modify the sliders the user can change depending on which transformation he chooses. The transformations work well. But I would like that the state of the corrected image stays from the previous transformation instead of starting from the original image each time we change input transformation.

Thank you for your help 

Here is the code of the app:

```library(shiny)
library(magick)

parameter_tabs <- tagList(
tags$style("#transform { display:none; }"),
tabsetPanel(id = "transform",
tabPanel("deskew",
sliderInput("threshold", "Threshold", min = 1, max = 1000, value = 200, step = 10)
),
tabPanel("resize",
numericInput("geometry", "% of resizing", value = 20, min = 1 , max = 100, step = 1)
),
tabPanel("trim",
numericInput("fuzz", "Color distance as equal", value = 40, min = 0, max = 100, step = 1)
),
tabPanel("modulate",
sliderInput("brightness", "Brightness", min = 0, max = 100, value = 100, step = 10),
sliderInput("saturation", "Saturation", min = 0, max = 100, value = 100, step = 10),
sliderInput("hue", "Hue", min = 0, max = 200, value = 100, step = 10)
),
tabPanel("reducenoise",
sliderInput("radius", "radius in pixel", min = 1, max = 1000, value = 1, step = 10),
),
tabPanel("contrast",
sliderInput("sharpen", "sharpen", min = 1, max = 100, value = 30, step = 10),
),
tabPanel("convolve",
selectInput("kernel", "kernel", selected = "Gaussian", choices = kernel_types()[2:38]),
numericInput("iteration", "iteration", value = 100, max = 10000) 
),
tabPanel("convertGrayscale",
checkboxInput("convertGrayscale", "Convert to Grayscale?", value = TRUE),
),
tabPanel("rotate",
sliderInput("degrees", "Angle for rotation", value = 0, min = -180, max = 180, step = 1),
)
)
)

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
fileInput("file_image","Upload an jpg to do OCR",accept = "jpg"),
fluidRow(
sidebarPanel(
selectInput("transform_image", "Image transformation", 
choices = c("deskew", "resize", "trim", "modulate", "reducenoise", "contrast", "convolve", "convertGrayscale", "rotate")),
parameter_tabs
),    
column(4,
plotOutput("image_plot", width = "100%", height = "400px")
),
column(4,
plotOutput("corrected_image_plot", width = "100%", height = "400px")
)
)
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
observeEvent(input$transform_image, {
updateTabsetPanel(session, "transform", selected = input$transform_image) 
})
correct_image <- reactive({ 
switch(input$transform_image,
deskew =  image_deskew(read_image(), threshold = input$threshold),
resize =  image_resize(read_image(), geometry = input$geometry),
trim =  image_trim(read_image(), fuzz = input$fuzz),
modulate =  image_modulate(read_image(), brightness = input$brightness, saturation = input$saturation, hue = input$hue),
reducenoise =  image_reducenoise(read_image(), radius = input$radius),
contrast =  image_contrast(read_image(), sharpen = input$sharpen),
convolve =  image_convolve(read_image(), kernel = input$kernel, iterations = 1),
convertGrayscale =  image_convert(read_image(), type = 'Grayscale'),
rotate =  image_rotate(read_image(), degrees = input$degrees)
)
}
)

read_image <- reactive({
req(input$file_image)
image_read(input$file_image$datapath)
})


output$image_plot <- renderPlot({
plot(read_image())
})
output$corrected_image_plot <- renderPlot({
plot(correct_image())
})
}

# Run the application 
shinyApp(ui = ui, server = server)```

Reply all
Reply to author
Forward
0 new messages