使用用户输入更改图像,有光泽,renderImage

时间:2017-08-21 16:11:09

标签: r shiny

我有一个应用程序,可以根据用户输入显示数据和图片。我的ui中有以下代码:

    selectInput("var", 
                  label = "Choose a penguin to display",
                  choices = c("Bowie", "Cookie",
                              "Mango", "Renesme"),
                  selected = "Bowie"),

      dateRangeInput("dates", 
                     "Date range",
                     start = "2017-06-16", 
                     end = as.character(Sys.Date())),

     imageOutput("img1")
      ),

    mainPanel(plotOutput("plot")
              )
  )
))

以下在我的服务器中:

 output$plot <- renderPlot({
      penguin <- switch(input$var, 
                        "Bowie" = filter(date_swim, penguin == 'Bowie'),
                        "Cookie" = filter(date_swim, penguin == 'Cookie'),
                        "Mango" = filter(date_swim, penguin == 'Mango'),
                        "Renesme" = filter(date_swim, penguin == 'Renesme'))
      getSwim(min = input$dates[1], 
              max = input$dates[2],
              p = penguin)
    })

    output$img1 <- renderImage({   #This is where the image is set 
      if(input$var == "Bowie"){            
        img(src = "Bowie.png", height = 240, width = 300)
      }                                        
      else if(input$var == "Cookie"){
        img(src = "Cookie.png", height = 240, width = 300)
      }
      else if(input$var == "Renesme"){
        img(src = "Renesme.png", height = 240, width = 300)
      }
      else if(input$var == "Mango"){
        img(src = "Mango.png", height = 240, width = 300)
    }
  })

})

当我跑步时,图像应该在哪里,我看到错误信息:

  

预期的字符参数向量。

1 个答案:

答案 0 :(得分:2)

我认为这在文档中有点误导,但对于RenderImage()您要使用list()函数而不是img()。我重新编写了你的​​server.R文件:

download.file("http://dehayf5mhw1h7.cloudfront.net/wp-content/uploads/sites/38/2016/01/18220900/Getty_011816_Bluepenguin.jpg",
              destfile = "Bowie.png")
download.file("http://3.bp.blogspot.com/_cBH6cWZr1IU/TUURNp7LADI/AAAAAAAABsY/76UhGhmxjzY/s640/penguin+cookies_0018.jpg",
              destfile = "Cookie.png")

function(input, output) {

  output$img1 <- renderImage({   #This is where the image is set 
    if(input$var == "Bowie"){            
      list(src = "Bowie.png", height = 240, width = 300)
    }                                        
    else if(input$var == "Cookie"){
      list(src = "Cookie.png", height = 240, width = 300)
    }
    else if(input$var == "Renesme"){
      list(src = "Renesme.png", height = 240, width = 300)
    }
    else if(input$var == "Mango"){
      list(src = "Mango.png", height = 240, width = 300)
    }
  })