Как объединить renderDataTable и renderImage в блестящем приложении - PullRequest
0 голосов
/ 10 мая 2019

Добрый день

Я создал блестящий код для создания этого приложения

 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 голосов
/ 13 мая 2019

Ваш пример кода не является полностью воспроизводимым примером, потому что пакет {переполнение} недоступен. Тем не менее, я могу предложить вам следующий код.

  • Включите 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)  
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...