如何在闪亮的应用程序中结合 renderDataTable 和 renderImage

How to combine renderDataTable and renderImage in shiny app

下午好

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

 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 的情节,但我不知道如何通过添加它来编辑我的脚本,谢谢你的任何建议

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

  • uiOutput 包含在 UI 您希望它出现的位置
  • 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)