R语言 闪亮给出空图

k4ymrczo  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(201)

设置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,它将生成一个空图,因此我认为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)
8hhllhi2

8hhllhi21#

正如在评论中指出的,代码有很多问题,但下面将根据选定的分类群生成图。我不太确定您在那里试图用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)

相关问题