【发布时间】:2025-12-20 22:30:11
【问题描述】:
我在过滤 ggplot2 可视化数据时遇到了麻烦。我想使用过滤器并输入 f 值,例如使用
data %>%
filter(Col == Number)
ggplot()
根据数据集自定义绘图
我使用此代码生成了一个 Shiny 应用程序,该应用程序允许用户上传数据并可以交互地绘制数据,但过滤器不起作用。
:
library(shiny)
library(tidyverse)
ui <- shinyUI(
fluidPage(
h1(' output'),
h2('Graphics'),
sidebarLayout(
sidebarPanel(
fileInput(inputId = 'file1', label = 'Upload your Data:', accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
selectInput('xcol', "X variable:", "", selected = ""),
selectInput('ycol', 'Y variable:', "", selected = ""),
selectInput('filter1', 'Filter:', "", selected = "", multiple = TRUE),
selectInput('filter2', 'Filter Value',"",selected = "")
),
mainPanel(
plotOutput("plot")
)
)
)
)
server <- shinyServer(function(session, input, output){
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- read_csv(inFile$datapath)#, header = T, sep=",")
updateSelectInput(session, inputId = 'xcol', label = 'X variable:',
choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'ycol', label = 'Y variable:',
choices = names(df), selected = names(df))
updateSelectInput(session, inputId = 'filter1', label = 'Filter:',
choices = names(df), selected ="")
observe({
updateSelectInput(session, inputId = 'filter2', label = 'Filter value', choices =c(0:10))
})
return(df)
})
output$plot <- renderPlot({
F1 <- input$filter1
F2 <- input$filter2
data() %>% filter(F1==F2)%>%
ggplot(aes(x =data()[input$xcol],y= data()[input$ycol]))+
geom_point()
})
})
shinyApp(ui = ui, server = server)
非常感谢
【问题讨论】: