Using date in Shiny valueBox - shiny

How can I show the current date in the subtitle of a Shiny valueBox?
output$approvalBox <- renderValueBox({
valueBox("95",
"Number of School Days as of `r Sys.Date()`

You can use paste()
valueBox(value = "95",
subtitle = paste("Number of School Days as of", Sys.Date()))

Related

Dynamically changing a date slice from another slicer

I am trying to create a slicer in PowerBI that will dynamically change the FULL_DATE whenever the ListIDrop value changes. I have a unique ID with a date range that you can see here with the code below:
ListIDrop =
VAR ListID = 'Master Table'[List ID ]
VAR Start_Date = 'Master Table'[Start Date].[Date]
VAR End_Date = 'Master Table'[End Date].[Date]
RETURN ListID & " (" & FORMAT(Start_Date, "Short Date")&" - "&FORMAT(End_Date, "Short Date") & ")"
The FULL_DATE is a slicer that will allow the user to custom search between the dates. But I want the FULL_DATE slicer to change to the dates of the ListIDrop if the ListIDrop slicer changes.
This is the behavior I see. In this example, the FULL_DATE slicer should have 4/25/2022 - 5/1/2022 selected as the range.
Any help would be appreciated. Thank you!

Group_by, summarise reactivity not working in R Shiny

Reactivity is working everywhere else in my app, but when I try to create a summary statistic table the table returns the name of the variable selected instead of the summary statistic:
ui <- selectInput('summary_metric', 'Select Metric', choices = c('height', 'weight'))
tableOuput('summary_table')
server <- function(input, output) {
output$summary_table <- renderTable({ data %>% group_by(Age_Group) %>%
summarise(min = min(input$summary_metric),
median = median(weight))})
Returns a table that reads:
Age_Group
min
median
18-29
weight
170
30-39
weight
180
40-49
weight
190
when 'weight' is selected in the UI and toggles to 'height' in the min column when 'height' is selected in the UI. So instead of calculating the minimum, it is just returning the variable name. Any thoughts as to what I'm missing?

Reactive coloring in DT table

Need help. Below is the application. ColB is to be colored based on numbers in ColA. Below is the condition table
Just to brief : if ColB is 2, So it is lesser than 6 (12/2), it should be red and similarly for others. I tried to build the code myself and came up with below . But looks like there is some issue in the code . I have attached the output also below and the logic is not working properly.
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(DT)
```
```{r}
tab1 <- data.frame(ColA = c(12,34,45,56), ColB = c(2,32,30,56))
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
DT::DTOutput("table1")
output$table1 <- DT::renderDT(
datatable(tab1))
```
Below is the output I got
So As per the code, the highlighted arrows are showing color.
First Arrow (Supposed to be red but it is showing as Yellow)
Second Arrow (Supposed to be Yellow but it is showing as Green)
Note : ColB is randomly generated and you may not see these numbers when you run it. But you observe randomly, this issue you will find for sure when you run as well. Not sure what is wrong in the code . Below is the code for your reference
You could use formatStyle from DT (link):
---
title: "Untitled"
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(DT)
```
```{r}
tab1 <- data.frame(ColA = c(12,34,45,56), ColB = c(2,32,30,56)) %>%
dplyr::mutate(backgroundColB = case_when(
ColA==ColB ~ 1,
ColA/2>ColB ~ -1,
ColA/2<ColB ~ 0
))
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
DT::DTOutput("table1")
output$table1 <- DT::renderDT(
datatable(tab1, options=list(columnDefs = list(list(visible=FALSE, targets=2)))) %>%
formatStyle('ColB', "backgroundColB",
backgroundColor = styleInterval(c(-.5,.5), c("red","yellow","green") ))
)
```

Stata: svy:mean affecting variable format

A code I am running uses svy:mean and there is NO subpop command used.
My issue is that is that for certain variables, it renames some of the values of the variable to _subpop_1, etc. but others are still in their original format. For example, I have a county variable. After using the svy:mean command, some counties show up as Alameda, Alpine, etc) while some show up as _subpop_7, _subpop_8, etc.
Does anyone know why this is?
When using a tab command on the same variable, none of the formats are affected and every county shows up.
An example of my code and output (I hid the numbers) would be:
foreach var of varlist county {
svy: mean deport, over(`var')
}
Survey: Mean estimation
Number of strata = . Number of obs = .
Number of PSUs = . Population size = .
Design df = .
ALAMEDA: county = ALAMEDA
ALPINE: county = ALPINE
AMADOR: county = AMADOR
BUTTE: county = BUTTE
CALAVERAS: county = CALAVERAS
COLUSA: county = COLUSA
_subpop_7: county = CONTRA COSTA
_subpop_8: county = DEL NORTE
_subpop_9: county = EL DORADO
FRESNO: county = FRESNO
GLENN: county = GLENN
HUMBOLDT: county = HUMBOLDT
IMPERIAL: county = IMPERIAL
More than a programming problem, this is simply a case of Stata doing what it states it'll do. From help mean:
Noninteger values,
negative values, and labels that are not valid Stata names are substituted with a default identifier.
An example reproducing the "problem" is:
webuse hbp
// some value labels with spaces
label define lblcity 1 "contra costa" 2 "el dorado" 3 "alameda" 5 "alpine"
label values city lblcity
mean hbp, over(city)
More on valid Stata names in [U] 11 Language syntax.
(Note the svy : prefix plays no role here.)

Customizing week number in R shiny

I am trying to build a shiny application where the output will say "The current week is x" where x is the week number. The problem in this case is my year starts on 3/30/2014 and I have defined a week to be from Sunday to Saturday which I am unable code properly resulting in erroneous output. I am attaching the code below. Any help will be greatly appreciated.
ui.R
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
dateInput('Start_Date',label = "Choose Date",value = Sys.Date())
),
mainPanel(
textOutput("text1")
),
)
))
server.R
library(shiny)
shinyServer(function(input, output) {
output$text1<-renderText({
paste("The current week is",ceiling(abs(difftime(as.Date("3/30/2014","%m/%d/%y"),as.Date(input$Start_Date),by="weeks"))/7))
})
})
I think you had small problem with formatting. I have added the start the day from which the year start too (so if you want your count to start from Sunday you can specify) so you can change it if you want.
rm(list = ls())
library(shiny)
ui = fluidPage(
sidebarLayout(
sidebarPanel(
dateInput('Year_starts',label = "Count From",value = as.Date("2014/03/30")),
dateInput('Start_Date',label = "Choose Date",value = Sys.Date())
),
mainPanel(
textOutput("text1")
),
)
)
server = function(input, output) {
output$text1<-renderText({
dates <- seq(input$Year_starts, as.Date(input$Start_Date), by = "weeks")
length(dates)-1
})
}
runApp(list(ui = ui, server = server))