【发布时间】:2015-09-08 19:11:43
【问题描述】:
我正在尝试在以下 Shiny 应用中有条件地显示图像。
有两个问题。
首先,如果我取消注释#img(src = "dollar.png"),我可以显示图片,否则我无法显示图像(尽管与图像关联的 Alt 文本会显示)。
第二个问题是我尝试根据ne 的值使if 语句反应性地工作没有成功。
对于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)
})
【问题讨论】: