【问题标题】:Shiny: Create multiple tabs with separate inputs on eachShiny:创建多个选项卡,每个选项卡都有单独的输入
【发布时间】:2023-04-09 05:12:01
【问题描述】:

我正在构建一个带有多个选项卡的闪亮应用程序,每个选项卡都接受用户输入(唯一(data_in$cat),并生成某种类型的图形。问题出现在第二个选项卡中——出于某种原因,它没有生成由data2指定的图形。第一个选项卡上的第一个图形显示正确。运行此代码时我没有看到错误,所以我不知道从哪里开始调试!

library(shiny)
library(openxlsx)
library(ggplot2)


data_in <- read.xlsx("www/dem_data_clean.xlsx")



ui <- navbarPage(title = "Data",
                 tabPanel(title = "Over-all trends",
                          plotOutput("Histall"),
                          selectInput("Indall","Demographic Variable of Interest",choices = unique(data_in$cat))
                 ),
                 tabPanel(title = "2017-2018"),
                 plotOutput("Hist17"),
                 selectInput("Ind17","Demographic Variable of Interest",choices = unique(data_in$cat))
)






server <- function(input, output, session) {
  
  data1 <- reactive({
    a <- subset(data_in,cat==input$Indall)
    return(a)
  })
  
  data2 <- reactive({
    a <- subset(data_in,cat==input$Ind17)
    return(a)
  })
  
  
  output$Histall <- renderPlot({
    ggplot(data1(), aes(x=Year,y=value, group =name, color=name)) + geom_line(stat = "identity") +
      ylab("Percent of Population") 

    
  })
  
  output$Hist17 <- renderPlot({
    data2() %>%
      filter(Year=="2017-18") %>%
      ggplot(aes(name, value)) + geom_bar(stat = "identity")

    
  })
  
  
  
  
}

shinyApp(ui, server)

对我做错了什么有什么建议吗?我已经尝试玩不同的东西几个小时了,但无济于事!

【问题讨论】:

  • 看来plotOutput("Hist17")selectInput("Ind17", ...) 在第二个tabPanel(title = "2017-2018") 的括号之外。

标签: r shiny


【解决方案1】:

UI 代码不正确,第二个plotOutputselectInput 不在第二个tabPanel 内。如果你修复它,它会起作用:

ui <- navbarPage(title = "Data",
                 tabPanel(title = "Over-all trends",
                          plotOutput("Histall"),
                          selectInput("Indall",
                                      "Demographic Variable of Interest",
                                      choices = unique(data_in$cat))
                 ),
                 tabPanel(title = "2017-2018",
                          plotOutput("Hist17"),
                          selectInput("Ind17",
                                      "Demographic Variable of Interest",
                                      choices = unique(data_in$cat)))
)

【讨论】:

    猜你喜欢
    • 2014-01-27
    • 2015-11-12
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多