使用Shiny中的滑块更改图像大小

Nan*_*ncy 7 r slider shiny

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

问题:有一个错误说"basename错误(imageinfo $ src):预期的字符向量参数".我找不到任何直接回答这个问题的东西,我不知道还有什么可以尝试的.是否有一个问题,如何在server.R中将sliderInput用作输入$ slider?

我当前的进展:我的理由是在ui.R文件中设置滑块,然后将图像的宽度作为server.R文件中的输入.

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)
      )
    )
))
Run Code Online (Sandbox Code Playgroud)

server.R部分:

 shinyServer(function(input, output) {

  output$logo = renderImage({
  img(src = "mylogo.png", width = input$slider)
  })
})
Run Code Online (Sandbox Code Playgroud)

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

jdh*_*son 7

img(src = "mylogo.png", width = input$slider)只是返回HTML.你可以用renderUI而不是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))
    })
  }
  )
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述