是否可以为我闪亮的应用程序过滤 geom_histogram 函数中的正确值?

Is it possible to filter the correct values in the geom_histogram function for my shiny app?

我要绘制的值和 sideBarpanel 的值在同一列中。我更喜欢使用长 table 而不是宽 table.

是否可以在 geom_histogram 函数中过滤出正确的值?

library(shiny)
library(ggplot2)
library(dplyr)
library(tidyverse)
library(readxl)
library(dplyr)

Survey <- read_excel("~/EIGSI/Project/Work files for R studio/04_Participant.xlsx")
Question <- read_excel("~/EIGSI/Project/Work files for R studio/02_Question.xlsx")


Data <- Survey


Data <- Data %>% 
  gather(key = "Question",
         value = "Answer",
         -Id)

Data <- Data %>% 
  right_join(Question, by=c("Question" = "question_ID"))

names(Data)


Data <- Data %>% 
  relocate(question_group_ID, .after = Id)

Data <- Data %>% 
  relocate(question_group_text, .after = question_group_ID)

Data <- Data %>% 
  relocate(question_text, .after = Question)


#APP STARTS HERE


# ui.R ----
ui <- fluidPage(
  titlePanel("Departure times"),  # Add a title panel
  sidebarLayout(  
    position = "right",
    
    sidebarPanel(h3("Inputs for histogram"),
                 selectInput("Answer", "Select mode", choices = c("On foot"="On foot",
                                                                "Bicycle" = "Bicycle",
                                                                "Bicycle (Yélo)"="Bicycle (Yélo)",
                                                                "Motorcycle/scooter"="Motorcycle/scooter",
                                                                "Scooter (trotinette)"="Scooter (trotinette)",
                                                                "Bus"="Bus",
                                                                "Train" = "Train",
                                                                "Car" = "Car",
                                                                "Carpool"="Carpool",
                                                                "Car (Yélo)"= "Car (Yélo)"),
                                                                 selected = "Car"),
                 br(),
                 
                 
    ),
    
    # Inside the sidebarLayout, add a sidebarPanel
    mainPanel(
      plotOutput("myhist")
      
    )  
  )
)

# server.R ----
server <- function(input, output) {
  

  Data %>% 
    filter(question_group_ID == "QG3") %>% 
    ggplot(aes(x=Answer))+
    geom_histogram(stat = "count", data = Data[Data$Answer== input$Answer,])
  
  
  
}

# Run the app ----
shinyApp(ui = ui, server = server)

这是我的 table Datahead 函数显示的:

 Id Question Answer
  <dbl> <chr>    <chr> 
1    54 Q1       No    
2   132 Q1       No    
3   498 Q1       No    
4   600 Q1       No    
5   620 Q1       No    
6   951 Q1       No    

是的,你可以。您需要在输出对象中渲染绘图,如下所示。

df <- mtcars

ui <- fluidPage(
  titlePanel("Departure times"),  # Add a title panel
  sidebarLayout(  
    position = "right",
    
    sidebarPanel(h3("Inputs for histogram"),
                 selectInput("Answer", "Select mode", choices = unique(df$cyl)),
                 br()
    ),
    
    # Inside the sidebarLayout, add a sidebarPanel
    mainPanel(
      plotOutput("myhist")
      
    )  
  )
)

# server.R ----
server <- function(input, output) {
  
  output$myhist <- renderPlot({
    df %>% dplyr::filter(vs==1) %>% 
      ggplot(aes(x=carb)) +
      geom_histogram(stat = "count", data = df[df$cyl == input$Answer,])
  })
  
}

# Run the app ----
shinyApp(ui = ui, server = server)