0

I keep getting errors when trying to filter a data base according to a selected input. I made this really simple example based on the iris dataset to show you guys my problem:


    ```{r}
    library(flexdashboard)
    library(tidyverse)
    ```
    
    Sidebar {.sidebar}
    =====================================
    
    ```{r}
    
    fluidRow(
      column(7,
             selectInput("Species", "Choose a species",
                         choices = c("setosa", "versicolor", "virginica"))))
    
    mydata <- reactive({
    iris %>% filter(Species == input$Species)
    })
    
    ```
    
    Results
    ===================================== 
    
    ```{r}
    head(mydata)
    ```

starja
  • 9,887
  • 1
  • 13
  • 28
Ruben
  • 5
  • 2

1 Answers1

0

As mydata is a reactive, you have to evaluate it with mydata() in a reactive context (e.g. renderDT). For more information, see flexdashboard with shiny and a shiny tutorial.

---
title: "test"
runtime: shiny
output:
  flexdashboard::flex_dashboard:
  orientation: columns
vertical_layout: fill
theme: bootstrap
---
  

    ```{r global, include = FALSE}
    
    library(flexdashboard)
    library(tidyverse)
    library(DT)
    
    ```
    
    Sidebar {.sidebar}
    =====================================
    
    ```{r}
    
    fluidRow(
      column(7,
             selectInput("Species", "Choose a species",
                         choices = c("setosa", "versicolor", "virginica"))))
    
    mydata <- reactive({
    iris %>% filter(Species == input$Species)
    })
    
    ```
    
    Results
    ===================================== 
    
    ```{r}
    renderDT({head(mydata())})
    ```

starja
  • 9,887
  • 1
  • 13
  • 28
  • Ok, thank you very much for your help. But if instead of showing the dataset, as in my example, I use it to make some more analysis, like calculate the mean of Sepal.Lenght `mean(mydata$Sepal.Lenght)`, how could I do it? – Ruben Sep 15 '20 at 18:52
  • again, use `mydata()$Sepal.Length` in a reactive context, e.g. a reactive: `new_data <- reactive({mean(mydata()$Sepal.Lenght)})`. Have a look at the tutorials I've linked – starja Sep 15 '20 at 18:57
  • Thank you again, I appreciate it. – Ruben Sep 15 '20 at 19:28