EDIT: solved with magick - thank you guys a lot :) Updated version in the GitHub repo, for anyone who's interested in the story of the reindeer.
I'm quite new to R and just got to know R Shiny. Yet, I am trying to do a reindeer generator. Basic principle: Combine different layers of images in order to create your personal reindeer. E.g. switch coat colours with buttons while the outlines still stay in place on top. Ideally, it should look something like this in the end (a preview I sketched with GIMP):
preview of the reindeer generator
I manage to get the images into Shiny and switch between two coats with radio buttons, for a start.
However, I don't have a clue how to display the images at the same time at the very same location so the outlines will be an extra layer on top of the coat.
You can see the issue here:
grey coat selected
brown coat selected
Here is my code. Since the file name is partly generated by the radio button input, I left the paths like that.
library(shiny)
ui <- fluidPage(
titlePanel("R-eindeer"),
sidebarLayout(
sidebarPanel(
radioButtons("check1","coat colour", choices = c("grey","brown"))
),
mainPanel(
imageOutput("reindeer_coat"),
imageOutput("reindeer_outline")
)
)
)
server <- function(input,output){
getImage <- reactive({
list(src = paste0("./coat/reindeer_", input$check1, ".png"),
width = 500,
height = 500)
})
output$reindeer_coat <- renderImage({
getImage()
}, deleteFile = FALSE)
output$reindeer_outline <- renderImage({
return(list(src = "./outlines/reindeer_outline.png",
width = 500,
height = 500,
contentType = 'image/png'))
}, deleteFile = FALSE)
}
shinyApp(ui = ui, server = server)
I would appreciate any help. Even with that issue solved, there's still a long way to go - but maybe I can learn quick enough to get stuff done until Christmas ;-)
PS: You can find all the folders, image layers and additional information in the Git repo I just created. Also, even if you can't solve my question: feel free to use the images and pass on the Christmas spirit. Reindeer content should always be free. Link to GitHub repo
As mentioned in the comments, magick can do this for you! Specifically, image_mosaic will overlay images.
Try this out:
library(shiny)
library(magick)
ui <- fluidPage(
titlePanel("R-eindeer"),
sidebarLayout(
sidebarPanel(
radioButtons("check1","coat colour", choices = c("grey","brown"))
),
mainPanel(
imageOutput("reindeer")
)
)
)
server <- function(input,output){
get_image <- function(type, color) {
image_read(file.path(type, paste0(color, ".png")))
}
output$reindeer <- renderImage({
# load the images
coat <- get_image("coat", paste0("reindeer_", input$check1))
outline <- get_image("outlines", "reindeer_outline")
# make the reindeer: overlay in order
reindeer <- c(coat, outline)
# create a temp file
tmpfile <- reindeer %>%
image_mosaic() %>%
image_flatten() %>%
image_write(tempfile(fileext='jpg'), format = 'jpg')
# render the file
return(list(src = tmpfile,
height = 300,
width = 300,
alt = "Your reindeer",
contentType = "image/jpg"))
}, deleteFile = TRUE)
}
shinyApp(ui = ui, server = server)
Related
I've this piece of code which is generating me an QR code, from an input field, and than saving it as .svg.
observeEvent(input$safe_qr,{
qr <- qr_code(input$qr_gen)
generate_svg(qr, "QR_Code.svg",
size = 100,
foreground = "black",
background = "white",
show = interactive())
renderPlot(qr)
})
Now I don't really need to save it, I want to see it next to my input field on the page. I have no idea how put an image inside the page.
One thing I was trying was Plot(qr) then a new window opened in edge and showed me the saved QR code. But yeah that's not what I want.
Here's a working shiny app to allow arbitrary QR generate (of text), showing in both SVG and PNG.
library(shiny)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
textInput("text", label = NULL, placeholder = "(some phrase)"),
imageOutput("svgplot")
),
mainPanel(
plotOutput("plot")
)
))
server <- function(input, output, session) {
QR <- eventReactive(input$text, {
qrcode::qr_code(input$text)
})
output$svgplot <- renderImage({
txt <- isolate(input$text)
tf <- tempfile(fileext = ".svg")
qrcode::generate_svg(QR(), tf, size = 100, foreground = "black", background = "white", show = FALSE)
list(
src = normalizePath(tf),
contentType = "image/svg+xml",
width = 100, height = 100,
alt = paste("My QR code:", sQuote(txt, FALSE))
)
}, deleteFile = TRUE)
output$plot <- renderPlot({ plot(QR()) })
}
shinyApp(ui, server)
You don't need to show both, I thought I'd compare/contrast how to show them since they require different steps.
Key takeaways:
renderImage allows us to show an arbitrary image that the browser should support. The expression in this call must be a list with the attributes for the HTML img attribute, so here my list(...) is creating
<img src="path/to/tempfile.svg" contentType="image/svg+xml" width=100 height=100 alt="...">
I'm doing a two-step here: create the QR object as reactive data, and then anything that will use that will depend on QR() (my QR object). This way there will be fewer reactive-chain calls. This may not be strictly necessary if all you're doing is showing a single QR code, over to you.
shiny::renderImage requires the deleteFile= argument; if all you want to is show it, this is fine; if the user wants to right-click on the displayed SVG file and download it locally, it's still fine. In fact, since the "link" text is data:image/svg+xml;base64,PD94bWwgdmVy... and is a fairly long string (39K chars in one example), even if the temp file is deleted, this link remains unchanged and working.
Say I wanted to use a custom image or shapefile in an interactive environment (like R Shiny) such as this image of a paper airplane:
I would also be willing to draw the image myself in the program to allow for full control.
But the overall goal would be to allow a user to drag the edges of the image and the program could keep track of the size of the changes for each dimension (say wingspan of the paper airplane)
Would Shiny be a possibility here, or do I need to use another program? I would also want some statistics of the changes available to the user.
Does anyone have any similar examples of such a thing, or can point me in the right direction?
Like i wrote in the comment you could use the shinyjqui package and read the session info of the user.
A reproducible example can be found below:
library(shiny)
library(shinyjqui)
library(ggplot2)
server <- function(input, output, session){
global <- reactiveValues(width = 400, height = 400)
observe({
print(session)
if(!is.null(session$clientData[["output_plot1_height"]])){
global$height <- session$clientData[["output_plot1_height"]]
global$width <- session$clientData[["output_plot1_width"]]
}
})
output$plot1 <- renderImage({
outfile <- tempfile(fileext='.png')
png(outfile, width = global$width, height = global$height)
hist(rnorm(200))
dev.off()
list(src = outfile)
}, deleteFile = TRUE)
output$clientdataText <- renderText({
paste("height is ", global$height, "width is", global$width)
})
}
ui <- fluidPage(
verbatimTextOutput("clientdataText"),
jqui_resizabled(plotOutput("plot1"))
)
shinyApp(ui, server)
I am trying to reactively display an image from a list in an R shiny application.
I have many .tiff images stored in the "www" directory of my app. They follow the naming convention OHC_130.tiff, OHC_131.tiff, OHC_132.tiff, IHC_133.tiff, Deiter_134.tiff, OHC_135.tiff etc...
I also have a vector containing all of these names
ImgID_Vector <- as.vector(c("OHC_130", "OHC_131", "OHC_132", "IHC_133", "Deiter_134", "OHC_135")
I would like to make a selectable input dropdown list like this where a user can select an image and then click the submit button to make the image appear below. I have set this up for the ui.r but i am not sure how to make it work on the server side.
#ui.r
library(shiny)
library(shinydashboard)
dashboardBody(
tabItems(
tabItem(tabName = "dt",
h2("Select an image"),
fluidRow(
box(title="This is a searchable database of images", solidHeader = TRUE,status = "primary"),
selectInput("input$ImageIDVariable1", label = h4("Enter your image of interest"), choices = (ImgID_Vector), multiple = TRUE),
submitButton("Submit"),
imageOutput("ImageID_Image")
)
)
)
Conceptually I know that on the server side I need to connect the user input from the UI side to the actual image in the "www" folder. I should be able to do this using reactive inputs and renderImage I think. But I am not sure how to write the render image command to achieve the desired result.
#server.r
#This is the data that contains the choices for the dropdown menu
ImgID_Vector <- readRDS("ImgID_Vector.RDS")
shinyServer(function(input, output) {
# This is where I am struggling, with the render image command
output$ImageID_Image <- renderImage({
filename <- normalizePath(file.path('./www',
paste(input$ImageIDVariable1, '.tiff', sep='')))
list(src = filename)
}, deleteFile = FALSE)
}
#This is where I have the reactive input variable
ImageIDVariable1 <- reactive({input$ImageIDVariable1})
})
Thanks for your help!
Hi argument inputId from your selectInput is wrong, it should be "ImageIDVariable1", not input$ImageIDVariable1.
In ui.R :
selectInput(inputId = "ImageIDVariable1", label = h4("Enter your image of interest")
In server.R
input$ImageIDVariable1
Moreover :
You should use this in a script called global.R or at least in ui.R :
ImgID_Vector <- readRDS("ImgID_Vector.RDS")
And you should not use multiple = TRUE because renderImage can only render one image at a time.
And you should put a selected choice by default, if not renderImage will search an image which doesn't exist.
I have an app that shows population models with a dropdown menu to choose the species. A plot of that species' model will be rendered and now I want an image of the species next to that plot.
The plot will change when the dates or location is changed, but the image next to it should only react to the select species input.
I've written this code for it:
ui <- fluidPage(
# ... rest of the code
wellPanel(
fluidRow(
column(3, selectInput("populatie", label = h4("Populatie"),
choices = list("PERENBLADVLO" = "PER", "OORWORM" = "OOR", "FLUWEELMIJT" = "FLU"),
selected = "PER"),
imageOutput("img1")), # here is the image
column(9, plotOutput("plot2"))
)
)
)
server <- function(input, output){
# ... rest of the code
output$img1 <- renderImage({
if(input$populatie == "PER"){
img(height = 240, width = 300, src = "PBV.jpg")
}
else if(input$populatie == "OOR"){
img(height = 240, width = 300, src = "OW.jpg")
}
else if(input$populatie == "FLU"){
img(height = 240, width = 300, src = "FM.jpg")
}
})
# ... rest of the code
}
What I get is an error saying "invalid filename argument".
I can render the images on their own though without a selection.
Thoughts?
I know this is so long ago, but here's the reason in case someone else is looking at this later:
You would use ImageOutput() and renderImage() if you could read in your images in the ui function because they don't depend on user input.. otherwise (most of the time) if you're reading in the images in your server function and the image you read depends on user input you'd use uiOutput() and renderUI()
hope this makes sense!
I still don't quite understand why it's like this, but I found a solution:
It works fine when I use uiOutput() and renderUI({}) instead of imageOutput and renderImage({}).
The rest of the code is still the same.
I am using R shiny to build web applications, and some of them are leveraging the great leaflet features.
I would like to create a customed and advanced popup, but I do not know how to proceed.
You can see what I can do in the project I created for this post on github, or directly in shinyapp.io here
The more complex the popup is, the weirdest my code is, as I am sort of combining R and html in a strange way (see the way I define my custompopup'i' in server.R)..
Is there a better way to proceed? What are the good practices to build such popups? If I plan to display a chart depending on the marker being clicked, should I build them all in advance, or is that possible to build them 'on the fly'? How can I do that?
Many thanks in advance for your views on this, please do not hesitate to share your answer here or to directly change my github examples!
Regards
I guess this post still has some relevance. So here is my solution on how to add almost any possible interface output to leaflet popups.
We can achieve this doing the following steps:
Insert the popup UI element as character inside the leaflet standard popup field. As character means, it is no shiny.tag, but merely a normal div. E.g. the classic uiOutput("myID") becomes <div id="myID" class="shiny-html-output"><div>.
Popups are inserted to a special div, the leaflet-popup-pane. We add an EventListener to monitor if its content changes. (Note: If the popup disappears, that means all children of this div are removed, so this is no question of visibility, but of existence.)
When a child is appended, i.e. a popup is appearing, we bind all shiny inputs/outputs inside the popup. Thus, the lifeless uiOutput is filled with content like it's supposed to be. (One would've hoped that Shiny does this automatically, but it fails to register this output, since it is filled in by Leaflets backend.)
When the popup is deleted, Shiny also fails to unbind it. Thats problematic, if you open the popup once again, and throws an exception (duplicate ID). Once it is deleted from the document, it cannot be unbound anymore. So we basically clone the deleted element to a disposal-div where it can be unbound properly and then delete it for good.
I created a sample app that (I think) shows the full capabilities of this workaround and I hope it is designed easy enough, that anyone can adapt it. Most of this app is for show, so please forgive that it has irrelevant parts.
library(leaflet)
library(shiny)
runApp(
shinyApp(
ui = shinyUI(
fluidPage(
# Copy this part here for the Script and disposal-div
uiOutput("script"),
tags$div(id = "garbage"),
# End of copy.
leafletOutput("map"),
verbatimTextOutput("Showcase")
)
),
server = function(input, output, session){
# Just for Show
text <- NULL
makeReactiveBinding("text")
output$Showcase <- renderText({text})
output$popup1 <- renderUI({
actionButton("Go1", "Go1")
})
observeEvent(input$Go1, {
text <<- paste0(text, "\n", "Button 1 is fully reactive.")
})
output$popup2 <- renderUI({
actionButton("Go2", "Go2")
})
observeEvent(input$Go2, {
text <<- paste0(text, "\n", "Button 2 is fully reactive.")
})
output$popup3 <- renderUI({
actionButton("Go3", "Go3")
})
observeEvent(input$Go3, {
text <<- paste0(text, "\n", "Button 3 is fully reactive.")
})
# End: Just for show
# Copy this part.
output$script <- renderUI({
tags$script(HTML('
var target = document.querySelector(".leaflet-popup-pane");
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if(mutation.addedNodes.length > 0){
Shiny.bindAll(".leaflet-popup-content");
};
if(mutation.removedNodes.length > 0){
var popupNode = mutation.removedNodes[0].childNodes[1].childNodes[0].childNodes[0];
var garbageCan = document.getElementById("garbage");
garbageCan.appendChild(popupNode);
Shiny.unbindAll("#garbage");
garbageCan.innerHTML = "";
};
});
});
var config = {childList: true};
observer.observe(target, config);
'))
})
# End Copy
# Function is just to lighten code. But here you can see how to insert the popup.
popupMaker <- function(id){
as.character(uiOutput(id))
}
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(lat = c(10, 20, 30), lng = c(10, 20, 30), popup = lapply(paste0("popup", 1:3), popupMaker))
})
}
), launch.browser = TRUE
)
Note: One might wonder, why the Script is added from the server side. I encountered, that otherwise, adding the EventListener fails, because the Leaflet map is not initialized yet. I bet with some jQuery knowledge there is no need to do this trick.
Solving this has been a tough job, but I think it was worth the time, now that Leaflet maps got some extra utility. Have fun with this fix and please ask, if there are any questions about it!
The answer from K. Rohde is great, and the edit that #krlmlr mentioned should also be used.
I'd like to offer two small improvements over the code that K. Rohde provided (full credit still goes to K. Rohde for coming up with the hard stuff!). Here is the code, and the explanation of the changes will come after:
library(leaflet)
library(shiny)
ui <- fluidPage(
tags$div(id = "garbage"), # Copy this disposal-div
leafletOutput("map"),
div(id = "Showcase")
)
server <- function(input, output, session) {
# --- Just for Show ---
output$popup1 <- renderUI({
actionButton("Go1", "Go1")
})
observeEvent(input$Go1, {
insertUI("#Showcase", where = "beforeEnd",
div("Button 1 is fully reactive."))
})
output$popup2 <- renderUI({
actionButton("Go2", "Go2")
})
observeEvent(input$Go2, {
insertUI("#Showcase", where = "beforeEnd", div("Button 2 is fully reactive."))
})
output$popup3 <- renderUI({
actionButton("Go3", "Go3")
})
observeEvent(input$Go3, {
insertUI("#Showcase", where = "beforeEnd", div("Button 3 is fully reactive."))
})
# --- End: Just for show ---
# popupMaker is just to lighten code. But here you can see how to insert the popup.
popupMaker <- function(id) {
as.character(uiOutput(id))
}
output$map <- renderLeaflet({
input$aaa
leaflet() %>%
addTiles() %>%
addMarkers(lat = c(10, 20, 30),
lng = c(10, 20, 30),
popup = lapply(paste0("popup", 1:3), popupMaker)) %>%
# Copy this part - it initializes the popups after the map is initialized
htmlwidgets::onRender(
'function(el, x) {
var target = document.querySelector(".leaflet-popup-pane");
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if(mutation.addedNodes.length > 0){
Shiny.bindAll(".leaflet-popup-content");
}
if(mutation.removedNodes.length > 0){
var popupNode = mutation.removedNodes[0];
var garbageCan = document.getElementById("garbage");
garbageCan.appendChild(popupNode);
Shiny.unbindAll("#garbage");
garbageCan.innerHTML = "";
}
});
});
var config = {childList: true};
observer.observe(target, config);
}')
})
}
shinyApp(ui, server)
The two main changes:
The original code would only work if the leaflet map is initialized when the app first starts. But if the leaflet map is initialized later, or inside a tab that isn't initially visible, or if the map gets created dynamically (for example, because it uses some reactive value), then the popups code won't work. In order to fix this, the javasript code needs to be run in htmlwidgets:onRender() that gets called on the leaflet map, as you can see in the code above.
This isn't about leaflet, but more of a general good practice: I wouldn't use makeReactiveBinding() + <<- generally. In this case it's being used correctly, but it's easy for people to abuse <<- without understanding what it does so I prefer to stay away from it. An easy almost drop-in replacement for that can be to use text <- reactiveVal(), which would be a better approach in my opinion. But even better than that in this case is instead of using a reactive variable, it's simpler to just use insertUI() like I do above.