r-有可能为我的闪亮应用程序过滤geom_histogram函数中的正确值吗



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

是否可以在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)

这是我的Data表,显示了head函数:

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)

最新更新