I'm trying to create a dashboard using Shiny. Here is some sample data:
###Creating Data
name <- c("Sharon", "Megan", "Kevin")
x <- c(5, 7,3)
y <- c(3,6,2)
z <- c(2,3,7)
jobForm = data.frame(name, x, y, z)
What I'm trying to figure out is, for every row of names how do I create their own TABLE? I believe there is a way to create a reactive for-loop but I've been at this for a long time and have given up.
Here is the full code of what the dashboard should look like for each name. This code only shows Sharon's scores, and it should run. If there are any issues on getting the code to run completely let me know.
I am using
packages shiny, shinydashboard and tidyverse
##Dashboard Header
header <- dashboardHeader(
title = "My Project")
##Dashboard Sidebar
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", icon = icon("dashboard"),tabName = "dashboard"),
menuItem("Job Positions", icon = icon("address-card"), tabName = "jobposition",
menuSubItem('Sales',
tabName = 'sales',
icon = icon('line-chart'))
)
)
)
##Dashboard Body
body <- dashboardBody(
tabItems(
# Dashboard Tab Content
tabItem(tabName = "dashboard",
fluidRow(
#Random Plot
box( )
)
),
# Associate Tab Content
tabItem(tabName = "sales",
fluidRow(
#Main Box for Candidate
box(
width = 8,
title = "Candidate 001",
status = "primary",
#Box for Table
box(
title = "Table",
status = "info",
tableOutput("stat1")
)
)
)
)
)
)
##User Interface Using Dashboard Function
ui <- dashboardPage(
skin = "yellow",
header,
sidebar,
body
)
##Server: Instructions
server <- function(input, output) {
temp <- data.frame(jobForm %>%
slice(1) %>%
select(x:z))
temp <- as.data.frame(t(temp))
output$stat1 <-renderTable({
temp
},
include.rownames=TRUE,
colnames(temp)<-c("Score")
)
}
##Create Shiny App Object
shinyApp(ui, server)
Thank you for any help
You better solve these kibnd of problems with an renderUI and since you never really know when shiny will evaluate an expression you are much better of using lapply then for loops.
name <- c("Sharon", "Megan", "Kevin")
x <- c(5, 7,3)
y <- c(3,6,2)
z <- c(2,3,7)
jobForm = data.frame(name, x, y, z)
##Dashboard Header
header <- dashboardHeader(
title = "My Project")
##Dashboard Sidebar
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", icon = icon("dashboard"),tabName = "dashboard"),
menuItem("Job Positions", icon = icon("address-card"), tabName = "jobposition",
menuSubItem('Sales',
tabName = 'sales',
icon = icon('line-chart'))
)
)
)
##Dashboard Body
body <- dashboardBody(
tabItems(
# Dashboard Tab Content
tabItem(tabName = "dashboard",
fluidRow(
#Random Plot
box( )
)
),
# Associate Tab Content
tabItem(tabName = "sales",
fluidRow(
#Main Box for Candidate
uiOutput("candidates")
)
)
)
)
##User Interface Using Dashboard Function
ui <- dashboardPage(
skin = "yellow",
header,
sidebar,
body
)
##Server: Instructions
server <- function(input, output) {
temp <- data.frame(jobForm %>%
slice(1) %>%
select(x:z))
temp <- as.data.frame(t(temp))
output$stat1 <-renderTable({
temp
},
include.rownames=TRUE,
colnames(temp)<-c("Score")
)
output$candidates <- renderUI(
tagList(
lapply(1:nrow(jobForm), function(idx){
output[[paste0("stat",idx)]] <- renderTable(
jobForm[idx,-1]
)
box(
width = 8,
title = paste0("Candidate: ",jobForm$name[idx]),
status = "primary",
#Box for Table
box(
title = "Table",
status = "info",
tableOutput(paste0("stat",idx))
)
)
})
)
)
}
##Create Shiny App Object
shinyApp(ui, server)
Hope this helps!!
Related
I have to create a Shiny/ShinyDashboard app which basically creates a bunch of tables for various teams. Users will select their team from the sidebar and then they will have several tab panels to choose from depending on the data. See here:
Now the requirement is that I have to split the data for each tab panel into distinct datatables and -because of the data- I have to generate this dynamically.
I came up with the following code (reprex down here) but since I'm quite new to Shiny, I wondered if:
I could split UI and data code even more
there is frankly a better way to do this
library(shiny)
library(shinydashboard)
library(datasets)
library(dplyr)
library(DT)
cars <- mtcars
irises <- iris
cars$team <- sample(c("Team1", "Team2"), nrow(cars), replace = TRUE)
irises$team <-
sample(c("Team1", "Team2"), nrow(irises), replace = TRUE)
# UI
ui <- dashboardPage(
dashboardHeader(title = "Teams"),
dashboardSidebar(sidebarMenu(
menuItem("Team 1",
tabName = "tab_team1",
icon = icon("dashboard")),
menuItem("Team 2",
tabName = "tab_team2",
icon = icon("dashboard"))
)),
dashboardBody(tabItems(
tabItem(tabName = "tab_team1",
fluidRow(
tabBox(
title = "",
width = "100%",
tabPanel(title = "A",
uiOutput("Team1_content_A")),
tabPanel(title = "B",
uiOutput("Team1_content_B"))
)
)),
tabItem(tabName = "tab_team2",
fluidRow(
tabBox(
title = "",
width = "100%",
tabPanel(title = "A",
uiOutput("Team2_content_A")),
tabPanel(title = "B",
uiOutput("Team2_content_B"))
)
))
))
)
server <- function(input, output, session) {
lapply(1:2, function(i) {
t <- paste0("Team", i)
table <- cars %>%
filter(team == t)
output[[paste0(t, "_content_A")]] <- renderUI({
lapply(sort(unique(table$gear)), function(i) {
id <- paste0(t, "_content_A_", i)
output[[id]] <-
DT::renderDataTable(datatable(table[table$gear == i, ]))
fluidRow(
box(
width = "100%",
title = paste0("Gears: ", i),
status = "info",
solidHeader = TRUE,
collapsible = TRUE,
DT::dataTableOutput(id)
)
)
})
})
table2 <- irises %>%
filter(team == t)
output[[paste0(t, "_content_B")]] <- renderUI({
lapply(sort(unique(table2$Species)), function(i) {
id <- paste0(t, "_content_B_", i)
output[[id]] <-
DT::renderDataTable(datatable(table2[table2$Species == i, ]))
fluidRow(
box(
width = "100%",
title = paste0("Species: ", i),
status = "info",
solidHeader = TRUE,
collapsible = TRUE,
DT::dataTableOutput(id)
)
)
})
})
})
}
shinyApp(ui, server)
Echo to #Limey, I would also suggest to use shiny modules https://mastering-shiny.org/scaling-modules.html. There are two reasons.
Reduce unnecessary computation. Currently the computation is run for all the four panels (team1_tabA, team1_tabB, team2_tabA, team2_tabB) at the same time. Ideally, as you add more features or data in the future, you would want to only run the necessary computation when certain action is performed. (i.e. when user click team1_tabA, only the required tables is calculated, no need to calculate tables for other tabs.). Modules can help achieve it.
More flexible control over UI and Server. Currently your app has the same server function and outputs for all the four panels, it works for now. But if in the future you want the four panels to have different layout and outputs, the current coding style might prompt you to write more complex and repeated code. And modules can help you get rid of the repeat and help with more flexible control over the UI and server.
Here is a modularized version of your shiny app. I encountered some issues with using namespace (NS(id)) in the dynamic UI (renderUI), and thanks to the feedback from #YBS Why the shiny dynamic UI + modules does not give the desired output?, the problem is solved, and the modularized shiny is able to run.
## module UI
tab_ui <- function(id) {
ns <- NS(id) ## namespace function
uiOutput(ns("content"))
}
## module Server
tab_server <- function(id, data, Team, var) {
moduleServer(id, function(input, output, session) {
ns <- session$ns ## call namespace in the server
table <- reactive({
data %>% filter(team == Team)
})
output$content <- renderUI({
lapply(sort(unique(table()[[var]])), function(i) {
idd <- paste0("content_", i)
output[[idd]] <-
DT::renderDataTable(datatable(table()[table()[[var]] == i, ]))
fluidRow(
box(
width = "100%",
title = paste0(var, " ", i),
status = "info",
solidHeader = TRUE,
collapsible = TRUE,
DT::dataTableOutput(ns(idd)) ## !!! need to use namespace
)
)
})
})
})
}
## library
library(shiny)
library(shinydashboard)
library(datasets)
library(dplyr)
library(DT)
## data
cars <- mtcars
irises <- iris
cars$team <- sample(c("Team1", "Team2"), nrow(cars), replace = TRUE)
irises$team <-
sample(c("Team1", "Team2"), nrow(irises), replace = TRUE)
## UI
ui <- dashboardPage(
dashboardHeader(title = "Teams"),
dashboardSidebar(sidebarMenu(
menuItem("Team 1",
tabName = "tab_team1"
),
menuItem("Team 2",
tabName = "tab_team2"
)
)),
dashboardBody(tabItems(
tabItem(
tabName = "tab_team1",
fluidRow(
tabBox(
title = "",
width = "100%",
tabPanel(
title = "A",
tab_ui("team1_tabA") ## module ui
),
tabPanel(
title = "B",
tab_ui("team1_tabB") ## module ui
)
)
)
),
tabItem(
tabName = "tab_team2",
fluidRow(
tabBox(
title = "",
width = "100%",
tabPanel(
title = "A",
tab_ui("team2_tabA") ## module ui
),
tabPanel(
title = "B",
tab_ui("team2_tabB") ## module ui
)
)
)
)
))
)
## server
server <- function(input, output, session) {
# module server
tab_server("team1_tabA", data = cars, Team = "Team1", var = "gear")
tab_server("team1_tabB", data = irises, Team = "Team1", var = "Species")
tab_server("team2_tabA", data = cars, Team = "Team2", var = "gear")
tab_server("team2_tabB", data = irises, Team = "Team2", var = "Species")
}
shinyApp(ui, server)
I am trying to make a shiny dashboard. I have two datasets, and based upon the selection of the datasets figures will be generate in the tab panels. However, by default only the last dataset that has been loaded/read is selected and I cannot select the first dataset. Even though I have made it default selection.
Below is my code.
library(shinydashboard)
library(uwot)
library(DESeq2)
library(gridExtra)
library(tidyverse)
library(RColorBrewer)
library(DESeq2)
library(pheatmap)
library(DEGreport)
library(vsn)
library(RColorBrewer)
library("genefilter")
library(org.Hs.eg.db)
library(dplyr)
library(tidyverse)
library(fgsea)
library(clusterProfiler)
library(ggplot2)
set_1<-load("C:/Users/abn/Documents/Shiny/DashBoardTutorial/TeData2.RData")
set_2<-load("C:/Users/abn/Documents/Shiny/DashBoardTutorial/TeData1.RData")
data_list = list(set_1=set_1,set_2=set_2)
ui <- dashboardPage(
dashboardHeader(title = "Data Visualizer", titleWidth = 300),
dashboardSidebar(
width = 300,
sidebarMenu(
menuItem("Datasets", icon = icon("cog"),
selectInput("Datasets", "Datasets:", choices = list("sample1" = "set_1", "sample2" = "set_2"),
selected = "set_1")),
menuItem("Quality Control", tabName = "widgets", icon = icon("th")),
menuItem("Differential Genes", tabName = "widgets2", icon = icon("th")),
menuItem("Downstream", tabName = "widgets3", icon = icon("th"))
)
),
## Body content
dashboardBody(
tabItems(
# First tab content
# Second tab content
tabItem(tabName="widgets",
h2("widgets"),
plotOutput("widgets"),
),
tabItem(tabName = "widgets2",
h2("Widgets2 tab content"),
),
tabItem(tabName = "widgets3",
h2("Widgets3 tab content"),
plotOutput("widgets3"),
)
)
)
)
server <- function(input, output) {
datasetInput <- reactive({
df <- data_list[[input$Datasets]]
})
output$widgets <- renderPlot({
datasetInput()
par(mfrow=c(1,2))
boxplot(counts(dds, normalized=F), outline=F, col=dds$condition, medcol = "white", cex.axis=0.6, main="Without Normalization")
boxplot(counts(dds, normalized=T), outline=F, col=dds$condition, medcol = "white", cex.axis=0.6, main="Normalized")
})
}
shinyApp(ui, server)
I am sure that I am missing a small trick, could anyone of you shiny masters help me out.
Or may be there is a better way to do the above procedure.
Many thanks in advance
Assuming you have access to both datasets, you plot them both and display the selection. Try this
data_list = list(set_1=mtcars,set_2=iris)
ui <- dashboardPage(
dashboardHeader(title = "Data Visualizer", titleWidth = 300),
dashboardSidebar(
width = 300,
sidebarMenu(
menuItem("Datasets", icon = icon("cog"),
selectInput("Datasets", "Datasets:", choices = list("sample1" = "set_1", "sample2" = "set_2"),
selected = "set_1")),
menuItem("Quality Control", tabName = "widgets", icon = icon("th")),
menuItem("Differential Genes", tabName = "widgets2", icon = icon("th")),
menuItem("Downstream", tabName = "widgets3", icon = icon("th"))
)
),
## Body content
dashboardBody(
tabItems(
# First tab content
# Second tab content
tabItem(tabName="widgets",
h2("widgets"),
#plotOutput("widgets"),
uiOutput("widgets")
),
tabItem(tabName = "widgets2",
h2("Widgets2 tab content"),
),
tabItem(tabName = "widgets3",
h2("Widgets3 tab content"),
plotOutput("widgets3"),
)
)
)
)
server <- function(input, output) {
# datasetInput <- reactive({
# df <- data_list[[input$Datasets]]
# })
#
# output$widgets <- renderPlot({
# datasetInput()
# par(mfrow=c(1,2))
#
# boxplot(counts(dds, normalized=F), outline=F, col=dds$condition, medcol = "white", cex.axis=0.6, main="Without Normalization")
# boxplot(counts(dds, normalized=T), outline=F, col=dds$condition, medcol = "white", cex.axis=0.6, main="Normalized")
#
# })
output$plot1 <- renderPlot({
boxplot(mpg ~ cyl , data=mtcars)
})
output$plot2 <- renderPlot({
boxplot(Sepal.Length ~ Species , data=iris)
})
output$widgets <- renderUI({
if (input$Datasets=="set_1") { plotOutput("plot1")
}else plotOutput("plot2")
})
}
shinyApp(ui, server)
Hi I am using shinydashboard to build some visualization for some raster files. I am use leafletOutput to display the map.
Under the first tabItem, where it is called 'KmeansOutput', I would like to display the leaflet map. When I do not include selectInput, it display the map, but once I include the selectInput, it do not display the map. I am not sure which part went wrong. Thanks in advance!!
Here is the UI section of the code:
library(shinydashboard)
library(leaflet)
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("KmeansOutput", tabName = "kmeans", icon = icon("kmeans"),
selectInput("run1",
"SoilAllWeatherAll",
choices = c('4' = 1, '5' = 2),
multiple = TRUE)
),
menuItem("HistoricalWeather", icon = icon("weather"), tabName = "weather"),
menuItem("SoilMap", icon = icon("soil"), tabName = "soil")
)
)
body <- dashboardBody(
tabItems(
tabItem(tabName = "kmeans",
leafletOutput("map", height = 700)
),
tabItem(tabName = "weather",
h2("weather")),
tabItem(tabName = "soil",
h2('soil'))
)
)
# Put them together into a dashboardPage
ui <- dashboardPage(
dashboardHeader(title = "Genome Prediction"),
sidebar,
body)
here is the server:
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles()
})
}
shinyApp(ui, server)
You need to add a sub-item to your k-means siderbar item as follows.
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("KmeansOutput", #icon = icon("kmeans"),
menuSubItem(
"K-Means Map", tabName = "kmeans", icon = icon("calendar")
),
selectInput("run1",
"SoilAllWeatherAll",
choices = c('4' = 1, '5' = 2),
multiple = TRUE)
),
menuItem("HistoricalWeather", tabName = "weather"), #icon = icon("weather"),
menuItem("SoilMap", tabName = "soil")#, icon = icon("soil")
)
)
DATA
I want to add text in rightmost region in the dashboard and the text should cover all the right space column.
dashboardPage(skin="yellow",
dashboardHeader(title = "Wheat Price dashboard ),
dashboardSidebar(
sidebarMenu(
menuItem("Punjab-khanna", tabName = "dashboard", icon = icon("area-chart"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
fluidPage(
titlePanel("Wheat DARA"),
mainPanel(fluidRow(
box( side="right",
tabPanel("Price chart", dygraphOutput("plot1")
)
),box(side = "right",height="250px",includeMarkdown("read.md")))
) )
)
))
)
SERVER.R
d1<-read_excel("data/Wheat data forecasted.xlsx",sheet = 1,col_names =
TRUE)
#stock
d2 <-subset(d1, select = c(1,2,3,4,5))
#last
d1 <-subset(d1, select = c(1,5,6,7))
d1$`Date GMT` <- as.POSIXct(d1$`Date GMT`, format = "%Y-%m-%d", tz="GMT")
ts1 <- irts(time=d1$`Date GMT`,value=as.matrix(d1[,2:4]))
#stock
d2$`Date GMT` <- as.POSIXct(d2$`Date GMT`, format = "%Y-%m-%d", tz="GMT")
ts2 <- irts(time=d2$`Date GMT`,value=as.matrix(d2[,2:5]))
shinyServer(function(input, output) {
output$plot1 <- renderDygraph({
dygraph(ts1) %>%
dyRangeSelector() %>%
dyLegend(show = "always", hideOnMouseOut = FALSE) %>%
dyHighlight(highlightCircleSize = 5) %>%
dyOptions(axisLineColor = "navy", gridLineColor = "grey")
})
} )
I am not able to arrange it to the right side.
NOTE:I have written different text(from the image) but the task is same to arrange the text to rightmost region in dashboard
I've added a minimal reproducible code myself. Please check. You just have to play with fluidrow and column with width values.
if(interactive()) {
## app.R ##
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
column(
column(
fluidRow(
box(plotOutput("plot1"))
),
fluidRow(
box(plotOutput("plot2"))
),
width = 10
),
column(
h3(
textOutput('text1')
),
width = 2
),
width = 12
)
)
)
)
server <- function(input, output) {
set.seed(122)
histdata <- rnorm(500)
output$plot1 <- renderPlot({
hist(histdata)
})
output$plot2 <- renderPlot({
hist(histdata)
})
output$text1 <- renderText({
"Uniform: These functions provide information about the uniform distribution on the interval from min to max. dunif gives the density, punif gives the distribution function qunif gives the quantile function and runif generates random deviates."
})
}
shinyApp(ui, server)
}
Source Code modified:
Please modify your dashboard input like this below. It also has plot2, since your initial question had one.
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
column(
column(
fluidRow(
tabPanel("Price chart", dygraphOutput("plot1")
),
fluidRow(
plotOutput("plot2")
),
width = 10
),
column(
h3(
includeMarkdown("read.md")
),
width = 2
),
width = 12
)
)
)
I am relative new to R and trying to learn on my own.
I want to create in a shiny dashboard a select-field where i can choose products of my Data (.xls) and get a sum returned.
The Input is via selectInput and selectize. This is the part, which works :)
If I choose 1 product i'll get the calories of this product back...so far.
My Problem is that wanna choose more products then 1 and get the sum of the calories. How do i have to identify/search the products of the input field in my table and how do i get the sum of it?
Thanks a lot for your help!
PS: Do you need further info about file? only two columns are important for this: product and calories.
library(dplyr)
library(plotly)
library(readxl)
library(shiny)
library(shinydashboard)
# Daten einlesen
McDaten <- read_excel("~/Desktop/McDaten.xlsx")
McDaten$kcal <- McDaten$`kcal (100g)`
ui <- dashboardPage(
skin="red",
dashboardHeader(title = "Analytics Dashboard", titleWidth = 290),
dashboardSidebar(
width = 290,
sidebarMenu(
menuItem("Virtuelles Menü", tabName = "charts", icon = icon("cutlery"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "charts",
fluidPage(
br(),
fluidRow(
column(4,
selectInput('in6', 'Menü', McDaten$Produkt, multiple=TRUE, selectize=TRUE)),
column(4,infoBoxOutput("progressBox"))
)
)
))))
server <- function(input, output) {
output$progressBox <- renderInfoBox({
b <- McDaten %>%
select(`kcal (Portion)`, Produkt) %>%
filter(McDaten$Produkt %in% input$in6) %>%
summarise(`kcal (Portion)`)
infoBox(
"Progress", paste0(b, " kcal"), icon = icon("list"),
color = "purple", fill = TRUE
)
})
}
shinyApp(ui, server)
We need the choices = unique(McDaten$Produkt) in the 'ui' and in summarise the sum needs to be specified for the column of interest
-ui
ui <- dashboardPage(
skin="red",
dashboardHeader(title = "Analytics Dashboard", titleWidth = 290),
dashboardSidebar(
width = 290,
sidebarMenu(
menuItem("Virtuelles Menü", tabName = "charts", icon = icon("cutlery"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "charts",
fluidPage(
br(),
fluidRow(
column(4,
selectInput('in6', 'Menü',
choices = unique(McDaten$Produkt), multiple=TRUE, selectize=TRUE )),
column(4,infoBoxOutput("progressBox"))
)
)
))))
-server
server <- function(input, output) {
output$progressBox <- renderInfoBox({
b <- McDaten %>%
select(`kcal (Portion)`, Produkt) %>%
filter(Produkt %in% input$in6) %>%
summarise(`kcal (Portion)` = sum(`kcal (Portion)`)) %>%
pull(`kcal (Portion)`)
infoBox(
"Progress", paste0(b, " kcal"), icon = icon("list"),
color = "purple", fill = TRUE
)
})
}
-run the app
shinyApp(ui, server)
-data
set.seed(24)
McDaten <- data.frame(Produkt = sample(LETTERS[1:5], 30, replace = TRUE),
`kcal (Portion)` = sample(1400:2000, 30, replace = TRUE),
stringsAsFactors= FALSE, check.names = FALSE)
McDaten$kcal <- McDaten$`kcal (Portion)`
-output