How can I filter from a reactive dataset using dplyr?

David Jorquera

I can't filter a reactive dataset. I need to call different datasets on my pc according to the user input; since those datasets have the same row names I want to define a single filter for them.
For this question I made two simple datasets. Thank you so much in advance.

UI

library(shiny)

ui <- fluidPage(
sidebarLayout(sidebarPanel(selectInput('number', h4("Select number"), 
choices = c("first", "second")),
    selectInput('dataset', h4("Select dataset"),
    choices=c("dataset1",
    "dataset2"))),

    mainPanel(
    plotOutput("graph")
    )))

SERVER

server <- function(input, output) {

library(ggplot2)
library(dplyr)


dataset1 <- data.frame(names=c("first", "second"), 
X1=rnorm(1:10),X2=rnorm(1:10),X3=rnorm(1:10))
dataset2 <- data.frame(names=c("first", "second"), 
X1=rnorm(1:10),X2=rnorm(1:10),X3=rnorm(1:10))

plotdata <- reactive ({get(input$dataset)

if(input$number == "first") {
  filter(plotdata, names=="first")}
else if(input$number == "second") {
  filter(plotdata, names=="second")} 
})

 output$graph <- renderPlot({

datos <- plotdata()

ggplot(datos, aes(X1, X2)) + 
  geom_col()

 })

 }

 shinyApp(ui = ui, server = server)

I get the following Error: no applicable method for 'filter_' applied to an object of class "c('reactiveExpr', 'reactive')"

However, when the reactive data section is replaced by the following code, it makes the graph without issues but I would have to define all possible combinations between variables and datasets (which is not an option since the real datasets are 5 with lots of variables and rows to be selected on the UI):

plotdata <- reactive ({

if(input$number == "first") {
  filter(dataset1, names=="first")}
else if(input$number == "second") {
  filter(dataset1, names=="second")} 
})

So how can I filter from a reactive dataset using dplyr??

Florian

You can do that like this:

library(shiny)

ui <- fluidPage(
  sidebarLayout(sidebarPanel(selectInput('number', h4("Select number"), 
                                         choices = c("first", "second")),
                             selectInput('dataset', h4("Select dataset"),
                                         choices=c("dataset1",
                                                   "dataset2"))),
                mainPanel(
                  plotOutput("graph")
                ))
)

server <- function(input, output) {

  library(ggplot2)
  library(dplyr)

  dataset1 <- data.frame(names=c("first", "second"), 
                         X1=rnorm(1:10),X2=rnorm(1:10),X3=rnorm(1:10))
  dataset2 <- data.frame(names=c("first", "second"), 
                         X1=rnorm(1:10),X2=rnorm(1:10),X3=rnorm(1:10))

  plotdata <- reactive ({
    dataset <- get(input$dataset)
    dataset %>% filter(names==input$number)
  })

  output$graph <- renderPlot({
    datos <- plotdata()
    ggplot(datos, aes(X1, X2)) + 
      geom_col()
  })

}

shinyApp(ui = ui, server = server)

However, my advice is to not use get, but store your datasets in a named list, so something like datasets <- list('dataset1'=1, 'dataset2'=2) and then call with datasets[[input$dataset]]

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I filter multiple columns with dplyr using string matching for the column name?

How can I filter a pandas dataset based on multiple columns?

How can I filter tf.data.Dataset by specific values?

Trouble using inputted variable with dplyr in Shiny with a reactive dataset

How to filter a dataset using a parameter?

In R, how can I filter a data frame by data type using dplyr?

How can I open a specific dataset (band) from a multi dataset MODIS image using Rasterio? Rasterio IndexError: band index out of range

I want to do random sampling from a dataset using Bagging. And use that dataset in further analysis. How can I do that?

How can I create a function that creates a matrix using values from my dataset in R?

How can I use dplyr::filter with "|" operator?

How can I filter rows that are all NA using dplyr `across()` syntax?

How can I plot several diagrams that represent several subset rows from the dataset in one plot using Gnuplot?

How can we filter a join in R using DPLYR

How can I filter a YAML dataset with an attribute value?

How can I filter based on a condition in dplyr?

How can I filter a string from a pcap file using python?

How can I use base::scale() on variables together with dplyr::filter()

How can I filter a string field in a dataset with a like clause and an umlaut?

How can I use a pre-assigned variable in dplyr::filter?

How can I filter a BigQuery dataset that contains array data?

How to use dplyr::filter with input values in a reactive environment?

How can I transform this dataset in pandas so that it easy to filter and compare?

In R, how can I filter out specific values in an array using dplyr's piping operator (%>%)?

How can I filter a time period that includes midnight in dplyr?

How can i get a specific data from my dataset using SQL and PHPmyadmin?

How can I filter keywords from a csv file using Python

How can I filter on dplyr using multiple columns?

How can I filter attribute from json file using nifi

How can I prepare a reactive dataset for reuse in downstream chunks in a shiny Rmd?