【问题标题】:How to combine renderDataTable and renderImage in shiny app如何在闪亮的应用程序中结合 renderDataTable 和 renderImage
【发布时间】:2019-05-10 14:30:38
【问题描述】:

下午好

我已经定制了一个闪亮的代码来生成这个应用程序

 Tomour_005=read.table(text = "     Gene Mutation
 1    TP53        -
 2   ERBB2        -
 3  PIK3CA        -
 4    KRAS        -
 5     MET        -
 6   CCNE1        -
 7    CDK6        -
 8   FBXW7        -
 9   CCND3        -
 10 CDKN2A        *")

Tumour_005 = overflow::sorandf()
Tumour_005$race=Tomour_005$Gene
Tumour_005$gender=Tomour_005$Mutation
Tumour_005=Tumour_005[,1:2]
colnames(Tumour_005)=c("Gene","Mutation")

library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      radioButtons("viewdataradio","View data by:", choices = c("patient", "Image"), inline = TRUE, selected = "patient"),
      selectInput("dataset", "Choose patient:", 
                  choices = c("Tumour_005"))
    ),  
    mainPanel(
      DT::dataTableOutput("table") 
    )
  )
))



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

  observe({
    if(input$viewdataradio == "patient"){
      choices = c("Tumour_005")
      firstchoice = "Tumour_005"
      label = "Choose patient:"
    }else{
      choices = c("Image")
      firstchoice = "Image"
      label = "Choose Image:"
    }
    updateSelectInput(session, "dataset", label = label, choices = choices, selected = firstchoice)
  })

  data <- reactive({
    data = switch(input$dataset,
                  "Tumour_005" = Tumour_005,
                  "Image" = Image
    )

  })

  output$table <- DT::renderDataTable({
    datatable(data())

  })
})

shinyApp(ui=ui,server=server)  

现在我想将这行代码添加到脚本以在图像选项中显示图像,但我不知道如何将此代码添加到主脚本

ui <- fluidPage(
     h4("Embedded image"),
     uiOutput("img")
   )
server <- function(input, output, session) {
     output$img <- renderUI({
         tags$img(src = "https://user-images.githubusercontent.com/43682980/57538542-8c45da00-7340-11e9-81c8-2a023fb050eb.png")
       })
   }

上面的代码会从 URL 渲染一个图,但我不知道如何通过添加它来编辑我的脚本谢谢你的任何建议

【问题讨论】:

  • 您可以将图像添加到绘图中并使用渲染图。否则,您可以在激活选项时使用 JavaScript 显示隐藏的 html div。或者让 img html 标签出现的 renderUI。
  • 谢谢,我找到了从 URL 渲染图像的代码,但我不知道如何将它与主脚本结合起来

标签: r image shiny shiny-server


【解决方案1】:

您的示例代码并非完全可重现的示例,因为包 {overflow} 不可用。但是,我可以向您推荐以下代码。

  • uiOutput 包含在您希望它出现的用户界面中
  • renderUI 需要包含在if 测试中以测试input$dataset 是否设置为image
Tomour_005=read.table(text = "     Gene Mutation
 1    TP53        -
 2   ERBB2        -
 3  PIK3CA        -
 4    KRAS        -
 5     MET        -
 6   CCNE1        -
 7    CDK6        -
 8   FBXW7        -
 9   CCND3        -
 10 CDKN2A        *")

Tumour_005 = overflow::sorandf()
Tumour_005$race=Tomour_005$Gene
Tumour_005$gender=Tomour_005$Mutation
Tumour_005=Tumour_005[,1:2]
colnames(Tumour_005)=c("Gene","Mutation")

library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      radioButtons("viewdataradio","View data by:", choices = c("patient", "Image"), inline = TRUE, selected = "patient"),
      selectInput("dataset", "Choose patient:", 
                  choices = c("Tumour_005"))

    ),  
    mainPanel(
      DT::dataTableOutput("table") ,
      uiOutput("img")
    )
  )
))



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

  observe({
    if(input$viewdataradio == "patient"){
      choices = c("Tumour_005")
      firstchoice = "Tumour_005"
      label = "Choose patient:"
    }else{
      choices = c("Image")
      firstchoice = "Image"
      label = "Choose Image:"
    }
    updateSelectInput(session, "dataset", label = label, choices = choices, selected = firstchoice)
  })

  data <- reactive({
    data = switch(input$dataset,
                  "Tumour_005" = Tumour_005,
                  "Image" = Image
    )

  })

  output$table <- DT::renderDataTable({
    datatable(data())

  })

  observe({
    input$dataset
    isolate({
      if (input$dataset == "Image") {
        output$img <- renderUI({
          tags$img(src = "https://user-images.githubusercontent.com/43682980/57538542-8c45da00-7340-11e9-81c8-2a023fb050eb.png")
        })
      } else {
        output$img <- renderUI({NULL})
      }
    })
  })
})

shinyApp(ui=ui,server=server)  

【讨论】:

  • 如果这回答了您的问题,请接受它,以便其他帮助者知道他们不必花一些时间在上面。我没有看到版本,但您可以将tagList() 与您的两张图片一起使用,例如tagList(tags$img("first"), tags$img("second"))
  • 谢谢一百万,非常有帮助,让我免于困惑
猜你喜欢
  • 2016-12-29
  • 2021-11-27
  • 2019-05-03
  • 2017-09-10
  • 1970-01-01
  • 2018-05-02
  • 2020-09-18
  • 2017-05-05
  • 2015-02-18
相关资源
最近更新 更多