How to make a dynamic valueBox in Flexdashboards in R - r

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")
})
```

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

Reactive Shiny flexdashboard Dynamic Inputs

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

How can I plot a selection from a Shiny selectInput widget in ggplot using flexdashboard

It is my understanding Shiny's selectInput widget returns a character vector. I am unclear as to why the user input cannot be passed to ggplot as a column name. Naturally this fails to plot as desired.
This is my yaml.
[![enter image description here][1]][1]
Blockquote ---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: rows
runtime: shiny
library(tidyverse)
library(flexdashboard)
library(shiny)
spy <- tibble(Date = as.Date(c("2020-12-28", "2021-12-30", "2022-03-15")),
Happy = c(8.25, 12.48, 11.52 ),
Glad = c(4, 5, 7 ) )
shiny::selectInput(
inputId = "select_data",
label = "Select Data",
choices = c("Happy", "Glad"),
selected = "Happy"
)
renderPlot({
ggplot(spy, aes( 1:3, input$select_data) ) +
geom_line()
})
[1]: https://i.stack.imgur.com/sHEWu.png
[2]: https://i.stack.imgur.com/rIWrY.png

Plotting after Checking the radio Button and Pressing action Button

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")

Render DT::datatable in browser using flexdashboard with shiny

I am trying to build an interactive table in flexdashboard with Shiny. The following reprex displays the table in the Rstudio viewer but the CSV button does not work. When attempting to open in a local browser the table is not displayed.
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(DT)
library(shiny)
df <- data_frame(
x = c("a", "b", "c", "c", "b", "c", "b", "a"),
y = c(1, 6, 7, 5, 9, 3, 8, 2),
z = c("one", "two", "three", "one", "two", "three", "one", "two")
)
```
Column {data-width=200 .sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("x_input", label = "Select x Value",
choices = c("All", unique(as.character(df$x))), selected = "All")
roster_input <- reactive({
if (input$x_input=="All"){
df
}else{
df %>%
filter(x==input$x_input)}
})
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart B
```{r}
renderDataTable({
DT::datatable(
roster_input(),
rownames = FALSE,
filter = "top",
extensions = c('Buttons', 'Scroller'),
options = list(
dom = 'Blfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
deferRender = T,
scrollY = '400px',
scroller = TRUE))})
```
What am I missing to make this work in my local browser?

Resources