在文档和嵌入式闪亮应用中呈现图像

时间:2017-05-30 08:09:16

标签: r shiny embed r-markdown

当我在我的文档中插入嵌入式闪亮应用程序时,如Embedded Shiny Apps所示,在YAML中使用“runtime:shiny”并单击“运行文档”按钮,只有图像占位符图标。

但是当我删除闪亮的应用程序并从YAML中移除“runtime:shiny”时,嵌入的图像在渲染后可见。

以下链接有嵌入图像的主题,但没有解决我的问题 - 在这两种情况下,图像占位符图标仍然存在。

问题:

  • 我的代码应该更改以获取图像?
  • 或者是否与我最初的编码选择有关?

在我的代码示例下面有一个嵌入式闪亮应用程序 - 所以在必要时你只需要复制和粘贴。闪亮的应用程序只是r工作室画廊的副本......

编辑:正如timfaber所建议,我在代码中添加了renderImage()部分。但是有两个问题,即渲染仍然存在。

如何禁止向上或向下滚动以查看整个图像?如何在闪亮的应用程序中定位图像?

---
title: "Documentation"
author: "tueftla"
date: "23 Mai 2017"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
```

Here is my documentation …  
and also one of the images.  


# my old version
#![](image1.png)
# 
```{r, echo=FALSE}
    # Here you have to scroll up or down to see the entire image
    shinyApp(

         ui = fluidPage(
                imageOutput("image1")
         ),

         server = function(input, output) {
                 output$image1=renderImage({
         # the images are stored in a subdirectory named images
         filename <- normalizePath(file.path('./images',
                              paste('image1', '.png', sep='')))

         # Return a list containing the filename
         list(src = filename, height = 600,width=800)
         }, deleteFile = FALSE)

         }


     )

```

在第二个代码序列中,我想将图像放在右侧。请参阅评论“旧版本”

```{r, echo = FALSE}

shinyApp(

  ui = fluidPage(

    # Application title
    titlePanel("Tabsets"),

    # my old version
    #img(src=image2.png', align = "right"),
    # my new version with bad alignment - note also the change in server
    imageOutput("image2", height = 200,width=100),


    # Sidebar with controls to select the random distribution type
    # and number of observations to generate. Note the use of the
    # br() element to introduce extra vertical spacing
    sidebarLayout(
      sidebarPanel(
        radioButtons("dist", "Distribution type:",
                     c("Normal" = "norm",
                       "Uniform" = "unif",
                       "Log-normal" = "lnorm",
                       "Exponential" = "exp")),
        br(),

        sliderInput("n", 
                    "Number of observations:", 
                     value = 500,
                     min = 1, 
                     max = 1000)
      ),

      # Show a tabset that includes a plot, summary, and table view
      # of the generated distribution
      mainPanel(
        tabsetPanel(type = "tabs", 
          tabPanel("Plot", plotOutput("plot")), 
          tabPanel("Summary", verbatimTextOutput("summary")), 
          tabPanel("Table", tableOutput("table"))
        )
      )
    )
  ),

  server = function(input, output) {

      # the image rendering - necessary for the image in ui of this app
      output$image2=renderImage({
      # the images are stored in a subdirectory named images
      filename <- normalizePath(file.path('./images',
                                paste('image2', '.png', sep='')))

      # Return a list containing the filename
      list(src = filename, height = 200,width=100)
      }, deleteFile = FALSE)

      # Reactive expression to generate the requested distribution.
      # This is called whenever the inputs change. The output
      # functions defined below then all use the value computed from
      # this expression
      data <- reactive({
        dist <- switch(input$dist,
                       norm = rnorm,
                       unif = runif,
                       lnorm = rlnorm,
                       exp = rexp,
                       rnorm)

        dist(input$n)
      })

      # Generate a plot of the data. Also uses the inputs to build
      # the plot label. Note that the dependencies on both the inputs
      # and the data reactive expression are both tracked, and
      # all expressions are called in the sequence implied by the
      # dependency graph
      output$plot <- renderPlot({
        dist <- input$dist
        n <- input$n

        hist(data(), 
             main=paste('r', dist, '(', n, ')', sep=''))
      })

      # Generate a summary of the data
      output$summary <- renderPrint({
        summary(data())
      })

      # Generate an HTML table view of the data
      output$table <- renderTable({
        data.frame(x=data())
      })

  },


)
```

我希望我提供了足够的信息......但是当缺少某些内容时请发表评论。我会编辑我的问题。

非常感谢提前!

第二次修改:以下是显示我的文件夹结构和结果。Folder structure and result

1 个答案:

答案 0 :(得分:1)

我认为它可以轻松完成。对我来说,问题是为图像定义正确的路径。无需使用renderImage!缩放图像可以解决滚动问题,使用img可以定义位置(对齐):

---
title: "Documentation"
author: "tueftla"
date: "23 Mai 2017"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
```

Here is my documentation …  
and also one of the images.  


```{r, echo = FALSE}

fluidPage(

titlePanel("Tabsets"),

img(src='www/logotitle.jpg', align = "right",width=100,height=100),
# make sure you define the right (full) path

sidebarLayout(
  sidebarPanel(
    radioButtons("dist", "Distribution type:",
                 c("Normal" = "norm",
                   "Uniform" = "unif",
                   "Log-normal" = "lnorm",
                   "Exponential" = "exp")),
    br(),

    sliderInput("n", 
                "Number of observations:", 
                 value = 500,
                 min = 1, 
                 max = 1000)
  ),


  mainPanel(
    tabsetPanel(type = "tabs", 
      tabPanel("Plot", plotOutput("plot")), 
      tabPanel("Summary", verbatimTextOutput("summary")), 
      tabPanel("Table", tableOutput("table"))
    )
  )
))

data <- reactive({
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)

    dist(input$n)
  })

  # Generate a plot of the data. Also uses the inputs to build
  # the plot label. Note that the dependencies on both the inputs
  # and the data reactive expression are both tracked, and
  # all expressions are called in the sequence implied by the
  # dependency graph
  output$plot <- renderPlot({
    dist <- input$dist
    n <- input$n

    hist(data(), 
         main=paste('r', dist, '(', n, ')', sep=''))
  })

  # Generate a summary of the data
  output$summary <- renderPrint({
    summary(data())
  })

  # Generate an HTML table view of the data
  output$table <- renderTable({
    data.frame(x=data())
  })
```

您可以删除renderImage部分和所有ui / server函数(如前所述),只需保留渲染函数和制表集即可。我的结果:

to initialize lazy associations and when to use them