Home > other >  Use early created dataframe in Shiny server
Use early created dataframe in Shiny server

Time:01-24

I am trying to use a created dataframe (df2 because iris is built-in) in a chunk before the shiny server chunk as input. Unfortunately, it doesn't work as expected. Here is some reproducible code:

---
title: "Object error"
format: 
  html:
    page-layout: custom
server: shiny
---

```{r}
# Created dataframe
df <- iris
df2 <- df
```

```{r}
#| panel: sidebar
vars <- unique(df2$Species)
selectInput('specie', 'Specie: ', vars)
```

```{r}
#| panel: fill
plotOutput('plot1')
```

```{r}
#| context: server
library(dplyr)
library(ggplot2)

selectedData <- reactive({
  subset(df2, Species %in% input$specie)
  })

output$plot1 <- renderPlot({
  selectedData() %>%
    ggplot(aes(x = Sepal.Length, y = Sepal.Width))  
    geom_point()
})
```

Output:

enter image description here

As you can see the shiny server doesn't find the early created dataframe df2. So I was wondering if it is possible to use an early created dataframe in a server shiny like above in Quarto?


Edit: create dataframe in server

When creating df2 with df in the server like this doesn't work:

```{r}
#| context: server
library(dplyr)
library(ggplot2)
df2 <- df
selectedData <- reactive({
  subset(df2, Species %in% input$specie)
  })

output$plot1 <- renderPlot({
  selectedData() %>%
    ggplot(aes(x = Sepal.Length, y = Sepal.Width))  
    geom_point()
})
```

CodePudding user response:

I would recommend to read along output

CodePudding user response:

Try add context: setup

   ```{r}
   #| context: setup
   df <- iris
    df2 <- df
    ```

-output

enter image description here

  • Related