ruser_jp
ruser_jp

Reputation: 1

Display a Table from fileInput selected by selectInput in Flexdashboard

  1. File (.xlsx) input
  2. Filter the inputfile by a value selected on selectInput
  3. Show the table created in #2.

Getting "error: operation not allowed without an active reactive context."

Reproducible data :

Drug Name Col_B Col_C
a1 140 14
a2 120 2
a3 140 14
a4 10 2
a3 20 2
a3 100 14
a4 120 2
a1 140 14
a2 120 2
a3 140 14
---
title: 'title1'
output: 
  flexdashboard::flex_dashboard:
    theme: 
      version: 4
      bootswatch: minty
  orientation: columns
  vertical_layout: fill
date: '2022-10-10'
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

```{r,  echo=FALSE}
dataset <- eventReactive(input$file1,{
      dataset <- read_excel(input$file1$datapath)
    })
cm_table = reactive({
  if(input$drug_name == "ALL"){
    dataset()
  }
  else{
    dataset() %>%
        filter(`Drug Name`== input$drug_name)
  }
})
```

```{r, echo=FALSE}
 #fileinput
fileInput("file1", "Choose Excel File", accept = c('.xlsx'))
 #select drug name from cm_table
choice <- cm_table() %>%
  .$`Drug Name` %>%
  unique %>% sort
selectInput("drug_name", label = "Select Drug Name:", choices = c("ALL", choice), selected = "ALL")

```



```{r}
renderTable({
      cm_table <- cm_table()
      cm_table
    })
```

Upvotes: 0

Views: 356

Answers (1)

stefan
stefan

Reputation: 125208

There are several issues with your code. First issue which gave rise to the error you mentioned in your post was that you did

choice <- cm_table() %>% ...

i.e. you tried to access the value of the reactive cm_table outside of a reactive context. However, when fixing that the next error jumped up due to the missing libraries and then the next one ... and so on. (;

I'm not going through all the issues but here is a working code.

Note: I dropped the file input stuff as IMHO it wasn't the reason for your issue.

---
title: 'title1'
output: 
  flexdashboard::flex_dashboard:
    theme: 
      version: 4
      bootswatch: minty
  orientation: columns
  vertical_layout: fill
date: '2022-10-10'
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

```{r}
library(dplyr)
```

```{r,  echo=FALSE}
dataset <- reactive({
  data.frame(
    stringsAsFactors = FALSE,
    `Drug Name` = c(
      "a1", "a2", "a3", "a4", "a3",
      "a3", "a4", "a1", "a2", "a3"
    ),
    Col_B = c(140L, 120L, 140L, 10L, 20L, 100L, 120L, 140L, 120L, 140L),
    Col_C = c(14L, 2L, 14L, 2L, 2L, 14L, 2L, 14L, 2L, 14L),
    check.names = FALSE
  )
})
```

Column {.sidebar}
-----------------------------------------------------------------------

```{r, echo=FALSE}
uiOutput("select_drug")

output$select_drug <- renderUI({
  choices <- sort(unique(dataset()[["Drug Name"]]))
  
  selectInput("drug_name", label = "Select Drug Name:", choices = c("ALL", choices), selected = "ALL")  
})
```

Column
-----------------------------------------------------------------------

```{r}
cm_table <- reactive({
  if (input$drug_name == "ALL") {
    dataset()
  } else {
    dataset() %>%
      filter(`Drug Name` == input$drug_name)
  }
})
```

```{r echo=FALSE}
renderTable({
  cm_table()
})
```

enter image description here

Upvotes: 0

Related Questions