簡體   English   中英

R Shiny - 從動態選項卡列表中提取嵌套向量

[英]R shiny - Extract nested vector from dynamic tab list

實際上,我是第一次在一個閃亮的應用程序上工作。

基於上傳的 xlsx,閃亮的應用程序創建了一個名為 garnetRF() 的反應式輸出。 根據上傳的 xlsx 中不同命名樣本的數量,應用程序在 output$tabp 中創建動態選項卡。 在選項卡中,用戶可以選擇要為每個樣本顯示的顏色。 到現在為止還挺好。

現在,在 ouput$setting 中,我想用用戶選擇的顏色繪制結果。 因此,我的想法是通過選擇的顏色名稱 (cls.1) 重命名樣本級別 (cls.0)。 但是,當執行如下代碼時,print(cls.1) 會顯示空列表和包含顏色代碼的向量。 我猜這是因為 colorInput 嵌套在動態選項卡列表中。

有誰知道如何僅提取顏色矢量?

output$tabp <- renderUI({
    req(garnetRF())
    l = length(unique((garnetRF()[,"sample"])))
    
    myTabs <- lapply(1:l, function(i) {
      tabPanel(title = as.list(factor(unique(garnetRF()[,"sample"])))[[i]],
               colourInput(paste0("col_", i), NULL, paste0("FF000", i), showColour = "background")
      )
    })
    do.call(tabsetPanel, myTabs)
  })
  
  output$setting <- renderPlot({
    req(garnetRF())
    l2 = length(unique((garnetRF()[,"sample"])))
    cls.0 = factor(garnetRF()[,"sample"])
    cls.1 = sapply(1:l2, function(j){input[[paste0("col_", j)]]})
    print(cls.1)

結果:

[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[1] "#FF0001" "#FF0002" "#FF0003"

這是一個最低限度可重現的示例:

ui <- fluidPage(
  
  titlePanel("Example"),
  
  fluidRow(
    column(8,
           plotOutput("setting"),
    ),
    column(4,
           uiOutput("tabp")
    )
  )
)
server <- function(input, output) {
  
  garnetRF <- reactive({
    mydata = data.frame(
      "sample" = c("example1", "example2", "example3"),
      "A" = (c(30, 30, 30)),
      "B" = (c(40, 30, 20)),
      "C" = (c(30, 40, 50)))
    return(mydata)
  })
  
  output$tabp <- renderUI({
    req(garnetRF())
    l = length(unique((garnetRF()[,"sample"])))
    
    myTabs <- lapply(1:l, function(i) {
      tabPanel(title = factor(unique(garnetRF()[,"sample"]))[[i]],
               colourInput(paste0("col_", i), NULL, paste0("FF000", i), showColour = "background")
      )
    })
    do.call(tabsetPanel, myTabs)
  })
  
  output$setting <- renderPlot({
    req(garnetRF())
    l2 = length(unique((garnetRF()[,"sample"])))
    cls.0 = factor(garnetRF()[,"sample"])
    cls.1 = sapply(1:l2, function(j){input[[paste0("col_", j)]]})
    print(cls.1)
    
    #plot following
    })
}

定義cls.1時,您需要如下所示的req()

output$setting <- renderPlot({
    req(garnetRF())
    l2 = length(unique((garnetRF()[,"sample"])))
    cls.0 = factor(garnetRF()[,"sample"])
    cls.1 <- sapply(1:l2, function(j){req(input[[paste0("col_", j)]])})
    print(cls.1)
    
    #plot following
    output$plot1 <- renderPlot(plot(cars))
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM