【问题标题】:Conditional filtering not working in shiny app条件过滤在闪亮的应用程序中不起作用
【发布时间】:2021-06-01 16:57:30
【问题描述】:

我正在 R 中构建一个闪亮的应用程序,它将:i) 使用两个单选按钮来过滤数据集,ii) 绘制过滤后的数据集,以及 iii) 使用画笔点来识别异常点以进行进一步检查。

以下是我的数据集的简短版本以及闪亮的代码。基于第一个单选按钮的过滤似乎工作正常。但是,车轮会通过基于第二个无线电按钮的过滤而脱离总线。我正在尝试设置代码,以便绘制第二个单选按钮的值>=,但这没有发生,我还没有弄清楚问题所在。

这就是我认为问题所在……但我现在不知所措。

plot_subset_df <- reactive({
    filter(sub_react$data, CODE == input$CODE & DAYS_DIFFERENCE <= input$DAYS_DIFFERENCE)})

虽然大部分代码是用dplyrDT 编写的,但我正在尝试移至基本包...所以欢迎所有解决方案。

提前感谢您的帮助。

示例数据集

sub <- read.table(text = "NUMBER DEPTH DAYS_DIFFERENCE CODE DAY MONTH YEAR INHA
1   0.81    0   BROA    16  8   1986    64.35
2   NA  NA  BROA    16  8   1986    64.35
1   0.67    7   BROA    4   9   1987    60.48
2   NA  NA  BROA    4   9   1987    60.48
1   0.67    14  BROA    29  9   1987    58.68
2   1   14  BROA    29  9   1987    58.68
1   0.96    20  BROA    21  8   1988    36.72
2   NA  NA  BROA    21  8   1988    36.72
1   0.96    0   BROA    15  9   1988    38.43
2   NA  NA  BROA    15  9   1988    38.43
1   0.76    10  BROA    24  10  1988    57.69
2   NA  NA  BROA    24  10  1988    57.69
1   0.76    3   BROA    9   11  1988    41.49
2   NA  NA  BROA    9   11  1988    41.49
1   1   14  DULB    18  8   1986    64.35
2   NA  NA  DULB    18  8   1986    64.35
1   0.5 30  DULB    20  7   1987    60.48
2   NA  NA  DULB    20  7   1987    60.48
1   0.7 10  DULB    8   10  1987    58.68
2   NA  NA  DULB    8   10  1987    58.68
1   2   3   DULB    24  10  1987    36.72
2   NA  NA  DULB    24  10  1987    36.72
1   3   0   DULB    9   11  1987    38.43
2   3.5 0   DULB    9   11  1987    38.43
1   1.5 7   DULB    25  11  1987    57.69
2   NA  NA  DULB    25  11  1987    57.69
1   0.6 20  DULB    11  12  1987    41.49
2   1.2 20  DULB    11  12  1987    41.49
1   3.3 25  DULB    27  12  1987    64.35
2   2.3 25  DULB    27  12  1987    64.35
1   4.2 40  DULB    13  2   1988    60.48
2   NA  NA  DULB    13  2   1988    60.48
1   1   50  DULB    29  2   1988    58.68
2   NA  NA  DULB    29  2   1988    58.68
", header = TRUE)

当前版本的代码

library(DT)
library(dplyr)
library(tidyr)
library(ggplot2)
library(shiny)

# Setup data set
sub$NUMBER = as.factor(sub$NUMBER)

# Shiny code
ui <- fluidPage(
  
  sidebarPanel(
  
    radioButtons("CODE", "SWMMP code",
                 choices = unique(sub$CODE)),
    radioButtons("DAYS_DIFFERENCE", "Days difference",
                 choices = c("0" = 0,
                             "3" = 3,
                             "7" = 7,
                             "10" = 10,
                             "14" = 14,
                             "20" = 20,
                             "25" = 25,
                             "30" = 30,
                             "All" = 100000))
  ),
  
  mainPanel(
    plotOutput("plot1", brush = "plot_brush"),
    DTOutput("dt_of_brushed_points"),
    verbatimTextOutput("actual_data_brushed"),
    verbatimTextOutput("actual_data_full")))

server <- function(input, output, session) {
  
  # Make data set reactive values
  sub_react = reactiveValues(data = sub)
  
  # Get data for plot
  plot_subset_df <- reactive({
    filter(sub_react$data, CODE == input$CODE & DAYS_DIFFERENCE <= input$DAYS_DIFFERENCE)
  })
  
  # Make plot
  output$plot1 <- renderPlot({
    
    ggplot(plot_subset_df(), aes(x = DEPTH, y = INHA)) + 
      geom_point(aes(size = 5, color = NUMBER))+
      theme(legend.position = "top",
            legend.title = element_blank(),
            panel.background = element_blank(),
            panel.grid.major = element_blank(), 
            panel.grid.minor = element_blank(),
            axis.line = element_line(colour = "black"))
  })
  
  # Display rows of data highlighted in plot
  brushed_df <- reactive({
    brushedPoints(plot_subset_df(), input$plot_brush, xvar = "DEPTH", yvar = "INHA", 
                  allRows = FALSE)})
  
  # Render highlighted data in plot
  output$dt_of_brushed_points = renderDT(brushed_df(), selection = 'none', editable = TRUE)
  
  # Show actual data frame to check edits are correct
  output$actual_data_brushed <- renderPrint({brushed_df()})
  #output$actual_data_full <- renderPrint({sub_react$data})
  
  # Transfer edits from top table to bottom table
  proxy = dataTableProxy('dt_of_brushed_points')
  
  observeEvent(input$dt_of_brushed_points_cell_edit, {
    info = input$dt_of_brushed_points_cell_edit
    str(info)
    i = info$row
    j = info$col
    v = info$value
    row_to_change <-  brushed_df()[i, 1]
    sub_react$data[row_to_change, j] <- isolate(coerceValue(v, sub_react$data[row_to_change, j]))
    replaceData(proxy, sub_react$data, resetPaging = FALSE)})  
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    问题是input$DAYS_DIFFERENCE 是字符类型。例如,如果您将“7”与值 1:20 进行比较,则只会显示 8 和 9 大于 7。

    which(1:20 > "7")
    #[1] 8 9
    

    在过滤数据之前将input$DAYS_DIFFERENCE 更改为数字/整数。

    plot_subset_df <- reactive({
        filter(sub_react$data, CODE == input$CODE & 
               DAYS_DIFFERENCE >= as.numeric(input$DAYS_DIFFERENCE))
      })
    

    【讨论】:

    • 感谢@Ronak Shah!我没有注意到输入产生了一个字符。保重。
    【解决方案2】:

    在我看来,问题可能出在您的 DAYS_DIFFERENCE 列中的 NAs 上。您可以考虑使用:

    plot_subset_df <- reactive({
      filter(sub_react$data, CODE == input$CODE & DAYS_DIFFERENCE %in% 
      c(0:input$DAYS_DIFFERENCE))
    })
    

    另外,请注意您在问题中提到了&gt;=,但在您的代码中提到了&lt;=

    最后,请注意,在为您的 DAYS_DIFFERENCE 列应用另一个值时,您的坐标轴一直在变化,因此有时绘图的变化似乎比更改输入时的实际变化更大。

    【讨论】:

    • 基于@Ronak Shah 的评论:这当然也是正确的,我没有注意到这一点。使用c(0:input$DAYS_DIFFERENCE),我将列转换为整数而没有注意到。
    • 是的,你们的两个解决方案都有效。给这只猫剥皮的方法有很多。谢谢你的帮助!保重。
    猜你喜欢
    • 2018-12-06
    • 2021-11-12
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多