繁体   English   中英

Shiny 给空 plot

[英]Shiny gives empty plot

设置 shiny 有点费力。不确定错误发生在何处,请参阅以下信息:

得到以下与 facet_grid 相关的错误:

Warning: Error in combine_vars: At least one layer must contain all faceting variables: `input$Cancer`
✖ Plot is missing `input$Cancer`
✖ Layer 1 is missing `input$Cancer`

但是,如果我删除 facet_grid,它会生成一个空的 plot,所以我认为selectInputdata_selectedggplot有问题

示例数据:

 files.Vir.DNA.df.test <- structure(list(ID = c("NC_010277.2", "NC_010277.2", "NC_010277.2", 
"NC_010277.2", "NC_010277.2", "NC_010277.2", "NC_010277.2", "NC_010277.2", 
"NC_010277.2", "NC_010277.2", "NC_010277.2", "NC_010277.2", "NC_010277.2", 
"NC_010277.2", "NC_010277.2", "NC_010277.2", "NC_010277.2", "NC_010277.2", 
"NC_010277.2", "NC_010277.2"), rowSums = c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
    Cancer = c("Adrenal gland", "Adrenal gland", "Adrenal gland", 
    "Adrenal gland", "Adrenal gland", "Adrenal gland", "Adrenal gland", 
    "Adrenal gland", "Adrenal gland", "Adrenal gland", "Adrenal gland", 
    "Adrenal gland", "Adrenal gland", "Adrenal gland", "Adrenal gland", 
    "Adrenal gland", "Adrenal gland", "Adrenal gland", "Adrenal gland", 
    "Adrenal gland"), position = c(1000, 1001, 1002, 1003, 1004, 
    1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013, 1014, 
    1015, 1016, 1017, 1018, 1019), V1 = c("Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b", "Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b", "Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b", "Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b", "Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b", "Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b", "Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b", "Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b", "Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b", "Merkel_cell_polyomavirus_isolate_R17b", 
    "Merkel_cell_polyomavirus_isolate_R17b"), Length = c(5387L, 
    5387L, 5387L, 5387L, 5387L, 5387L, 5387L, 5387L, 5387L, 5387L, 
    5387L, 5387L, 5387L, 5387L, 5387L, 5387L, 5387L, 5387L, 5387L, 
    5387L)), row.names = c("1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19", "20"), class = "data.frame")

用户界面:

ui <- dashboardPage(
  # Application title
  dashboardHeader(title=h4(HTML("Virus Coverage plot"))),
  dashboardSidebar(
    useShinyjs(),
    
    selectInput("Taxa", "Taxa", choices = unique(files.Vir.DNA.df.test$V1)),
    shinyjs::hidden(selectInput("Taxa", "Taxa", choices = c("Taxa")))
  ),
  
  dashboardBody(
    tabsetPanel(
      tabPanel("Taxa", plotOutput("myplot1", width = "400px", height = "300px"))
      
    )
  )
)

服务器:

server <- function(input, output, session) {
  
  data_selected <- reactive({
    filter(files.Vir.DNA.df.test, V1 %in% input$V1)
  })
  
  output$myplot1 <- renderPlot({
    ggplot(data_selected(), aes_string(input$V1, "position", fill = input$V1)) + 
        scale_y_log10(breaks = c(1,100,10000)) +
        theme_classic(base_size = 6) +
        geom_bar(stat="identity") +
        facet_grid(Cancer~. , scales = "free_x", space = "free_x", switch = "x") 

  })
}

shinyApp(ui, server)

正如评论中指出的那样,代码存在很多问题,但下面将根据选定的分类单元生成图表。 我不太确定你想用shinyjs::hidden做什么。

ui <- dashboardPage(
  # Application title
  dashboardHeader(title=h4(HTML("Virus Coverage plot"))),
  dashboardSidebar(
    
    selectInput("Taxa", "Taxa", choices = unique(files.Vir.DNA.df.test$V1))
  ),
  
  dashboardBody(
    tabsetPanel(
      tabPanel("Taxa", 
               plotOutput("myplot1", width = "400px", height = "300px"))
      
    )
  )
)
server <- function(input, output, session) {
  
  data_selected <- reactive({
    filter(files.Vir.DNA.df.test, V1 %in% input$Taxa)
  })
  
  output$myplot1 <- renderPlot({
    ggplot(data_selected(), aes(V1, position, fill = V1)) + 
      geom_bar(stat="identity") +
      scale_y_log10(breaks = c(1,100,10000)) +
      theme_classic(base_size = 6) +
      facet_grid(Cancer~. , scales = "free_x", space = "free_x", switch = "x")
    
    
  })
}

shinyApp(ui, server)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM