【发布时间】:2017-05-30 08:09:15
【问题描述】:
当我将嵌入的闪亮应用程序插入到我的文档中时,如 Embedded Shiny Apps 所示,在 YAML 中使用“运行时:闪亮”并单击“运行文档”按钮,只有图像占位符图标。
但是当我删除闪亮的应用程序并从 YAML 中删除“运行时:闪亮”时,嵌入的图像在渲染后是可见的。
以下链接包含嵌入图像的主题,但没有解决我的问题 - 在这两种情况下,图像占位符图标仍然存在。
问题:
- 我应该对我的代码进行哪些更改才能获取图像?
- 还是与我最初选择的编码有关?
下面是我的代码示例,其中包含一个嵌入式闪亮应用程序——因此在必要时您只需复制和粘贴即可。闪亮的应用程序只是 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
#
#
```{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())
})
},
)
```
我希望我提供了足够的信息...但是如果缺少某些内容,请发表评论。我将编辑我的问题。
非常感谢!
【问题讨论】:
-
不确定但你不必在 Markdown 中定义 shinyapp,你可以直接调用 server/ui 元素为 renderPlot() 等。见rmarkdown.rstudio.com/authoring_shiny.html
-
@timfaber:感谢您使用
renderPlot()提供的提示。但是图像的占位符图标的问题仍然存在…… -
你必须使用闪亮的功能
renderImage()。在shiny.rstudio.com/articles/images.html 上查看预渲染图像但是现在出现了图像正确对齐的新问题...... -
你能用你的
renderImage()部分更新你的代码吗? -
@timfaber:我已经更新了我的代码。并查看新的两个问题——也许你知道如何解决这个问题。我正在尝试使用
fillPage()和fillRow()来抑制滚动的需要...
标签: r shiny embed r-markdown