Reputation: 607
I am building a shiny app in R that will: i) use two radiobuttons to filter a data set, ii) plot the filtered data set, and iii) use brushpoints to identify outlier points for futher inspection.
Below is a short version of my data set as well as the shiny code. The filtering based on the first radiobutton appears to be working correctly. However, the wheels come off the bus with the filtering based on the second radiobuttion. I am trying to setup the code so that values >=
the value of the second radiobutton are plotted, but this is not happening and I have not figured out what the problem is.
Here is where I think the problem is...but I am at a loss at this point.
plot_subset_df <- reactive({
filter(sub_react$data, CODE == input$CODE & DAYS_DIFFERENCE <= input$DAYS_DIFFERENCE)})
While much of this code is written in dplyr
and DT
, I trying to move to base package...so all solutions are welcome.
Thanks in advance for your help.
Example data set
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)
Current version of code
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)
Upvotes: 0
Views: 528
Reputation: 388817
The issue is input$DAYS_DIFFERENCE
is of type character. For example, if you compare "7" with values 1:20 only 8 and 9 are shown greater than 7.
which(1:20 > "7")
#[1] 8 9
change input$DAYS_DIFFERENCE
to numeric/integer before filtering the data.
plot_subset_df <- reactive({
filter(sub_react$data, CODE == input$CODE &
DAYS_DIFFERENCE >= as.numeric(input$DAYS_DIFFERENCE))
})
Upvotes: 1
Reputation: 6132
To me it seems like the problem could be the NA
s in your DAYS_DIFFERENCE
column. You may consider to use:
plot_subset_df <- reactive({
filter(sub_react$data, CODE == input$CODE & DAYS_DIFFERENCE %in%
c(0:input$DAYS_DIFFERENCE))
})
Also, please notice in your question you mention >=
, but in your code <=
.
At last, notice that your axes changes all the time when applying another value for your DAYS_DIFFERENCE
column, so sometimes the plot seems to change more than it really does when changing the input.
Upvotes: 1