Selection of charts in Shiny - r

I am trying to create a simple Shiny app. My goal is to select one of the charts from the drop-down menu and visualization of the selected chart. Below you will see my script.
---
title: "Test App"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
# Libraries ----
# App
library(flexdashboard)
library(shiny)
library(shinyjs)
library(shinyWidgets)
# Core
library(tidyverse)
library(tidyquant)
# Visualizations
library(ggplot2)
# Data
data=mtcars
data$cyl <- as.factor(data$cyl)
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
useShinyjs(rmd = TRUE)
selectInput("clusterNum", label = h4("Charts"),
choices = list("Chart1" = "Chart1", "Chart2" = "Chart2"),
selected = "Chart1")
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart
```{r}
Chart1<-qplot(x = data$wt, y = data$mpg)
Chart2<-qplot(data$cyl, geom = "bar")
num <- reactive(input$clusterNum)
renderPlot(num())
```
When I run this script, I can change the names of the Charts (e.g., Chart1 or Chart2) from the drop-down menu, but I can't see a visualization of the selected chart. So can anybody help me how to solve this problem and to have a visualization as in the pic below?

You could use switch or an ìf-else inside your renderPlot to display the chosen chart.
Note: qplot was depcrecated in ggplot2 3.4.0 and will probably removed in the future. To take account of this I switched to ggplot().
---
title: "Test App"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(tidyverse)
# Data
data <- mtcars
data$cyl <- as.factor(data$cyl)
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("clusterNum",
label = h4("Charts"),
choices = list("Chart1" = "Chart1", "Chart2" = "Chart2"),
selected = "Chart1"
)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart
```{r}
Chart1 <- ggplot(data, aes(x = wt, y = mpg)) +
geom_point()
Chart2 <- ggplot(data, aes(cyl)) +
geom_bar()
renderPlot({
switch(input$clusterNum,
"Chart1" = Chart1,
"Chart2" = Chart2
)
})
```

Related

Layout of flexdashboard - having multiple tabsets

I currently have my dashboard which looks like:
What I am trying to do is under the Data tab have additional tabs. For example I want to combine the Table Population tab with the Graphic iris tab so that the table and graphic are on the same page. Then in a new tab have the Another tab-pag (as it currently is).
How can I merge the two tabs to have them on the same page?
I would like this joined tab to look like the Analysis tab:
Dashboard:
---
title: "Analysis"
output:
flexdashboard::flex_dashboard:
#css: "custom_style.css"
theme:
base_font:
google: Prompt
code_font:
google: JetBrains Mono
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
library(shinyWidgets)
library(shinyjs)
library(gt)
library(gtExtras)
library(plotly)
```
MySidebar {.sidebar}
==============================
```{r}
useShinyjs(rmd = TRUE)
shinyWidgets::pickerInput(
inputId = "select_species",
label = h4("Species"),
choices = unique(iris$Species),
selected = unique(iris$Species[1]),
multiple = TRUE,
options = list(
`actions-box`= TRUE,
size = 10,
`selected-text-format` = "count > 1"
)
)
```
Introduction {data-vertical_layout=scroll}
==============================
Column {data-width=650 .main .tabset}
-----------------------------------------------------------------------
Here is some introduction text
Data {data-vertical_layout=scroll}
==============================
IRIS Species {.tabset .tabset-fade}
-------------------------------------
### Table Population
```{r}
output$irisTable = render_gt({
iris %>%
gt() %>%
gt_add_divider(columns = "Provincias", weight = px(3), color = "lightgrey", include_labels = FALSE)
})
div(style='height:800px; overflow-y: scroll',
gt_output("populationTable")
)
```
### Graphic iris
```{r}
renderPlotly({
ggplotly(
iris %>%
ggplot(aes(x = Species, y = Petal.Length)) +
geom_bar() +
theme_bw()
)
})
```
### Another tab-page
Analysis {data-vertical_layout=scroll}
==============================
Column
-------------------------------------
### Tab 1
some text
### Tab 2
some more text

How to use shiny inputpael to filter my dataframe by a category in a column? Rshiny and RMarkdown

I'm trying to make a simple shiny app where I can select an input of cylinders(4,6, or 8) and then generate a table with the cars who are either 4,6, or 8 cylinders.
This is my code
---
title: "Test Dash"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
Inputs {.sidebar data-width=300}
=====================================
$$\\[.01in]$$
```{r pressure, echo=FALSE, out.width = '100%'}
library(readr)
library(shiny)
library(DT)
```
```{r}
cylinder <- mtcars$cyl
selectInput("my_dropdown", label = "Select Cylinders:", choices = cylinder)
```
# Overview
Column {data-width=750}
-----------------------------------------------------------------------
### Table
```{r}
renderDataTable(
datatable(mtcars[input$my_dropdown,]
)
)
```
i'm able to generate the input and an empty table but the table does not update when I choose a cylinder.
Here is a screenshot.
Any idea how I can fix this?
mtcars2 <- reactive({
mtcars[mtcars$cyl == input$my_dropdown,]
})
renderDataTable(
datatable(mtcars2())
)
)

file.choose() alternative to import file in flexdashboard rstudio when published on shinyapps.io

I have created a project where you can upload any file from your local computer and you get different graphs on selected variables(Bar graph,Line graph,Box plot etc)
My project is working on local computer software, but when i publish on shinyapps.io , it shows an error since it does not support read.csv(file.choose()) command.
Please help me with code that i can add, so that shinyapps.io would run my code, and everyone could upload their file on website and startData variable could store that csv file for further process.
Or making a Input button on my flexdashboard which will import file and give to my variable startData
---
#author: "Yatharth Garg"
title: "SARAL"
output:
flexdashboard::flex_dashboard:
social: menu
source_code: embed
#orientation: rows
vertical_layout: scroll
theme: united #cerulean
logo: favicon-32x32.png
runtime: shiny
---
```{r setup, include=FALSE}
library(tidyverse)
library(forecast)
library(highcharter)
library(DT)
startData <-read.csv(file.choose())
GBChoices <- as.list(names(startData))
names(GBChoices) <- paste(names(startData),map(startData,~length(unique(.x))))
updateData <- reactive(
startData %>% group_by(!!! rlang::syms(input$GB)) %>% summarise_if(is.numeric,sum,na.rm=T))
```
Sidebar {.sidebar}
=====================================
```{r}
selectInput(inputId = "GB",label = "Group By",choices = GBChoices)
selectInput(inputId = "Metric",label = "Metric",choices = names(select_if(startData,is.numeric)))
```
Bar Graph
=====================================
Column
-------------------------------------
### Bar Graph
```{r}
renderPlot({
updateData() %>%
ggplot(aes(x=!! rlang::sym(input$GB),y=!! rlang::sym(input$Metric),fill=!! rlang::sym(input$GB))) +
geom_col()
})
```
### Table
```{r}
renderDT(
updateData(), rownames = F, extensions = 'Buttons', filter="top", editable=T,
options = list(
dom = 'Blfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
lengthMenu = list(c(10,50,100,-1),c(10,50,100,"All"))
)
)
```
Line Graph
=====================================
Column
-------------------------------------
### Line Graph
```{r}
renderPlot({
updateData() %>%
ggplot(aes(x=!! rlang::sym(input$GB),y=!! rlang::sym(input$Metric),fill=!! rlang::sym(input$GB))) +
geom_line()
})
```
Heat Map
=====================================
Column
-------------------------------------
### bin2d Graph
```{r}
renderPlot({
updateData() %>%
ggplot(aes(x=!! rlang::sym(input$GB),y=!! rlang::sym(input$Metric),fill=!! rlang::sym(input$GB))) +
geom_bin2d()
})
```
Point & Smooth graph
=====================================
Column
-------------------------------------
### Point Graph
```{r}
renderPlot({
updateData() %>% ggplot(aes(x=!! rlang::sym(input$GB),y=!! rlang::sym(input$Metric),fill=!! rlang::sym(input$GB))) +
geom_jitter()
})
```
Column
-------------------------------------
### Smooth Graph
```{r}
renderPlot({
updateData() %>% ggplot(aes(x=!! rlang::sym(input$GB),y=!! rlang::sym(input$Metric),fill=!! rlang::sym(input$GB))) +geom_point(shape=1) +
geom_smooth()
})
```
BoxPlot
=====================================
Column
-------------------------------------
### BoxPLot
```{r}
renderPlot({
updateData() %>% ggplot(aes(x=!! rlang::sym(input$GB),y=!! rlang::sym(input$Metric),fill=!! rlang::sym(input$GB))) +
geom_boxplot()
})
```
Please append your innovative code in my code and reply me back.
I need your help, please please help me with that.

Using plot_click in flexdashboard

Is there a way to have plots with mouse interactions using flexdashboard?
In shiny this is not difficult. I want to save mouse clicks, and in shiny UI I would use:
mainPanel(plotOutput("scatterplot", click = "plot_click"))
And in the server you would have:
df <- reactiveValues(Clicksdf = data.frame(clickx = numeric(), clicky = numeric()))
Can I do this in flexdashboard?
Write the code chunk as if it were both the Shiny UI and server:
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
plotOutput("plot1", click = "wt")
output$plot1 <- renderPlot({
plot(mtcars$mpg ~ mtcars$wt)
})
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart B
```{r}
renderText({
unlist(input$wt$x)
})
```

Tabset panel in flexdashboard storyboard layout

I'm trying to include ploly plots in a tabbed html widget within a flexdashboard document with a storyboard layout. The autosizing of the first plot is fine, but not on any subsequent tab. Here is an MRE,
---
title: "MRE"
output:
flexdashboard::flex_dashboard:
storyboard: true
---
```{r setup, include=FALSE}
library(flexdashboard)
library(plotly)
library(shiny)
```
### Test
```{r}
plot1 = plot_ly(x = 1:5, y = 6:10, type = "scatter")
plot2 = plot_ly(x = 1:5, y = 1:5, type = "scatter")
tabsetPanel(
tabPanel("Employee Monthly", plot1),
tabPanel("Employee Weekly", plot2)
)
```

Resources