Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在Shining中使用反应性条件渲染的困难_R_Shiny - Fatal编程技术网

R 在Shining中使用反应性条件渲染的困难

R 在Shining中使用反应性条件渲染的困难,r,shiny,R,Shiny,我在以下闪亮的应用程序中尝试有条件地显示图像,这让我大吃一惊 有两个问题 第一个问题是,如果我取消注释#img(src=“dollar.png”),我可以显示图片,但我无法以其他方式显示图像(尽管与图像关联的Alt文本确实会显示) 第二个问题是,我试图使if语句根据ne的值以反应方式工作,但没有成功 对于ui.R我有: library(shiny) shinyUI(fluidPage( # Application title titlePanel("My App that Won'

我在以下闪亮的应用程序中尝试有条件地显示图像,这让我大吃一惊

有两个问题

第一个问题是,如果我取消注释
#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")

    )
  )
))
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)
})
对于
server.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")

    )
  )
))
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)
})
库(闪亮)

net_effect我假设图片
dollar.png
dolar_red.png
位于文件夹
www
中,该文件夹与server.R和ui.R位于同一目录中

第二个问题是我试图使if语句起作用 基于ne值的反应性测试未成功

这是因为将字符串与0进行比较没有意义。我在服务器代码中做了一些更改,应该也解决了第一个问题

编辑:我忘记添加更改的
net\u效果
功能

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)
})

net\u effect Update:事实上,现在我尝试了一下,似乎文本和图像仍然没有显示/更新。它们在www上,如果我在
ui.R
中使用
img
作为测试,它们就会显示出来……好的,我知道了。我不得不将
www
添加到
src
路径中