【问题标题】:Output Headline with Image输出带有图像的标题
【发布时间】:2016-08-17 15:08:24
【问题描述】:

我只是在学习 R Shiny,并且一直在玩画廊中的各种示例。对于“图像输出”示例:

http://shiny.rstudio.com/gallery/image-output.html

我想知道当我选择链环或笑脸单选按钮时,如何能够包含图片标题。例如,选择“笑脸”后,我希望标题“笑脸”显示在图像下方。谢谢您的帮助。下面是上面链接中也包含的代码。 (为了清楚起见,其中一些被故意删除)。谢谢

library(png) # For writePNG function

shinyServer(function(input, output, session) {

# A temp file to save the output.
# This file will be automatically removed later by
# renderImage, because of the deleteFile=TRUE argument.
outfile <- tempfile(fileext = ".png")

# Generate the image and write it to file
x <- matrix(rep((0:(width-1))/(width-1), height), height,
            byrow = TRUE)
y <- matrix(rep((0:(height-1))/(height-1), width), height)
pic <- gauss2d(x, y, input$r)
writePNG(pic, target = outfile)

# Return a list containing information about the image
list(src = outfile,
     contentType = "image/png",
     width = width,
     height = height,
     alt = "This is alternate text")

}, deleteFile = TRUE)


# image2 sends pre-rendered images
output$image2 <- renderImage({
if (is.null(input$picture))
  return(NULL)

if (input$picture == "face") {
  return(list(
    src = "images/face.png",
    contentType = "image/png",
    alt = "Face"
  ))
} else if (input$picture == "chainring") {
  return(list(
    src = "images/chainring.jpg",
    filetype = "image/jpeg",
    alt = "This is a chainring"
  ))
}

}, deleteFile = FALSE)
 })







shinyUI(fluidPage(
titlePanel("Client data and query string example"),

fluidRow(
column(4, wellPanel(
  sliderInput("r", "Radius :", min = 0.05, max = 1,
              value = 0.2, step = 0.05),
  radioButtons("picture", "Picture:",
              c("chainring", "face"))
)),
column(4,
  imageOutput("image1", height = 300),
  imageOutput("image2")
)
)

【问题讨论】:

    标签: r image shiny output


    【解决方案1】:

    这有帮助吗?

    ui.R

        library(shiny)
        shinyUI(fluidPage(
          titlePanel("Image title"),
          sidebarLayout(
            sidebarPanel(
                    sliderInput("obs", "Number of observations:",
                                min = 0, max = 1000,  value = 500)
            ),
            mainPanel(
                    imageOutput("myImage"),
                    uiOutput("text")
    
            )
          )
        ))
    

    服务器.R

        library(shiny)
    
        shinyServer(function(input, output) {
    
                output$myImage <- renderImage({
                        outfile <- tempfile(fileext='.png')
    
                        png(outfile, width=400, height=300)
                        hist(rnorm(input$obs), main = "")
                        dev.off()
    
                        list(src = outfile,
                             contentType = 'image/png',
                             width = 400,
                             height = 300,
                             alt = "This is alternate text")
                }, deleteFile = TRUE)
                histogramTitle <- reactive({
                        paste("<h5>", input$obs, " observation used to create the histogram</h5>", sep = "")
                })
                output$text <- renderUI({   
                        HTML(as.character(histogramTitle()))
                })
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多