Shiny: How to combine renderDataTable and renderImage in shiny app

Created on 10 May 2019  路  4Comments  路  Source: rstudio/shiny

Good afternoon

I have tailored a shiny code to generate this app

Tumour_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        *")
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())

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

})

shinyApp(ui=ui,server=server)

But I am getting this error

Warning: Error in <reactive:data>: object 'Image' not found
  [No stack trace available]

Could you please help me?

Type

Most helpful comment

Hi, thanks for updating your example. I was able to run it and I think I can see what you're up against.

What you're trying to do is fundamentally difficult because it involves "overloading" the same set of inputs and outputs for different purposes. In one case you'd like the inputs to be related to a table; in the other case, to images.

To simplify your application I propose a two-tab layout, clearly separating the table and image functionalities. By using tabs instead of trying to use the same UI for both cases, you can avoid needing to use any update*() functions.

An example follows. I hope this is helpful. If it is, please don't hesitate to close this issue. If it isn't, you might consider asking your question in the Shiny category on Community.

Tumour_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        *")

ui <- shinyUI(navbarPage("Patients",
  tabPanel("Table",
    sidebarPanel(
      selectInput(
        "table_dataset",
        "Choose patient:",
        choices = c("Tumour_005")
      ) 
    ),
    mainPanel(DT::dataTableOutput("table"))
  ),
  tabPanel("Image",
    sidebarPanel(
      selectInput(
        "image_dataset",
        "Choose image:",
        choices = c(
          "Image" = "https://user-images.githubusercontent.com/43682980/57538542-8c45da00-7340-11e9-81c8-2a023fb050eb.png"
        )
      ) 
    ),
    mainPanel(
      uiOutput("image") 
    )
  )
))

server <- function(input, output) {

  # Related to displaying tables
  table_data <- reactive({
    switch(input$table_dataset, "Tumour_005" = Tumour_005)
  })
  output$table <- DT::renderDataTable({
    datatable(table_data())
  })

  # Related to displaying images
  output$image <- renderUI({
    tags$img(src = input$image_dataset)
  })

}

shinyApp(ui=ui,server=server)

All 4 comments

Hi, I'm sorry, but I wasn't able to run your example; I don't have the overflow package. Is it possible for you to provide a simpler and/or standalone example?

Maybe because I can't run the app, I also don't fully understand your question.

Are you asking about how to display an image when something in your app is clicked on?

Oh, and I forgot to mention, we recently posted an article about how to create the best kind of example: https://github.com/rstudio/shiny/wiki/Creating-a-Reproducible-Example

It might be helpful to you.

Thanks in advance for clarifying, hopefully we can give you a satisfying answer.

Thank you I edited my question, could you please have a look?

Tumour_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        *")
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())

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

})

shinyApp(ui=ui,server=server)

But I am getting this error

Warning: Error in <reactive:data>: object 'Image' not found
  [No stack trace available]

Hi, thanks for updating your example. I was able to run it and I think I can see what you're up against.

What you're trying to do is fundamentally difficult because it involves "overloading" the same set of inputs and outputs for different purposes. In one case you'd like the inputs to be related to a table; in the other case, to images.

To simplify your application I propose a two-tab layout, clearly separating the table and image functionalities. By using tabs instead of trying to use the same UI for both cases, you can avoid needing to use any update*() functions.

An example follows. I hope this is helpful. If it is, please don't hesitate to close this issue. If it isn't, you might consider asking your question in the Shiny category on Community.

Tumour_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        *")

ui <- shinyUI(navbarPage("Patients",
  tabPanel("Table",
    sidebarPanel(
      selectInput(
        "table_dataset",
        "Choose patient:",
        choices = c("Tumour_005")
      ) 
    ),
    mainPanel(DT::dataTableOutput("table"))
  ),
  tabPanel("Image",
    sidebarPanel(
      selectInput(
        "image_dataset",
        "Choose image:",
        choices = c(
          "Image" = "https://user-images.githubusercontent.com/43682980/57538542-8c45da00-7340-11e9-81c8-2a023fb050eb.png"
        )
      ) 
    ),
    mainPanel(
      uiOutput("image") 
    )
  )
))

server <- function(input, output) {

  # Related to displaying tables
  table_data <- reactive({
    switch(input$table_dataset, "Tumour_005" = Tumour_005)
  })
  output$table <- DT::renderDataTable({
    datatable(table_data())
  })

  # Related to displaying images
  output$image <- renderUI({
    tags$img(src = input$image_dataset)
  })

}

shinyApp(ui=ui,server=server)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Toniiiio picture Toniiiio  路  4Comments

snowman55 picture snowman55  路  5Comments

wch picture wch  路  3Comments

daattali picture daattali  路  3Comments

hlherrera picture hlherrera  路  5Comments