Reference the dataframe that is drawn from the for loop

Hi Folks. Godd evening. I am facing difficulties while referencing in ggplot code. When I try to use below code for subsetting, I am getting an error.Any idea why below code is not proper?

title: "Untitled"
    orientation: columns
    vertical_layout: scroll
    runtime: shiny
    source_code: embed
    theme: cosmo

```{r setup, include=FALSE}
df <- structure(list(ColA = structure(c(19L, 3L, 12L), .Label = c("asgfg", 
"dds", "dfg", "dfh", "dgh", "dghgd", "dgrert", "dgsh", "dsgdfg", 
"dsgdg", "e", "er", "ewt", "fdg", "fdgd", "fg", "gdfgd", "gdfsh", 
"gf", "gfdg", "gfs", "gh", "ghj", "ghjhg", "gjgj", "gt", "hdsger", 
"hgdhg", "hgj", "hjghj", "jgh", "jyfj", "ret", "rth", "ryuy", 
"sdf", "sdfh", "sfdg", "sgdf", "tyew", "tyu", "tyutyu", "uiuy", 
"yoiy", "yt"), class = "factor"), ColB = structure(c(1L, 1L, 
1L), .Label = c("A", "B"), class = "factor"), ColC = 1:3, ColD = 2:4), row.names = c(NA, 
3L), class = "data.frame")

df <-
  df <-
  # percentage of count for categorical univariate ------------------------
  p1 <- list()
  bs <- names(Filter(is.factor, df))
  for(i in bs)
    p1[[i]] <-[,i]))*100,1)),p1) %>%

Inputs {.sidebar} {data-width=140}

h6(selectInput("Plot1","First Variable",choices = c("",names(df)),width = 200))
    output$filter_2 <- renderUI({
    if (input$Plot1 == "") {
    } else {
      h6(selectInput("b", label = "Levels", choices = unique(df[,input$Plot1]),multiple = TRUE,width = 200))
h6(selectInput("Plot2","Second Variable",choices = c("",names(df)),width = 200))

Column {data-width=350}

### Univariate analysis

s_data <- reactive({
   p1[[input$Plot1]][["Var1"]] %in% input$b
Plot2 <- reactive(
      for(i in bs)
      {if (input$Plot1 == i && input$b != "") {
        p[[i]] <- print(ggplot(data=s_data()[[i]],aes_string(x=reorder(s_data()[[i]][["Var1"]],+s_data()[[i]][["Freq"]]),y=s_data()[[i]][["Freq"]],fill=s_data()[[i]][["Freq"]]))+geom_bar(stat = "identity")+ylab("Percentage")+xlab(i)+theme(axis.text.x=element_text(angle=90, hjust=1,size = 12))+geom_text(aes_string(label = s_data()[[i]][["Freq"]]),size=4,vjust = 1,hjust =1, colour="white")+coord_flip())
      } else if (input$Plot1 == "NULL") {
    output$Plot2 <- renderPlot(

when I try to run this code, there is an error like below. Basically my filtering is not working


Hi @vinayprakash808!

It's definitely helpful to cut your code down to only the parts that relate to your question, but it's also possible to go too far, which is what I'm afraid has happened here. In order for people to be able to help, the code still needs to be complete enough to run on someone else's computer.

I can't say much about what's causing your problem, because I have no idea where the two lines in your Step 2 code occur relative to each other, and to other code in the surrounding app. For instance, I can't see where in your app p1 gets created, so I can't even begin to suggest why you might be having trouble with it. And I can't see exactly what error is occurring, so I also don't quite know what "not able to reference p1" means in this case (there are lots of different ways you can get the same error symptoms).

But as I think you've figured out, the best way to help people here understand what's going wrong is not to provide all the code for the app :sweat_smile:. What you need to do is make a new, bare bones shiny app that has just enough code in it to demonstrate your problem in a way that is faithful to your real app. This can be a difficult task when you're not used to doing it, and it may require some thought and some trial and error.

For a bare bones Shiny skeleton app that you can use as a starting point for composing a reproducible example, see this FAQ: Shiny debugging and reprex guide. Another approach is to start with a copy of your app, then delete everything that's not related to your problem — but make sure to leave enough of the Shiny skeleton so that the app is actually an app!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.