使用 Shiny 中的滑块更改图像大小 [英] Change size of image with a slider in Shiny

查看:17
本文介绍了使用 Shiny 中的滑块更改图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标: 使图像改变大小以响应在 Shiny (RStudio) 中移动滑块.想想放大缩小效果.

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

问题: 出现错误提示basename(imageinfo$src) 中的错误:预期的字符向量参数".我找不到任何可以直接回答这个问题的东西,我不确定还有什么可以尝试的.是否只是sliderInput在server.R中用作input$slider的问题?

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?

我目前的进展:我的理性是在ui.R文件中设置滑块,然后将图像的宽度作为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.

ui.R 部分:

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)
      )
    )
))

server.R 部分:

The server.R part:

 shinyServer(function(input, output) {

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

附加信息:当我使用 img(src = "mylogo.png", width = 200) 时,图像本身显示得很好.另外,我这样做只是为了更好地构建 Shiny 应用程序.

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.

推荐答案

img(src = "mylogo.png", width = input$slider) 只是返回html.您可以使用 renderUI 代替 renderImage.

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))
    })
  }
  )
)

这篇关于使用 Shiny 中的滑块更改图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆