R shiny dashboard infobox with a dataset input - r

I am new to r and shiny and suspect that I am stuck with a simple problem.
I want 2 infoboxes which show me in one the maximum amount over all categories and in the second infobox only the category with the most amount and its total amount.
I have tried a lot of things but nothing brought me success.
## app.R ##
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
uiOutput("info_box1"),
uiOutput("info_box2"),
uiOutput("rawdata")
)
)
set.seed(24)
mydf <- data.frame(Type = sample(LETTERS[1:5], 30, replace = TRUE),
Amount = sample(10:200, 30, replace = TRUE),
stringsAsFactors= FALSE, check.names = FALSE)
server <- function(input, output) {
output$info_box1 <- renderUI({
infoBox("Amount in Total here", input$ "???")
})
output$info_box2 <- renderUI({
infoBox("Class with the hightest amount and amount in total of that class", "input$ function needed?")
})
output$rawdata = renderTable({
mydf
})
}
# Run the application
shinyApp(ui = ui, server = server)
Could someone please show me how to do this?
Thanks a lot. Appreciate your help.

You should use approriate functions, see here : https://rstudio.github.io/shinydashboard/structure.html
app.R
library(shiny)
library(shinydashboard)
library(dplyr)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
infoBoxOutput("info_box1"),
infoBoxOutput("info_box2"),
tableOutput("rawdata")
)
)
set.seed(24)
mydf <- data.frame(Type = sample(LETTERS[1:5], 30, replace = TRUE),
Amount = sample(10:200, 30, replace = TRUE),
stringsAsFactors= FALSE, check.names = FALSE)
server <- function(input, output) {
output$info_box1 <- renderInfoBox({
infoBox("Amount in Total here", sum(mydf$Amount))
})
output$info_box2 <- renderInfoBox({
df_output <- mydf %>% group_by(Type) %>% tally()
infoBox("Class with the hightest amount and amount in total of that class", paste(df_output$Type[df_output$n == max(df_output$n)],max(df_output$n)) )
})
output$rawdata = renderTable({
mydf
})
}
# Run the application
shinyApp(ui = ui, server = server)

Related

How to display data frame for calculated difference between times?

I'm a beginner, which is worth mentioning at the beginning. I wanted to create an application in which the user enters an arbitrary date and, based on it, calculates the time between that date and the last activity of a person from the data table. if that time is greater than the value of input$disabled, which is the expected time of being offline, I would like to display all the information about those people. currently, I'm getting the following problem: Error in UseMethod: no applicable method for 'filter' applied to an object of class "c('reactiveExpr', 'reactive', 'function')" and for the second one: Error in UseMethod: no applicable method for 'mutate' applied to an object of class "difftime".
last_event has a character type and looks like this: "2019-12-22 00:00:0", which is why I overlay as.Date() on it. I am not sure if I used reactive() correctly, because this application is a work of trial and error. Can someone help me? I would be very grateful, because it is kinda frustrating for me right now.
last_event <- c("2019-12-26 00:00:00","2020-10-21 00:00:00","2020-05-27 00:00:01","2020-02-25 00:00:00","2020-10-09 00:00:00","2020-10-16 00:00:00","2019-12-01 00:00:01")
id <- c(1:7)
users_name <- c("Krox", "Minit", "Brulon", "Loc", "Mese02", "Robu78", "CoffeeMan")
data <- data.frame(id, users_name, last_event)
ui <- dashboardPage(
dashboardHeader(title = "X"),
dashboardSidebar(
dateInput(inputId = "date", label = "Podaj date : "),
sliderInput(inputId = "disabled", label = "Czas nieaktywnosci (msc): ", min = 6, max = 24, value = 12)),
dashboardBody(
dataTableOutput(outputId = "table")
))
server <- function(input, output){
reactive_data <- eventReactive(input$date, {
mutate(diff = (difftime(as.Date(input$date), as.Date(data$last_event)))/30)
data
})
output$table <- renderDataTable({
data <- reactive_data()
if (input$disabled){
data %>%
filter(diff > input$disabled)
}})
}
shinyApp(ui, server)
Also I try to write this code in another way:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(dplyr)
data <- read_excel(file.choose())
ui <- dashboardPage(
dashboardHeader(title = "X"),
dashboardSidebar(
dateInput(inputId = "date", label = "Podaj date : "),
sliderInput(inputId = "disabled", label = "Czas nieaktywnosci (msc): ", min = 6, max = 24, value = 12)),
dashboardBody(
dataTableOutput(outputId = "table")
))
server <- function(input, output){
data1 <- reactive(data %>%
mutate(diff = (difftime(as.Date(input$date)) - as.Date(data1$last_event))/30))
output$table <- renderDataTable(data1 %>%
filter(diff > input$disabled))
}
shinyApp(ui, server)
Please try the below:
last_event <- c("2019-12-26 00:00:00","2020-10-21 00:00:00","2020-05-27 00:00:01","2020-02-25 00:00:00","2020-10-09 00:00:00","2020-10-16 00:00:00","2019-12-01 00:00:01")
id <- c(1:7)
users_name <- c("Krox", "Minit", "Brulon", "Loc", "Mese02", "Robu78", "CoffeeMan")
data <- data.frame(id, users_name, last_event)
ui <- dashboardPage(
dashboardHeader(title = "X"),
dashboardSidebar(
dateInput(inputId = "date", label = "Podaj date : "),
sliderInput(inputId = "disabled", label = "Czas nieaktywnosci (msc): ", min = 6, max = 24, value = 12)),
dashboardBody(
dataTableOutput(outputId = "table")
))
server <- function(input, output){
reactive_data <- eventReactive(input$date, {
mutate(data, diff = (difftime(as.Date(input$date), as.Date(data$last_event)))/30)
})
output$table <- renderDataTable({
data <- reactive_data()
if (input$disabled){
data %>%
filter(diff > input$disabled)
}})
}
shinyApp(ui, server)
This line needed work:
mutate(data, diff = (difftime(as.Date(input$date), as.Date(data$last_event)))/30)
Alternatively:
data %>% mutate(diff = (difftime(as.Date(input$date), as.Date(data$last_event)))/30)
Update
for your second code example, see here:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(dplyr)
data <- read_excel(file.choose())
ui <- dashboardPage(
dashboardHeader(title = "X"),
dashboardSidebar(
dateInput(inputId = "date", label = "Podaj date : "),
sliderInput(inputId = "disabled", label = "Czas nieaktywnosci (msc): ", min = 6, max = 24, value = 12)),
dashboardBody(
dataTableOutput(outputId = "table")
))
server <- function(input, output){
data1 <- reactive(data %>%
mutate(diff = (difftime(as.Date(input$date), as.Date(data$last_event))/30)))
output$table <- renderDataTable(data1() %>%
filter(diff > input$disabled))
}
shinyApp(ui, server)
Actually I solved this problem by myself, but thank you all for answers. The final code looks like that:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(readxl)
library(dplyr)
library(DT)
ui <- dashboardPage(
dashboardHeader(title = "X"),
dashboardSidebar(
dateInput(inputId = "date", label = "Choose date : "),
sliderInput(inputId = "disabled", label = "Choose time of inactivity: ", min = 6, max = 24, value = 12)),
dashboardBody(
dataTableOutput(outputId = "table")
))
server <- function(input, output){
data1 <- reactive(read_excel(file.choose()))
output$table <- renderDT({
data1() %>%
mutate(diff = (difftime(input$date, as.Date(last_event))/30)) %>%
filter(diff > input$disabled) %>%
select(-diff) %>%
DT::datatable(options(list(scrollX=TRUE)))
})
}
shinyApp(ui, server)

How do I get aggregated data for selected attributes from shiny app?

It would be great someone can help to solve below criteria.
Requirement details:-
How to pass multiple attributes dynamically to group_by/summaries clause to get aggregated data for selected attributes? in my case I am able to achieve the same by using below code, but it was restricted to 1 group by attribute and summary attribute. If I select multiple group by or summary attributes, it's throwing error.
library(dplyr)
library(data.table)
library(shiny)
library(DT)
df1 <- data.frame("name"=c("AAA","BBB","CCC"),"dept"=c("HR","HR","FIN"),"Salary"=c(1000,1345,5678),"Salary2"=c(4567,7896,5678))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput("Id0001","group by attributes",choices = unique(names(df1)),multiple = TRUE,options = list(`live-search` = TRUE)),
pickerInput("Id0002","summary attributes",choices = unique(names(df1)),multiple = TRUE,options = list(`live-search` = TRUE)),
actionButton("Id0003", "show data")
),
mainPanel( DT::DTOutput("data_tbl") )
)
)
server <- function(input,output,session){
reactive_string <- eventReactive(input$Id0003, {
if (input$Id0003 > 0) {
dt_agg_ui <<- df1 %>%
group_by(!!rlang::sym(input$Id0001)) %>%
summarise_at(vars(!!rlang::sym(input$Id0002)),funs(sum,n()))
}
dt_agg_ui
})
output$data_tbl <- DT::renderDT( {reactive_string()})
}
shinyApp(ui = ui, server = server)
Using dplyr::across and tidyselect::all_of this could be achieved like so:
library(dplyr)
library(data.table)
library(shiny)
library(shinyWidgets)
library(DT)
df1 <- data.frame("name"=c("AAA","BBB","CCC"),"dept"=c("HR","HR","FIN"),"Salary"=c(1000,1345,5678),"Salary2"=c(4567,7896,5678))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput("Id0001","group by attributes",choices = unique(names(df1)),multiple = TRUE,options = list(`live-search` = TRUE)),
pickerInput("Id0002","summary attributes",choices = unique(names(df1)),multiple = TRUE,options = list(`live-search` = TRUE)),
actionButton("Id0003", "show data")
),
mainPanel( DT::DTOutput("data_tbl") )
)
)
server <- function(input,output,session){
reactive_string <- eventReactive(input$Id0003, {
if (input$Id0003 > 0) {
dt_agg_ui <<- df1 %>%
group_by(across(all_of(input$Id0001))) %>%
summarise(across(all_of(input$Id0002), .fns = list(sum = sum, n = ~ n())))
}
dt_agg_ui
})
output$data_tbl <- DT::renderDT( {reactive_string()})
}
shinyApp(ui = ui, server = server)

Reactive select input to update table

I am trying to understand the reactive part in R shiny. In that process I am trying to update an output table based on the input change while selecting values from the age drop down. It seems to do it by the first value but when I change any value from the age drop down it won't update my table. The input I am using is chooseage. Below is the code which I am using.
library(shiny)
library(shinydashboard)
library(shinyBS)
library(knitr)
library(kableExtra)
library(shiny)
library(shinythemes)
ui <- dashboardPage(
dashboardHeader(disable = F, title = "PATH Study"),
dashboardSidebar(sidebarMenu(
menuItem(
"Population Filter",
uiOutput("choose_age")
)
)),
dashboardBody(box(
width = 12,
tabBox(
width = 12,
id = "tabBox_next_previous",
tabPanel("Initiation",
fluidRow(
box(
width = 5,
solidHeader = TRUE,
status = "primary",
tableOutput("smoke"),
collapsible = F
)
))
),
uiOutput("Next_Previous")
))
)
server <- function(input, output, session) {
# Drop-down selection box for which Age bracket to be selected
age_levels <- c("18 to 24 years old","25 to 34 years old","35 to 44 years old")
output$choose_age <- renderUI({
selectInput("selected_age", "Age", as.list(age_levels))
})
myData <- reactive({
with_demo_vars %>%
filter(age == input$choose_age) %>%
pct_ever_user(type = "SM")
})
output$smoke <-
renderTable({
head(myData())
})
}
shinyApp(ui = ui, server = server)
Here is a quick prototype for your task
library(shiny)
library(tidyverse)
library(DT)
# 1. Dataset
df_demo <- data.frame(
age = c(16, 17, 18, 19, 20),
name = c("Peter", "Mary", "Mike", "Nick", "Phillipe"))
# 2. Server
server <- function(input, output, session) {
# 1. UI element 'Age'
output$ui_select_age <- renderUI({
selectInput("si_age", "Age", df_demo$age)
})
# 2. Reactive data set
df_data <- reactive({
# 1. Read UI element
age_selected <- input$si_age
# 2. Filter data
df <- df_demo %>%
filter(age == age_selected)
# 3. Return result
df
})
# 3. Datatable
output$dt_table <- renderDataTable({
datatable(df_data())
})
}
# 3. UI
ui <- fluidPage(
fluidRow(uiOutput("ui_select_age")),
fluidRow(dataTableOutput("dt_table"))
)
# 4. Run app
shinyApp(ui = ui, server = server)
I think youre shinyApp is over-reactive, as all functions in the server are executed straight away, without waiting for any selected input. So either it will break down or behave weird. So you have to delay the reactivity with req(), validate() / need() or with any observeEvent or eventReactive() function.
Maybe this snippet might help you, although there would be several ways to achieve the desired behaviour.
library(shiny)
library(shinydashboard)
library(dplyr)
data(mtcars)
mtcars$age <- sample(x = c(10,20,30,40,50), size = nrow(mtcars), replace = T)
with_demo_vars <- mtcars
ui <- dashboardPage(
dashboardHeader(disable = F, title = "PATH Study"),
dashboardSidebar(sidebarMenu(
menuItem(text = "Population Filter",
uiOutput("choose_age")
)
)
),
dashboardBody(
tableOutput("smoke")
)
)
server <- function(input, output, session) {
output$choose_age <- renderUI({
selectInput("selected_age", "Age", with_demo_vars$age)
})
myData <- reactive({
with_demo_vars %>%
dplyr::filter(age == input$selected_age)
})
output$smoke <- renderTable({
req(input$selected_age)
head(myData())
})
}
shinyApp(ui = ui, server = server)

Show pop up from Datatable in shiny with subset of other dataframe

I have a shiny app that displays a number of products depending on the search.
It subsets a large dataset and shows me the products I want that match.
I have another dataframe that has the reviews of said products.
I would like that when a specific row is clicked the review information appears in a different datatable.
The second datatable also needs to be a subset based on the Catalog Number (here is Acolumn.
All help appreciated.
df1 <- data.frame(A= c("BX002","BX006", "BX008"),
B= c("Actin","Tubulin", "GAPDH"),
C = c("Mouse","Human", "Human"),
D = c("WB","WB", "IHC"))
df2 <- data.frame(A= c("BX002","BX006", "BX008"),
B= c("Actin","Tubulin", "GAPDH"),
C = c("5","5", "4"),
D = c("Good","Good", "Bad"),
E = c("Kidney", "Liver", "Heart"))
library(shinydashboard)
library(shiny)
app <- shinyApp(
ui = dashboardPage(
dashboardHeader(
title = "Search"),
dashboardSidebar(
sidebarMenu(
menuItem("Search Product", tabName = "product", icon = icon("search")))),
dashboardBody(
tabItems(
tabItem("product",
fluidPage(
sidebarLayout(
sidebarPanel(textInput("name", "Protein name", value = ""),
submitButton("Search")),
mainPanel(
tabsetPanel(
tabPanel("Products", dataTableOutput("table1")))))))))),
server = shinyServer(function(input, output, session) {
output$table1 <- DT::renderDataTable({
validate(need(input$name != "", "Please select a Protein Name"))
search <- input$name
df <- subset(df1, grepl(search, df1$B, ignore.case = TRUE)==TRUE)
datatable(df, escape = FALSE, selection = "single")
})
observeEvent(input$table1_rows_selected,
{
df <- subset(df2, df2$A == input$table1_rows_selected$A)
showModal(modalDialog(
title = "Reviews",
df
))
})
})
)
I have tried a few methods but cant make it work.
This is my last atempt, no box popup, no error message nothing.
best
I took some liberties with your formatting to make it simpler to recreate.
This should give you the jist of how the modalDialogfunction works with DT.
Here's a working example:
library(shiny)
library(DT)
ui <- fluidPage(
textInput('name','Protein Name'),
dataTableOutput('table1')
)
server <- function(input, output,session) {
df1 <- data.frame(A= c("BX002","BX006", "BX008"),
B= c("Actin","Tubulin", "GAPDH"),
C = c("Mouse","Human", "Human"),
D = c("WB","WB", "IHC"))
df2 <- data.frame(A= c("BX002","BX006", "BX008"),
B= c("Actin","Tubulin", "GAPDH"),
C = c("5","5", "4"),
D = c("Good","Good", "Bad"),
E = c("Kidney", "Liver", "Heart"))
tbl1 <- reactive({
if(nchar(input$name)>0){
df1[which(tolower(input$name) == tolower(df1$B)),]
}
})
output$table1 <- renderDataTable({
if(nchar(input$name)>0){
datatable(tbl1(),selection = 'single')
}
})
review_tbl <- reactive({
df2[which(df1[input$table1_rows_selected,1]==df2$A),]
})
observeEvent(input$table1_rows_selected,{
showModal(
modalDialog(
renderDataTable({
review_tbl()
})
))
})
}
# Run the application
shinyApp(ui = ui, server = server)

How to show list of countries which something more than EDITED

EDITED
I would like to ask you how to do that simple thigs.
I want to make Shiny Web application that get from interface ammount of something (inputId=num), and show the table with countries that
data$both>num
I made some code, but it doesnt work.
library(shiny)
ui <- fluidPage(
sliderInput(inputId = 'num',label = 'Countries that...',min = '1',max =189',value = '20',step = '1')
tableOutput(outputId = 'liczba')
)
server <- function(input, output) {
output$liczba <- renderTable({
data[data$both>input$num,]
})
}
shinyApp(ui=ui, server=server)
Here's a possible solution based on your code. There are several small errors in your implementation. Please take a look here for a set of very good examples.
library(shiny)
library(DT)
data <- data.frame(
country = c("Germany", "Netherlands", "Canada"),
male = c(15, 30, 45)
)
ui <- fluidPage(
fluidRow(
sliderInput(
inputId = 'num',
label = 'Countries that...',
min = 1,
max = 189,
value = 20,
step = 1
)
),
fluidRow(
DT::dataTableOutput("liczba")
)
)
server <- function(input, output) {
output$liczba <- DT::renderDataTable(DT::datatable({
result <- subset(
data,
data$male > input$num
)
result
}))
}
shinyApp(ui = ui, server = server)

Resources