提问人:Hack-R 提问时间:9/9/2015 更新时间:9/10/2015 访问量:1580
Shiny 中反应式条件 renderImage 的困难
Difficulty with reactive conditional renderImage in Shiny
问:
我试图在以下 Shiny 应用程序中有条件地显示图像。
有 2 个问题。
首先,虽然如果我取消注释,我可以显示图片,但我无法以其他方式显示图像(尽管与图像关联的 Alt 文本确实显示)。#img(src = "dollar.png")
第二个问题是,我试图根据 的值使语句被动地工作,但没有成功。if
ne
因为我有:ui.R
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("My App that Won't Show Pics!"),
sidebarLayout(
sidebarPanel(
# Simple integer interval
sliderInput("ns", "Hypothetical NS with Treatment:",
min=100, max=10000, value=4000),
sliderInput("RPS", "RPS:",
min = 0, max = 50000, value = 21594, step = 100),
# Revenue Delta
sliderInput("revDelta", "RPS Delta:",
min = -10000, max = 5000, value = -4900, step = 100),
# NS Lift
sliderInput("nsLift", "NS Lift (See PPT for example results):",
min = 0, max = .4, value = .18, step = .001)
),
# Show a table summarizing the values entered
mainPanel(
tableOutput("values"),
imageOutput("image1")
#img(src = "dollar.png")
)
)
))
因为我有:server.R
library(shiny)
net_effect <- function(ns, revDelta, nsLift, RPS) {
trBase <- (ns/(1 + nsLift)) * RPS
trOffer <- (ns * RPS) + (ns * revDelta)
netEffect <- round(trOffer - trBase)
return(paste("$",format(netEffect, big.mark=","),sep=""))
}
shinyServer(function(input, output) {
sliderValues <- reactive({
# Compose data frame
data.frame(
Name = c("New Students with Offer",
"Rev per Student", "Net Effect"),
Value = as.character(c(input$ns,
input$RPS,
ne <- net_effect(input$ns, input$revDelta, input$nsLift, input$RPS))),
stringsAsFactors=FALSE)
})
# Show the values using an HTML table
output$values <- renderTable({
sliderValues()
})
# image2 sends pre-rendered images
output$image1 <- renderImage({
if (net_effect(input$ns, input$revDelta, input$nsLift, input$RPS) < 0) {
return(list(
src = "images/red_dollar.png",
contentType = "image/png",
alt = "We're losing money!"
))
} else if (net_effect(input$ns, input$revDelta, input$nsLift, input$RPS) > 0) {
return(list(
src = "images/dollar.png",
filetype = "image/png",
alt = "We're making money!"
))
}
}, deleteFile = FALSE)
})
答:
2赞
Michal Majka
9/9/2015
#1
我假设图片和位于与服务器位于同一目录中的文件夹中。R 和 ui.r.dollar.png
dolar_red.png
www
第二个问题是我试图使 if 语句起作用 基于NE值的被动性一直没有成功。
这是因为您正在将字符串与 0 进行比较,这并不重要。我对服务器代码进行了一些更改,这些更改也应该可以解决第一个问题。
编辑:我忘了添加更改的功能net_effect
net_effect <- function(ns, revDelta, nsLift, RPS) {
trBase <- (ns/(1 + nsLift)) * RPS
trOffer <- (ns * RPS) + (ns * revDelta)
return(round(trOffer - trBase)) # new return value
}
shinyServer(function(input, output) {
net <- reactive({
net_effect(input$ns, input$revDelta, input$nsLift, input$RPS)
})
sliderValues <- reactive({
# Compose data frame
data.frame(
Name = c("New Students with Offer",
"Rev per Student", "Net Effect"),
Value = as.character(c(input$ns,
input$RPS,
ne <- paste("$",format( net(), big.mark=","),sep="") )),
stringsAsFactors=FALSE)
})
# Show the values using an HTML table
output$values <- renderTable({
sliderValues()
})
output$image1 <- renderImage({
if ( net() < 0) {
return(list(
src = "www/red_dollar.png",
contentType = "image/png",
alt = "We're losing money!"
))
} else if (net() > 0) {
return(list(
src = "www/dollar.png",
filetype = "image/png",
alt = "We're making money!"
))
}
}, deleteFile = FALSE)
})
评论
0赞
Hack-R
9/10/2015
更新:实际上,现在我尝试了,似乎文本和图像仍然没有显示/更新。它们在 www 中,如果我只是用作测试,它们就会显示......img
ui.R
0赞
Hack-R
9/10/2015
好的,我明白了。我不得不添加到路径中www
src
评论