【问题标题】:Using filter in plotOutput for dynamic User defined data in Shiny app使用 plotOutput 中的过滤器获取 Shiny 应用程序中的动态用户定义数据
【发布时间】: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) 

非常感谢

【问题讨论】:

    标签: r graphics shiny


    【解决方案1】:

    就我个人而言,我不会在 ggplot() 调用中进行任何子集设置。您也不需要(实际上也不应该)在ggplot() 调用中指定data()。我可能会这样做(尽管如果你不提供可重现的数据就很难测试):

    x_var <- reactive(input$xcol)
    y_var <- reactive(input$ycol)
    
    data() %>%
        filter(F1 == F2) %>%
        ggplot(aes_string(x = x_var(), y = y_var())) +
        geom_point()
    

    关于我的最后一点,请考虑以下之间的区别:

    mtcars %>% filter(carb == 2) %>% ggplot(aes(x = carb, y = wt)) + geom_point()
    # this works!
    
    mtcars %>% filter(carb == 2) %>% ggplot(aes(x = mtcars$carb, y = mtcars$wt)) + geom_point()
    # this doesn't
    

    后者不起作用,因为它试图绘制 mtcars$carb,而不是过滤后的 (mtcars == 2) 向量。

    【讨论】:

    • 感谢您的回答,但在这种情况下,它只会占用数据点而不是整个列来进行绘图。如果我们使用,x_var 作为时间序列,y_var 作为连续变量;它会一次显示一个点,而不是所有数据。
    • 嗨@Mutaz,看看我的编辑——这行得通吗?我认为以编程方式传递变量时我们需要aes_string
    • 谢谢,但没有任何变化,事实上,即使尝试不同的数据集,它也会在尝试绘制时给出清晰的图形
    • 好的。将来,请尝试提供一些数据,以便尝试提供帮助的人能够使用与您相同的数据。
    • 非常感谢@heds1!但我正在尝试将其用作数据可视化工具,而不是用于特定数据集。