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"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
runtime: shiny
source_code: embed
theme: cosmo
---
```{r setup, include=FALSE}
library(flexdashboard)
library(magrittr)
library(ggplot2)
library(tidyverse)
library(dplyr)
library(tidyr)
```
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 <- as.data.frame(df)
df <- as.data.frame(unclass(df))
# percentage of count for categorical univariate ------------------------
p1 <- list()
bs <- names(Filter(is.factor, df))
for(i in bs)
{
p1[[i]] <- as.data.frame(round(prop.table(table(df[,i]))*100,1))
do.call(rbind,p1) %>% as.data.frame()
}
Summary
=================
Inputs {.sidebar} {data-width=140}
-----------------------------------------------------------------------
```{r}
h6(selectInput("Plot1","First Variable",choices = c("",names(df)),width = 200))
output$filter_2 <- renderUI({
if (input$Plot1 == "") {
return()
} else {
h6(selectInput("b", label = "Levels", choices = unique(df[,input$Plot1]),multiple = TRUE,width = 200))
}
})
uiOutput("filter_2")
h6(selectInput("Plot2","Second Variable",choices = c("",names(df)),width = 200))
```
Column {data-width=350}
-----------------------------------------------------------------------
### Univariate analysis
```{r}
plotOutput("Plot2")
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(
{
Plot2()
}
)
```
when I try to run this code, there is an error like below. Basically my filtering is not working