Search code examples
rimageshinyshiny-server

How to combine renderDataTable and renderImage in shiny app


Good afternoon

I have tailored a shiny code to generate this 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)  

Now I want to add this line of code to script to display an image in image option but I don't know how to add this code to the main script

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

The above code would render a plot from URL but I don't know how to edit my script by adding this to work Thank you for any suggestion


Solution

  • Your example code is not totally a reproducible example because package {overflow} is not available. However, I can propose you the following code.

    • Include the uiOutput in the UI where you want it to appear
    • The renderUI needs to be included in a if test to test if the input$dataset is set to 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)