当在闪亮的应用中使用绘图时,用户界面中的复选框消失了

发布于 2025-02-03 05:14:00 字数 2593 浏览 3 评论 0原文

我是闪亮和情节的新手。我正在尝试创建一个带有几页的闪亮应用程序,其中一个我想显示一个带有复选框的交互式散点图,以选择由我的变量“性别”过滤的数据。

我毫无疑问地使用常规GGPLOT函数运行代码,但是当我尝试使用Plotly进行代码时,复选框在用户界面中消失。

这是我的代码,我的问题在第2页中,其余的按预期工作:

library(shiny)
library(tidyverse)
library(plotly)
library(DT)

#####Import Data

dat<-read_csv(url("https://www.dropbox.com/s/uhfstf6g36ghxwp/cces_sample_coursera.csv?raw=1"))
dat<- dat %>% select(c("pid7","ideo5","newsint","gender","educ","CC18_308a","region"))
dat<-drop_na(dat)
dat$gender1<-as.numeric(dat$gender)


#####App

ui <- navbarPage("My application",
                 
    tabPanel("Page 1",
        sidebarLayout(
             sidebarPanel(
                  sliderInput("ideo5",
                  "Select Five Point Ideology (1=Very liberal, 5=Very conservative)",
                  min = 1,
                  max = 5,
                  value = 3)
             
    ),
    

    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Tab 1", plotOutput("bar1")),
                  tabPanel("Tab2", plotOutput("bar2"))
                  )
      )
    )
  ),
    
    
tabPanel("Page 2",
         sidebarLayout(
           sidebarPanel(
              checkboxGroupInput("gender", label = "Select gender", 
                   choices = list("1" = 1, "2" = 2),
                   selected = 1)
           ),
plotlyOutput("scatter")
    )
  ),




tabPanel("Page 3",
         selectInput("region", "Select region",
                     multiple=TRUE,
                     choices=c(1,2,3,4)),
         
         dataTableOutput("table", height=500)
),
 

)



server<-function(input,output){
    output$bar1 <- renderPlot({
      ggplot(
        plot_dat<-filter(dat,ideo5 %in% input$ideo5),
        aes(x=pid7))+
        geom_bar()+
        xlab("7 Point Party ID, 1 = Very D, 7 = Very R")+
        ylab("Count")+
        scale_y_continuous(breaks=seq(0, 125, 25), limits=c(0, 125))
      
    })
    
    output$bar2 <- renderPlot({
      ggplot(
        plot_dat<-filter(dat, ideo5 %in% input$ideo5),
        aes(x=CC18_308a))+
        geom_bar()+
        xlab("Trump support")+
        ylab("count")+
        scale_y_continuous(breaks=seq(0, 150, 50), limits=c(0, 150))
    })
    
    output$scatter<-renderPlotly({
      
      ggplot(filter(dat,gender %in% input$gender),aes(x=educ,y=pid7))+
        geom_jitter()+
        geom_smooth(method = "lm")+
        geom_point()
      })
    
    output$table <- renderDataTable({
      filter(dat,region %in% input$region)})
    
    
  }
  
shinyApp(ui,server)

谢谢!

I am new to Shiny and Plotly. I'm trying to create a Shiny app with several pages, in one of them I'd like to show an interactive scatter plot with a checkbox to select the data filtered by my variable "gender".

I have no problem running the code with a regular ggplot function, but when I try to do it with plotly, the checkbox disappears in the user interface.

This is my code, my problem is in Page 2, the rest works as intended:

library(shiny)
library(tidyverse)
library(plotly)
library(DT)

#####Import Data

dat<-read_csv(url("https://www.dropbox.com/s/uhfstf6g36ghxwp/cces_sample_coursera.csv?raw=1"))
dat<- dat %>% select(c("pid7","ideo5","newsint","gender","educ","CC18_308a","region"))
dat<-drop_na(dat)
dat$gender1<-as.numeric(dat$gender)


#####App

ui <- navbarPage("My application",
                 
    tabPanel("Page 1",
        sidebarLayout(
             sidebarPanel(
                  sliderInput("ideo5",
                  "Select Five Point Ideology (1=Very liberal, 5=Very conservative)",
                  min = 1,
                  max = 5,
                  value = 3)
             
    ),
    

    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Tab 1", plotOutput("bar1")),
                  tabPanel("Tab2", plotOutput("bar2"))
                  )
      )
    )
  ),
    
    
tabPanel("Page 2",
         sidebarLayout(
           sidebarPanel(
              checkboxGroupInput("gender", label = "Select gender", 
                   choices = list("1" = 1, "2" = 2),
                   selected = 1)
           ),
plotlyOutput("scatter")
    )
  ),




tabPanel("Page 3",
         selectInput("region", "Select region",
                     multiple=TRUE,
                     choices=c(1,2,3,4)),
         
         dataTableOutput("table", height=500)
),
 

)



server<-function(input,output){
    output$bar1 <- renderPlot({
      ggplot(
        plot_dat<-filter(dat,ideo5 %in% input$ideo5),
        aes(x=pid7))+
        geom_bar()+
        xlab("7 Point Party ID, 1 = Very D, 7 = Very R")+
        ylab("Count")+
        scale_y_continuous(breaks=seq(0, 125, 25), limits=c(0, 125))
      
    })
    
    output$bar2 <- renderPlot({
      ggplot(
        plot_dat<-filter(dat, ideo5 %in% input$ideo5),
        aes(x=CC18_308a))+
        geom_bar()+
        xlab("Trump support")+
        ylab("count")+
        scale_y_continuous(breaks=seq(0, 150, 50), limits=c(0, 150))
    })
    
    output$scatter<-renderPlotly({
      
      ggplot(filter(dat,gender %in% input$gender),aes(x=educ,y=pid7))+
        geom_jitter()+
        geom_smooth(method = "lm")+
        geom_point()
      })
    
    output$table <- renderDataTable({
      filter(dat,region %in% input$region)})
    
    
  }
  
shinyApp(ui,server)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文