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

时间:2019-05-10 14:30:38

标签: r image shiny shiny-server

下午好

我定制了一个闪亮的代码来生成此应用

 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渲染图,但是我不知道如何通过添加脚本来工作来编辑脚本

1 个答案:

答案 0 :(得分:0)

您的示例代码不是完全可复制的示例,因为软件包{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)