I am developing a small app that runs on a server. The data is filtered using a session user id. The radio choices should be conditional on the reactive data (only the values of var1 that exist for user "A" in var3 should be available for choice when user "A" is logged in). Any hint? Is observe and updateSelectInput the way to go?
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(magrittr)
library(shiny)
library(ggplot2)
library(dplyr)
```
Column {data-width=650}
-----------------------------------------------------------------------
```{r global, include=FALSE}
set.seed(1234)
df <- data.frame(
var1 = sample(letters, 50, replace = T),
var2 = runif(50),
var3 = sample(LETTERS[1:5], 50, replace = T)
)
df_tmp <- reactive({
var_user <- "A" #session$user
df %>% filter(var3 == var_user) %>% return()
})
```
### Filter
```{r}
radioButtons(label = h4("Choose data"),
inputId = "var1_filter",
choices = df$var1 %>% unique,
selected = df$var1 %>% unique %>% .[1])
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart B
```{r}
renderPlot({
df_tmp() %>% ggplot(aes(var2)) + geom_density()
})
```
See also:
Create an input variable that is dependent on another input variable in flexdashboard shiny widget
Related
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
)
})
```
I'm trying to build a panel where when selecting a State in a drop-down list the value of a valueBox dynamically change according to the column value in the database, but in every attempt I get an error return. Below, the code used:
library(flexdashboard)
library(shiny)
UF = c('AC', 'AM', 'AP', 'BA', 'CE', 'ES', 'PB', 'PE')
Column = c(30, 200, 7, 12, 854, 2, 78, 965)
df <- data.frame(UF,Coluna)
Row {data-width=200 .sidebar}
--------------------------------------------------------------
{r}
selectInput(inputId = "states",
label="Select State:",
choices = unique(df$UF),
selected = "",
multiple=FALSE
)
Row
-----------------------------------------------------------------------
{r}
renderValueBox({
b <- df %>%
filter(UF %in% input$states) %>%
select(df$Column)
valueBox(value = b, icon = "fa-users")
})
It turns out you have a couple of mistakes here:
You have some misspelling of variable names.
You didn't load all the required libraries.
You had the wrong {dplyr} grammar for select.
After fixing everything, here is the working code:
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r}
library(flexdashboard)
library(shiny)
library(magrittr)
library(dplyr)
```
```{r}
UF = c('AC', 'AM', 'AP', 'BA', 'CE', 'ES', 'PB', 'PE')
Column = c(30, 200, 7, 12, 854, 2, 78, 965)
df <- data.frame(UF,Column)
```
Row {data-width=200 .sidebar}
--------------------------------------------------------------
```{r}
selectInput(inputId = "states",
label="Select State:",
choices = unique(df$UF),
selected = "",
multiple=FALSE
)
```
Row
-----------------------------------------------------------------------
```{r}
renderValueBox({
b <- df %>%
filter(UF %in% input$states) %>%
select(Column)
valueBox(value = b, icon = "fa-users")
})
```
My goal is to get a value box to update using some inputs I have.
Here is my code
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
graphics: yes
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(plotly)
library(DT)
library(htmltools)
library(readr)
library(shiny)
library(knitr)
```
Inputs {.sidebar data-width=300}
=====================================
```{r}
selectInput("input_type","Select Cylinder Size: ", c("All", mtcars$cyl))
selectInput("input_type2", "Select # of Gears: ", c("All", mtcars$gear))
mtcars2 <- reactive({
d <- mtcars
if(input$input_type !="All")
d <- subset(d, cyl == input$input_type)
if(input$input_type2 !="All")
d <- subset(d, gear == input$input_type2)
d
})
```
# Page 1
Column {data-width=600}
-----------------------------------------------------------------------
### Table
```{r echo=FALSE}
renderDataTable(
datatable(mtcars2())
)
```
Column {data-width=300}
-----------------------------------------------------------------------
### Value Box
```{r}
valueBox(0)
```
My goal is to get a value box where I can get the sum of hp depending on the remaining data that is filtered.
For example in this screenshot I filtered to only see cars that are 6 cyl and have 3 gears.
My goal would be to see the sum of hp populate on the value box.
Additional Code
renderValueBox({
valueBox(
mtcars2() %>% summarise(Sum=sum(hp)) %>% pull(Sum) ,
paste("Total HP:", input$input2)
)
})
Add
library(dplyr)
to your YAML header and then replace
valueBox(0)
with
renderValueBox({
valueBox(
"Total HP",
mtcars2() %>% summarise(Sum=sum(hp)) %>% pull(Sum)
)
})
or similar.
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())
)
)
I have a dataframe, please help me in executing this. The moment I check "HoltWinters" and press "Execute" button, dataframe "HW" should appear. I have tried half way. But need anyone help here please................................
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
---
library(flexdashboard)
library(readxl)
library(tidyverse)
library(shiny)
library(rhandsontable)
library(dplyr)
library(forecast)
library(fpp)
library("TTR")
x <- c(1:123)
x <- ts(x, start = c(2017, 23), end = c(2019, 39), frequency = 53)
x.hw <- HoltWinters(x)
HW <- forecast(x.hw, h = 6)
HW <- as.data.frame(HW)
Model Execution
Inputs {.sidebar}
radioButtons("r",h5("Models"),choices = list("Regression", "Arima","HoltWinters","Model4","Model5"),selected = "No", inline = F)
actionButton("a","Execute",icon = NULL)
Row {.tabset .tabset-fade}
HoltWinters
output$table1 <- renderRHandsontable({
eventReactive(input$a,{
rhandsontable(HW)
})
})
rHandsontableOutput("table1")
You should not use eventReactive but observeEvent instead (check here). Also, this condition should be outside of the output part: "if I observe this event, then I will display this table" (and not "I will display this table and then fill it according to which button is ticked").
Here's the solution to your problem (you should customize it so that just selecting HoltWinters displays the table but at least you have a working basis here):
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(readxl)
library(tidyverse)
library(shiny)
library(rhandsontable)
library(dplyr)
library(forecast)
library(fpp)
library(TTR)
```
```{r}
x <- c(1:123)
x <- ts(x, start = c(2017, 23), end = c(2019, 39), frequency = 53)
x.hw <- HoltWinters(x)
HW <- forecast(x.hw, h = 6)
HW <- as.data.frame(HW)
```
Model Execution
=================
Inputs {.sidebar}
-----------------------------------------------------------------------
```{r}
radioButtons("r",h5("Models"),choices = list("Regression", "Arima","HoltWinters","Model4","Model5"),selected = "No", inline = F)
actionButton("a","Execute",icon = NULL)
```
Row {.tabset .tabset-fade}
-------------------------------------
### HoltWinters
```{r}
observeEvent(input$a,{
output$table1 <- renderRHandsontable({
rhandsontable(HW)
})
})
rHandsontableOutput("table1")
```
Edit: you can add a condition within the observeEvent so that the table is displayed only if HoltWinters is ticked:
observeEvent(input$a,{
if (input$r == "HoltWinters") {
output$table1 <- renderRHandsontable({
rhandsontable(HW)
})
}
else {
output$table1 <- renderRHandsontable({
})
}
})
rHandsontableOutput("table1")