【问题标题】:Image output in shiny app闪亮应用程序中的图像输出
【发布时间】:2018-11-09 07:43:59
【问题描述】:

我正在尝试在闪亮的应用程序中包含图像。我想要:“如果是类型 1 绘制“此图像”,如果是类型 0 绘制“其他图像”。我知道我必须将 jpg 文件放入 app.R 所在的文件夹中,然后调用它但我不知道怎么做。

这是我迄今为止使用的代码(它有效),我只需要将图像包含在渲染中。

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Myapp"),

  #Inputs
  dateInput(inputId = "dob", label="Birth"),
  dateInput(inputId = "ad", label="Date"),
  actionButton("Submit", icon("fas fa-magic"), label="Submit"),


  #Outputs
  textOutput(outputId = "textR"),
  imageOutput(outputId = "imageR1"),
  imageOutput(outputId="imageR2")
)



# Define server logic required to draw a histogram
server <- function(input, output) {
  #my output should be named textR and imageR1, imageR2

  observeEvent(input$Submit, 
           output$textR<-renderText({
             v1<-as.numeric(as.Date(input$ad,format="%Y/%m/%d") - as.Date(input$dob, format="%Y/%m/%d"))/30.5
             value_v1<-ifelse(v1>48, "type1", "type2")
             print(value_v1)
           }))

}

# Run the application 
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny shiny-server


    【解决方案1】:

    observeEvent 中定义输出对象是不好的做法。在这种情况下,独立于如何切换图像的选择,我建议使用eventReactive - 我们称之为myval。这会创建一个只有在某个事件发生时才会发生变化的反应,在这种情况下是单击提交按钮。然后我们可以在renderText 语句的主体中引用它,这样就可以简单地变成:

      output$textR<-renderText({
        print(myval())
      })
    

    其次,图片的输出,应该放在www目录下,见here。然后我们可以使用renderUIUIOutput 创建一个ui 元素,其中我们使用eventReactive myval() 的值来选择要显示的图像。

    下面给出了一个工作示例。请注意,我将其保存为 app.R,并使用了引用链接的文件夹结构,因此:

    | shinyApp/
        | app.R
        | www/
           | zorro.jpg
           | notzorro.jpg
    

    希望这会有所帮助!


    library(shiny)
    
    # Define UI for application that draws a histogram
    ui <- fluidPage(
    
      # Application title
      titlePanel("Myapp"),
    
      #Inputs
      dateInput(inputId = "dob", label="Birth"),
      dateInput(inputId = "ad", label="Date"),
      actionButton("Submit", icon("fas fa-magic"), label="Submit"),
    
      #Outputs
      textOutput(outputId = "textR"),
      uiOutput(outputId = "my_ui")
    )
    
    
    
    # Define server logic required to draw a histogram
    server <- function(input, output) {
    
        myval <- eventReactive(input$Submit,
                             {
                               v1<-as.numeric(as.Date(input$ad,format="%Y/%m/%d") - as.Date(input$dob, format="%Y/%m/%d"))/30.5
                               return(ifelse(v1>48, "type1", "type2"))
                             })
    
      output$textR<-renderText({
        print(myval())
      })
    
      output$my_ui<-renderUI({
        if(myval()=='type1')
          img(src='zorro.jpg', height = '300px')
        else
          img(src='notzorro.jpg', height = '300px')
      })
    
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 一个全面的答案,包含工作代码和输出的动画 gif,所有这些都在提出问题的 20 分钟内完成。太棒了!
    猜你喜欢
    • 2014-03-26
    • 2019-03-24
    • 2018-01-06
    • 2016-12-12
    • 2017-11-18
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    相关资源
    最近更新 更多