display png file in shinyR - r

I am trying to display a png file using shiny. file is uploaded but not displayed properly. I have included both ui and server code
ui <- fluidPage(
titlePanel("Upload Slide Image"),
sidebarLayout(
sidebarPanel(fileInput("file1", "Choose png File", multiple = TRUE,
accept = c(".png")) ), # Input: Select a file ----
mainPanel(imageOutput("myImage"))
)
)
server <- function(input, output, session){
output$myImage <- renderImage({
outfile <- tempfile(fileext = '.png')
png(outfile, width = 400, height = 300) # Generate the PNG
dev.off()
list(src = outfile,contentType = 'image/png',width = 400, height = 300,
alt = "This is alternate text")
}, deleteFile = TRUE)
}
# Create Shiny app ----
shinyApp(ui, server)

You are not doing anything with input (as mentioned by #RolandASc). Rather you are generating a new png file in your server.
As a source, you need to add input$file1$datapath to use the file that has been uploaded using the UI, as mentioned in this answer.
ui <- fluidPage(
titlePanel("Upload Slide Image"),
sidebarLayout(
sidebarPanel(fileInput("file1", "Choose png File", multiple = TRUE,
accept = c(".png")) ), # Input: Select a file ----
mainPanel(imageOutput("myImage"))
)
)
server <- function(input, output, session){
observe({
if (is.null(input$file1)) return()
output$myImage <- renderImage({
## Following three lines CREATE a NEW image. You do not need them
#outfile <- tempfile(fileext = '.png')
#png(outfile, width = 400, height = 300) # Generate the PNG
#dev.off()
list(src = input$file1$datapath, contentType = 'image/png',width = 400, height = 300,
alt = "This is alternate text")
}, deleteFile = TRUE)
})
}
# Create Shiny app ----
shinyApp(ui, server)
EDIT: I have added a check in observe to cater for error when the app is first run.

Related

How To Upload Image to Shiny App From Dropdown

I am trying to upload images to my shiny app, but seem to be stuck on a basic step. The images are in my www directory. I am able to implement a drop down option, and would like the user to select an image (e.g, mouse.png) which would upload said image. However, the image itself is not uploading.
This is my code, does anyone have any ideas?
library(shiny)
#create a box function
my.box <- function(title, obj) {
box(
title = title,
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
plotOutput(obj, height = "300px")
)
}
# List of choices for selectInput
mylist <- list.files("~/APP/www/")
body <- dashboardBody(tableOutput("filtered_table"),
my.box("Table1", "Table1"))
#create dropbox
ui <- fluidPage(
#drop down box
selectInput(inputId ="gene",label = h3("Select an image from below"),choices = mylist),
#name of the plot.
mainPanel(plotOutput("image")) #NOT SURE WHAT TO PLACE HERE
)
#server function
server = shinyServer(function(input, output,session){
observeEvent(input$myFile, {
inFile <- input$myFile
if (is.null(inFile))
return()
file.copy(inFile$datapath, file.path("~/APP/www/", inFile$name) )
})
})
Following the example from the shiny tutorial, you can use renderImage/imageOutput. Note that I've adjusted the file paths a bit.
library(shiny)
library(shinydashboard)
#create a box function
my.box <- function(title, obj) {
box(
title = title,
status = "primary",
solidHeader = TRUE,
collapsible = TRUE,
plotOutput(obj, height = "300px")
)
}
# List of choices for selectInput
mylist <- list.files("./www")
body <- dashboardBody(tableOutput("filtered_table"),
my.box("Table1", "Table1"))
#create dropbox
ui <- fluidPage(
#drop down box
selectInput(inputId ="gene",label = h3("Select an image from below"),choices = mylist),
#name of the plot.
mainPanel(imageOutput("image"))
)
#server function
server = shinyServer(function(input, output,session){
output$image <- renderImage({
filename <- normalizePath(file.path('www',
input$gene))
list(src = filename)
}, deleteFile = FALSE)
})
shinyApp(ui, server)

Export DiagrammeR, data.tree to image (png) in Shiny

I'm trying to download a image from a Shiny App, this image is produced by a DiagrammeR object.
This is the code:
# Load packages
library(shinythemes)
library(DiagrammeR)
library(data.tree)
library(plotly)
library(shiny)
# Load data
data(acme)
# Define UI
ui <- fluidPage(theme = shinytheme("lumen"),
titlePanel("Paula trying II"),
sidebarLayout(
sidebarPanel(downloadButton(outputId = "dld_diagrama", label = "Download diagram")),
mainPanel(
grVizOutput("tree_plot", width = "100%", height = "760px")
)
)
)
# Define server function
server <- function(input, output) {
output$tree_plot <- renderGrViz({
plot(acme)
})
output$dld_diagrama <- downloadHandler(
filename = function(){
paste("diagram", "png", sep = ".")
},
content = function(file) {
plotly::export(tree_plot, file = "diagram.png")
}
)
}
# Create Shiny object
shinyApp(ui = ui, server = server)
This downloads (with errors) a .txt, obviously wrong. I'm trying to download a .png Also I've tried with appshot with no success.
Here is one solution among many using shiny, you could also bring back the export as png button
library(shinythemes)
library(DiagrammeR)
library(data.tree)
library(plotly)
library(shiny)
data(acme)
# Define UI
ui <- fluidPage(theme = shinytheme("lumen"),
titlePanel("Paula trying II"),
sidebarLayout(
sidebarPanel(downloadButton(outputId = "dld_diagrama", label = "Download diagram")),
mainPanel(
grVizOutput("tree_plot", width = "100%", height = "760px")
)
)
)
# Define server function
server <- function(input, output) {
input_plot <- reactive(plot(acme))
output$tree_plot <- renderGrViz({
input_plot()
})
output$dld_diagrama <- downloadHandler(
filename = function(){
paste("diagram", "html", sep = ".")
},
content = function(file) {
htmlwidgets::saveWidget(as_widget(input_plot()), file)
}
)
}
# Create Shiny object
shinyApp(ui = ui, server = server)
This works:
# Load packages
library(shinythemes)
library(DiagrammeR)
library(data.tree)
library(plotly)
library(shiny)
# Load data
data(acme)
# Define UI
ui <- fluidPage(theme = shinytheme("lumen"),
titlePanel("Paula trying II"),
sidebarLayout(
sidebarPanel(downloadButton(outputId = "dld_diagrama", label = "Download diagram")),
mainPanel(
grVizOutput("tree_plot", width = "100%", height = "760px")
)
)
)
# Define server function
server <- function(input, output) {
input_plot <- reactive(plot(acme))
output$tree_plot <- renderGrViz({
plot(acme)
})
output$dld_diagrama <- downloadHandler(
filename = function(){
paste("diagram", "png", sep = ".")
},
content = function(file) {
htmlwidgets::saveWidget(as_widget(input_plot()), "www/diagrama.html", selfcontained = FALSE)
webshot(url = "diagrama.html", delay = 5, file = file)
}
)
}
# Create Shiny object
shinyApp(ui = ui, server = server)

How can I browse and upload an image in a shiny application?

I want to create a shiny application which will the user the ability to browse and load an image and then display it. My question is whether this is supported by shiny.
#ui.r
pageWithSidebar(
headerPanel('Image Recognition'),
sidebarPanel(
fileInput("file1", "Choose Image",
accept = c(
".jpg")
))
,
mainPanel(
imageOutput("file1")
)
)
#server.r
library(shiny)
function(input, output, session) {
(shiny.maxRequestSize=30*1024^2)
output$myImage <- renderImage({
# A temp file to save the output.
# This file will be removed later by renderImage
file1 <- tempfile(fileext = '.png')
# Generate the PNG
png(file1, width = 400, height = 300)
dev.off()
# Return a list containing the filename
list(src = file1,
contentType = 'image/png',
width = 400,
height = 300,
alt = "This is alternate text")
}, deleteFile = TRUE)
}
Here is a solution using base64 encoding of the uploaded file.
library(shiny)
library(base64enc)
options(shiny.maxRequestSize = 30*1024^2)
ui <- fluidPage(
fileInput("upload", "Upload image", accept = "image/png"),
uiOutput("image")
)
server <- function(input, output){
base64 <- reactive({
inFile <- input[["upload"]]
if(!is.null(inFile)){
dataURI(file = inFile$datapath, mime = "image/png")
}
})
output[["image"]] <- renderUI({
if(!is.null(base64())){
tags$div(
tags$img(src= base64(), width="100%"),
style = "width: 400px;"
)
}
})
}
shinyApp(ui, server)

How to put entire image location in tags$img of shiny package?

The following code for tags$img is:
Working...when the image is stored in 'www' folder and src = "Rlogo.png"
Not working...when entire path of the image is given
I need to put the entire location in one of my shiny app where the app.R file will be run from command prompt. Please help thanks..
library(shiny)
ui <- fluidPage(
box(
tags$img(height = 100, width = 100,src = "Rlogo.png"),
tags$img(height = 100, width = 100,src = "E:/myApp/www/Rlogo.png")
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
use imageOutput instead of tags$img:
library(shiny)
ui <- fluidPage(
box(
tags$img(height = 100, width = 100,src = "Rlogo.png"),
imageOutput('image')
)
)
server <- function(input, output, session) {
output$image <- renderImage({
list(src = "E:/myApp/www/Rlogo.png",
alt = "This is alternate text"
)
}, deleteFile = TRUE)
}
shinyApp(ui, server)

renderImage NOT DISPLAYING - R Shiny (only alt text)

Using the code below, I am only getting the alt text to appear.
Any suggestions on what may be the problem?
From server.R:
output$face <- renderImage({
list(src = "http://www.clipartbest.com/cliparts/yco/GGE/ycoGGEacE.png",
filetype = "image/png",
alt = "YOU MUST BE KIDDING ME!")
}, deleteFile = FALSE)
From ui.R:
imageOutput("face")
Thanks,
Chad
Adding to the explanation of the problem - I am not just trying to display the image. Rather, I am trying to make it reactive - and display a different image, based on inputs... per the server.R code below:
output$imagegauge <- renderImage({
if (is.null(IRR_calc()))
return(NULL)
if (IRR_calc() > .085) {
return(list(
src = "http://www.i2symbol.com/images/abc-123/o/white_smiling_face_u263A_icon_256x256.png",
contentType = "image/png",
alt = "Smiley Face"
))
} else {
return(list(
src = "http://www.clipartbest.com/cliparts/yco/GGE/ycoGGEacE.png",
filetype = "image/png",
alt = "Sad Face"
))
}
}, deleteFile = FALSE)
Thanks again,
Chad
renderImage takes a file as src input rather then a url. You can just include this image directly using tags$img :
library(shiny)
runApp(list(
ui = fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
numericInput('n', 'Number of obs', 100),
numericInput('m', 'Select image (Happy (1) or Sad(2))', 1, min = 1, max = 2),
uiOutput('test')
),
mainPanel(
plotOutput('plot')
)
)
),
server = function(input, output) {
output$plot <- renderPlot({ hist(runif(input$n)) })
output$test <- renderUI({
images <- c("http://www.i2symbol.com/images/abc-123/o/white_smiling_face_u263A_icon_256x256.png"
, "http://www.clipartbest.com/cliparts/yco/GGE/ycoGGEacE.png")
tags$img(src= images[input$m])
})
}
))

Resources