【问题标题】:R Shiny: render image from fileR Shiny:从文件中渲染图像
【发布时间】:2020-06-19 11:27:18
【问题描述】:

此代码应在倒计时完成后呈现 /www 目录中的图像 sing.jpg。这可以渲染普通文本但带有图像 - 正如我已经实现的那样 - 我收到以下错误:

参数 1(类型 'closure')不能被 'cat' 处理

这是为什么?

library(shiny)
library(shinyjs)

ui <- fluidPage(
  actionButton("do", "Click Me"),
  verbatimTextOutput("eventTimeRemaining")
)

server <- function(input, output, session) {

  EventTime <- reactiveVal()

  observeEvent(input$do,{
    EventTime(Sys.time() + 3)
  })

  output$eventTimeRemaining <- renderText({

    req(input$do)

    timeLeft <- round(difftime(EventTime(), Sys.time(), units='secs'))

    if(timeLeft > 0){
      invalidateLater(1000, session)
      msg <- timeLeft
    } else {

      msg <- renderImage({
        # Return a list containing the filename
        list(src = './sing.jpg')
    }, deleteFile = FALSE)

      }

    msg

  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我会在ui 中创建一个单独的输出,例如imageOutput 来显示您的图像。此外,我会将timeLeft 设为一个单独的reactive 表达式,eventTimeRemainingmsg 都可以引用它来显示倒计时(创建了一个单独的output)。

    我不确定您想如何处理初始条件 - 我将默认 EventTime 设置为 Sys.time() 但如果您使用 NULL 或不初始化,则需要检查 output 和 @ 中的 NULL 987654331@计算。

    library(shiny)
    library(shinyjs)
    
    ui <- fluidPage(
      actionButton("do", "Click Me"),
      verbatimTextOutput("eventTimeRemaining"),
      imageOutput("msg")
    )
    
    server <- function(input, output, session) {
    
      EventTime <- reactiveVal(Sys.time())
    
      observeEvent(input$do,{
        EventTime(Sys.time() + 3)
      })
    
      timeLeft <- reactive({
        tl <- round(difftime(EventTime(), Sys.time(), units='secs'))
        if (tl > 0) {
          invalidateLater(1000, session)
        } 
        tl
      })
    
      output$eventTimeRemaining <- renderText({
        timeLeft()
      })
    
      output$msg <- renderImage({
        if (timeLeft() > 0) {
          list(src = '')
        } else {
          list(src = './sing.jpg')
        }
      }, deleteFile = FALSE)
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 谢谢,但这并没有为我显示图像。它确实消除了错误。
    • @syntheso - 如果您删除if/else,它会在没有任何条件的情况下显示图像吗?如果没有,也许它无法找到/访问该文件?
    • 不,所以我想可能是这种情况,但没有错误。它在万维网。我尝试了一些替代方案,将继续。
    • 我没主意了。
    • 它现在可以工作了——我的工作目录设置不正确。已接受答案。
    猜你喜欢
    • 2021-11-04
    • 2020-11-08
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多