Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change size of image with a slider in Shiny

Tags:

r

slider

shiny

Goal: Make an image change size in response to moving a slider in Shiny (RStudio). Think zoom-in-zoom-out effect.

Problem: There's an error saying "Error in basename(imageinfo$src) : a character vector argument expected". I can't find anything that directly answers the question and I'm not sure what else to try. Is it just a problem with how sliderInput is used as input$slider in server.R?

My current progress: My rational was to set up the slider in the ui.R file and then have the width of the image be the input in the server.R file.

The ui.R part:

shinyUI(fluidPage(
  titlePanel("Nancy's Brainstorming"),
  sidebarLayout(

sidebarPanel(
  h3(
    strong("What is this?", style = "font-si24pt")),
  p("This is a pilot project."),
  sliderInput("slider", 
              label = "", 
              min = 100, 
              max = 300, 
              value = 200),
   imageOutput("logo", width = 200)
      )
    )
))

The server.R part:

 shinyServer(function(input, output) {

  output$logo = renderImage({
  img(src = "mylogo.png", width = input$slider)
  })
})

Additional information: The image shows up just fine by itself when I use img(src = "mylogo.png", width = 200). Also, I'm doing this just to get a better feel for building Shiny apps.

like image 448
Nancy Avatar asked May 16 '14 18:05

Nancy


1 Answers

img(src = "mylogo.png", width = input$slider) is just returning html. You can use renderUI instead of renderImage.

library(shiny)
runApp(
  list(ui = fluidPage(
    titlePanel("Nancy's Brainstorming"),
    sidebarLayout(    
      sidebarPanel(
        h3(
          strong("What is this?", style = "font-si24pt")),
        p("This is a pilot project."),
        sliderInput("slider", label = "", min = 100, max = 300, value = 200),
        uiOutput('logo')
      ),
      mainPanel(
        plotOutput("distPlot")
      ) 
    )
  ),
  server = function(input, output, session) {
    output$logo <- renderUI({
      img(src = "http://i.stack.imgur.com/mTqXa.png", width = as.integer(input$slider))
    })
  }
  )
)

enter image description here

like image 104
jdharrison Avatar answered Sep 21 '22 00:09

jdharrison