我正在试图在以下Shiny应用程序中有条件地显示图像。
有两个问题。
首先,如果我取消注释#img(src = "dollar.png")
我可以显示图片,但我无法显示图像(尽管显示与图像关联的Alt文本)。
第二个问题是我尝试使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)
})
答案 0 :(得分:2)
我认为图片dollar.png
和dolar_red.png
位于www
文件夹中,该文件夹与server.R和ui.r位于同一目录中。
第二个问题是我尝试使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)
})