You solution look correct. But when I implemented in a dashboard I did for iris dataset, it does not work. The code is below. Am i implementing it wrong?
library(tidyverse)
library(janitor)
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
library(extrafont)
#> Warning: package 'extrafont' was built under R version 4.1.2
#> Registering fonts with R
library(shiny)
library(shinydashboard)
#>
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#>
#> box
library(gridExtra)
#> Warning: package 'gridExtra' was built under R version 4.1.3
#>
#> Attaching package: 'gridExtra'
#> The following object is masked from 'package:dplyr':
#>
#> combine
windowsFonts(a=windowsFont("Times New Roman"))
ui<-dashboardPage(
skin="red",
dashboardHeader(title = "Iris dataset dashboard"),
dashboardSidebar("Select the inputs here",
selectInput("species","Select the species",choices = c("All",unique(iris$Species))
,multiple = T)),
dashboardBody(
tabsetPanel(
type="tabs",
id="tab_selected",
tabPanel("First view",
plotOutput("plot1",width = 1200,height = 900))
)
))
server<-function(input,output){
data_react<-reactive({
filter(Species %in% input$species) %>%
group_by(Species) %>%
summarise(mean_petal_length=mean(Petal.Length),
mean_petal_width=mean(Petal.Width),
mean_sepal_length=mean(Sepal.Length),
mean_sepal_width=mean(Sepal.Width))
iris %>%
if(input$species=="All"){
iris
} else{
iris[iris$Species==input$species, ]
}
})
output$plot1<-renderPlot({
g1<-ggplot(data_react(),aes(input$species,mean_petal_length))+
geom_bar(stat="identity",width=0.5,fill="orange")+
theme_classic()+
geom_text(aes(label=mean_petal_length),size=6)+
labs(title="Average value of Petal length of 3 species of iris",
x="Species",
y="Average value")+
theme(plot.title = element_text(hjust=0.5,family = "a",face="bold",size = 18),
text = element_text(family = "a"),
axis.title = element_text(face="bold",size=15),
axis.text = element_text(size=14))+
scale_y_continuous(breaks = seq(0,10,by=1))
g2<-ggplot(data_react(),aes(input$species,mean_petal_width))+
geom_bar(stat="identity",width=0.5,fill="orange")+
theme_classic()+
geom_text(aes(label=mean_petal_width),size=6)+
labs(title="Average value of Petal width of 3 species of iris",
x="Species",
y="Average value")+
theme(plot.title = element_text(hjust=0.5,family = "a",face="bold",size = 18),
text = element_text(family = "a"),
axis.title = element_text(face="bold",size=15),
axis.text = element_text(size=14))+
scale_y_continuous(breaks = seq(0,3,by=0.5))
g3<-ggplot(data_react(),aes(input$species,mean_sepal_length))+
geom_bar(stat="identity",width=0.5,fill="orange")+
theme_classic()+
geom_text(aes(label=mean_sepal_length),size=6)+
labs(title="Average value of Sepal length of 3 species of iris",
x="Species",
y="Average value")+
theme(plot.title = element_text(hjust=0.5,family = "a",face="bold",size = 18),
text = element_text(family = "a"),
axis.title = element_text(face="bold",size=15),
axis.text = element_text(size=14))+
scale_y_continuous(breaks = seq(0,7,by=1))
g4<-ggplot(data_react(),aes(input$species,mean_sepal_width))+
geom_bar(stat="identity",width=0.5,fill="orange")+
theme_classic()+
geom_text(aes(label=mean_sepal_width),size=6)+
labs(title="Average value of Sepal width of 3 species of iris",
x="Species",
y="Average value")+
theme(plot.title = element_text(hjust=0.5,family = "a",face="bold",size = 18),
text = element_text(family = "a"),
axis.title = element_text(face="bold",size=15),
axis.text = element_text(size=14))+
scale_y_continuous(breaks = seq(0,4,by=0.5))
grid.arrange(g1,g2,g3,g4,nrow=2, ncol=2)
})
}
shinyApp(ui,server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents
Created on 2022-04-09 by the reprex package (v2.0.1)