library(needs)
needs(
shiny,
ggplot2,
tidyverse,
shinydashboard,
DT
)
source("~/functions.R",local = T)
# Define UI for application that draws a histogram
header = dashboardHeader(
# tags$li(class = "dropdown",
# tags$style(".main-header {max-height: 80px}"),
# tags$style(".main-header .logo {height: 80px}")),
#title = tags$img(src='logo.png',height='100',width='200')
)
sidebar = dashboardSidebar(
menuItem("Full data",tabName="Data",icon=icon("table"),startExpanded = F,
fileInput("file","Upload CSV files",multiple=TRUE,accept=("text/comma"))),
menuItem(text = 'Simulate',tabName = 'simulate',icon=icon('chart-line'),
helpText('Simulation Parameters'),
radioButtons('type',"Please choose the type of analysis:",choices = list("Gender" = 1,"US Minority Status" = 2),selected = 1),
sliderInput("numSims","Number of simulations:",min = 1, max = 10000,step = 1000,value = 10000),
sliderInput("numYears","Number of years to simulate:",min = 1,max = 5,value = 3,step = 1),
numericInput('turnover','Total Turnover', value = 10),
sliderInput('promoRate','Set Promo rate', value = 25, min = 1, max = 100, step = 5),
sliderInput('growthRate','Set growth rate',value = 0,min=0,max=100,step = 1),
helpText('0% Growth Rate assumes a flat, constant headcount'),
actionButton('go',label = "Update"),width = 4)
)
body <- dashboardBody(
tabItems(
tabItem(
tabName = 'data',
fluidRow(wellPanel(
fileInput(
inputId = 'file',
label = "File Upload:",
accept = c("csv", ".csv")))),
wellPanel(DT::dataTableOutput('table'))),
tabItem(
tabName = 'simulate',
fluidRow(
wellPanel(
DT:::dataTableOutput('simDataTable')
))
)
))
ui = shinydashboard::dashboardPage(header,sidebar,body,skin='red')
server = server <- function(input, output) {
options(shiny.maxRequestSize = 30 * 1024 ^ 2)
dataset <- reactive({
req(input$file)
read.csv(input$file$datapath)
})
output$table = renderDataTable(dataset(), filter = 'top',options = list(scrollX = TRUE))
simulate = eventReactive(input$go,{
req(input$numSims,input$type)
x = dataset()
temp = dataSim(x,type=input$type,
numSims = input$numSims)
})
simulateAvg = reactive({
x = simulate()
y = x %>% group_by(Role) %>% summarise(mean(freq))
})
output$simDataTable = renderDataTable(simulateAvg())
}
shinyApp(ui,server)
I'm having some trouble with two issues.
1.) The formatting of the shiny dashboard is odd. The text on the side bar seems very compacted and not what other shiny dashboards look like. I'm not sure what the issue is.
2.) After upload, a table is suppose to appear on the dashboard body but it doesn't
3.) Once a table appears and I head to the simulate tab, will the dashboard body change accordingly and display the simulateAvgData set that I populated?
The dataSim function is from the source file on top. I don't receive any errors when I run anything so looking for guidance and inputs to whether or not this shiny dashboard work as intended. I'm newer to the dashboard packages from shiny.
You have a couple of issues here. You do not need a fileInput statement inside dashboardBody. Next, within dashboardSidebar, you can define fileInput at the top level of menuItem (option 1 in the code below), or a sub-level of the first menuItem (option 2 below). In either case, you need to have a menuItem with a tabName where you want to display the file that was read in. Once you read the input file, you need to select the appropriate tab to see the displayed data. Try this code
header <- dashboardHeader()
### option 1: fileInput at the first menuItem level
# sidebar <- dashboardSidebar(width=320,
# menuItem("Full data",tabName="Data",icon=icon("table"),startExpanded = F),
# fileInput("file","Upload CSV files",multiple=FALSE,accept=c("csv", ".csv"))
# )
### option 2 - fileInput as a subitem
sidebar <- dashboardSidebar(width=320,
menuItem("Full data",tabName="noData",icon=icon("table"),startExpanded = F, ## data not displayed for this tabName
menuItem("Full_data",tabName="Data", icon=icon("table")),
fileInput("file","Upload CSV files",multiple=FALSE,accept=c("csv", ".csv")))
)
body <- dashboardBody(
tabItems(
tabItem(
tabName = 'Data',
fluidRow(DTOutput('table')))
))
ui <- shinydashboard::dashboardPage(header,sidebar,body,skin='red')
server <- function(input, output, session) {
data1 <- reactive({
req(input$file)
data <- read.csv(input$file$datapath,sep = ",", header = TRUE)
})
output$table <- renderDT(data1())
}
shinyApp(ui,server)
I have a data set named "a2". Sample Data
CID Store Distance
1 X 2
2 Y 3
2 S 5
1 A 1
3 B 10
I want to develop an app in shiny with three tabs Filter Value, Nearest Store, Nearest Client. So whatever user chooses as an input so it should display all the rows of input.
So in Filter Value Tab
Ex if i choose CID 3 then it should extract only rows having CID3.
So in Nearest Store Tab
Ex if i choose Store X then it should extract only rows having Store X.
shinyApp(
ui = fluidPage(
titlePanel("Lat Long Address Mapping in R"),
fluidRow(
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("FilterValue",
selectInput('InputID', 'ID', choices=NULL, selected=NULL),
),
tabPanel("Nearest Store",
selectInput('InputStore', 'ID', choices=NULL, selected=NULL),
),
tabPanel("Nearest Client",
selectInput('InputCustomer', 'ID', choices=NULL, selected=NULL),
)
))))
,
server = function(input, output,session) {
output$FilterValue<- renderDataTable(a2)
updateSelectizeInput(session, 'InputID',
choices = a2$CID,
server = TRUE)
updateSelectizeInput(session, 'InputStore',
choices = a2$Store,
server = TRUE)
updateSelectizeInput(session, 'InputCustomer',
choices = a2$CID,
server = TRUE)
output$Nearest Client<- renderDataTable({
paste(input$InputCustomer)
})
})
However in Nearest Client Tab I want to have two filters one of CID and one of distance
So if choose CID1 and minimum distance 2 it should give me only 1 row.
My output is generating all data of a2 in tab FIlter Value and Nearest Store. I am stuck with Nearest Client Tab
Thanks
Leaflet
Data frame is a
Sample Data is as follows
ID Lat Long Address
1 12.904249 77.70253 1/2 CA
2 21.221475 72.81281 2/3 DC
3 23.039251 72.58388 3/5 HJ
library (leaflet)
shinyApp(
ui = fluidPage(
titlePanel("Lat Long Address Mapping in R"),
fluidRow(
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Map",
bootstrapPage(
tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
leafletOutput("map", width = "100%", height = "100%"),
absolutePanel(
top = 80,
left = 30,
)
)
)
,
server = function(input, output,session) {
output$map <- renderLeaflet({
leaflet(a) %>%
addProviderTiles("CartoDB.Positron") %>%
addMarkers(lng = ~Long, lat = ~Lat,
popup = ~address)})
When I run this standalone it works
leaflet(a) %>%
addProviderTiles("CartoDB.Positron") %>%
addMarkers(lng = ~Long, lat = ~Lat,
popup = ~address)
You can save the data as reactive and filter them it with dplyr
a2 %>% filter(CID == input$InputID & Distance == input$InputCustomer)
This will do what you want in the last tab. The code you gave also has issues like no output for the data tables you want to render and so on.I tried not to change too much on the way you constructed it but there are better ways to build the app. Below is a working example of what I think you are asking for:
a2 <- data.frame(CID = c(1,2,2,1,3),
Store = c("X", "Y", "S", "A", "B"),
Distance = c(2,3,5,1,10), stringsAsFactors = FALSE)
library(shiny)
library(dplyr)
library(DT)
ui <- fluidPage(
fluidRow(
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("FilterValue", value = "filtervalue",
selectInput('InputID', 'ID', choices=NULL, selected=NULL),
dataTableOutput("out1")
),
tabPanel("Nearest Store", value = "neareststore",
selectInput('InputStore', 'ID', choices=NULL, selected=NULL),
dataTableOutput("out2")
),
tabPanel("Nearest Client", value = "nearestclient",
selectInput('InputCustomer', 'ID', choices=NULL, selected=NULL),
dataTableOutput("out3")
)
)))
)
server <- function(input, output, session) {
filtout <- reactive({
a3 <- a2 %>% filter(CID == input$InputID)
return(a3)
})
output$out1 <- DT::renderDataTable(datatable(filtout(), options = list(searching = F,
pageLength = 20,
lengthMenu = c(5, 10, 15, 20),
scrollX = T,
autoWidth = TRUE
)))
storeout <- reactive({
a3 <- a2 %>% filter(Store == input$InputStore)
return(a3)
})
output$out2 <- DT::renderDataTable(datatable(storeout(), options = list(searching = F,
pageLength = 20,
lengthMenu = c(5, 10, 15, 20),
scrollX = T,
autoWidth = TRUE
)))
custout <- reactive({
a3 <- a2 %>% filter(CID == input$InputID & Distance == input$InputCustomer)
return(a3)
})
output$out3 <- DT::renderDataTable(datatable(custout(), options = list(searching = F,
pageLength = 20,
lengthMenu = c(5, 10, 15, 20),
scrollX = T,
autoWidth = TRUE
)))
updateSelectizeInput(session, 'InputID',
choices = a2$CID,
server = TRUE)
updateSelectizeInput(session, 'InputStore',
choices = a2$Store,
server = TRUE)
updateSelectizeInput(session, 'InputCustomer',
choices = a2$Distance,
server = TRUE)
output$nearestclient<- renderDataTable({
paste(input$InputCustomer)
})
}
shinyApp(ui, server)
New info:
So I found out the issue was the cardDB.postitron. I changed the map to what I use normally. Also I had to strip some of the UI to get it the principle working and adress variable was not provided but this should form the basis of what you need.
library(shiny)
library(leaflet)
ui <- fluidPage(
fluidRow(
mainPanel(
tabsetPanel(type = "tabs",
tabPanel(title = "Map",
leafletOutput("map")
)))))
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet(a) %>%
addProviderTiles(providers$Esri.WorldGrayCanvas,
options = providerTileOptions(noWrap = TRUE)) %>%
addMarkers(lng = ~Long, lat = ~Lat,
popup = ~address)
})
}
shinyApp(ui, server)
In the below code, I am attempting to create an input to show all of my markets, or just a selection within a plot and a data table. I am doing this through, or attempting, through ifelse statements within my render functions, however I am getting errors, and neither the plot or data table will render. They do render without the if else statements. I have included an Example data set to hopefully help place in context.
ui <- dashboardPage(
dashboardHeader(title = "Example"),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "Dashboard"),
menuItem("Example", tabName = "example"))),
dashboardBody(
tabItems(
tabItem(tabName = "Dashboard",
fluidRow(
valueBoxOutput("example"))),
tabItem(tabName = "example",
fluidRow(
box(title = "Example",
plotOutput("plotexample"), width = 12),
box(title = "Data Selection",
selectInput("market","Market(s): ", c(unique(data$marketnum),"All"),multiple = T, selectize = T, selected = "All"))),
fluidRow(
box(DT::dataTableOutput("markettable"), width = 12))))))
server <- function(input,output) {
ExampleAllMarkets <- reactive({
ExampleData %>%
group_by(Event,marketnum) %>%
summarise(ItemCount = n_distinct(ItemNumber))
})
Example <- reactive({
ExampleData %>%
filter(marketnum == input$market) %>%
group_by(Event,marketnum) %>%
summarise(PolicyCount = n_distinct(Policy_Number_9_Digit))
})
output$example <- renderValueBox({
valueBox(
paste0("44", "%"), "example", icon = icon("car"),
color = "red"
)
})
I am placing ifelse statements within my render blocks reactive to whether or not "All" is selected.
output$plotexample <- renderPlot({
ifelse(input$market=="All",
ggplot(Example(), aes(x=MBC_Number, y=ItemCount)) +
geom_bar(stat="identity"),
ggplot(ExampleAllMarkets(), aes(x=marketnum, y=ItemCount))
+
geom_bar(stat="identity"))
})
output$markettable <- DT::renderDataTable({
ifelse(input$market == "All",
ExampleAllMarkets(),
Example())
})
}
shinyApp(ui,server)
Example Data in csv format
marketnum,ItemNumber
2,118
7,101
1,109
2,109
10,101
4,102
8,100
12,103
5,106
13,116
5,112
10,103
7,113
9,114
10,112
6,114
2,116
11,113
3,107
13,102
8,107
10,109
12,110
1,120
4,106
8,116
2,112
2,106
11,101
6,108
11,107
10,111
6,120
10,118
11,119
13,117
You probably cannot use ifelse in this scenario.
Analyzing the source code for ifelse, since a plot object is not so simple, it does not just return the plot itself, but
rep(plot, length.out = 1)
or equivalently plot[1] which is just the dataset of the plot. A plot object has a length > 1 and for those, ifelse only returns its first element.
This can be easily confirmed by evaluating
> ifelse(T, c(1, 2), c(3, 4))
[1] 1
So the render function cannot draw anything, since it's input is just this dataset.
You will simply have to use the regular if else.
I have got a dashboard that pulls data from Google analytics or a CSV upload and then calculate conversion rate and average order value(for AB testing purpose).
I have been trying to implement filters that allow selecting device category e.g (mobile, tablet or desktop) and product category e.g(card, gift or flowers). The filters should pulled from from the data frame dynamically and then be available for selection in the drop downs.
I have seen a lot of similar example of this forum but for the life of me I haven't been able to make it work. The cases I have seen seem to be using observe ({}) but my issue seem to be coming from the fact that I need to pass the choices out of the reactive function first.
Below is a reproducible, simplified example, with data frame generated as they would appear.
I have commented out #choices= Results()$Devices in the UI so to show you how it looks like before it breaks.
Many thanks in advance
G
require(shiny)
require(shinydashboard)
require(googleVis)
require(dplyr)
ui <- dashboardPage(
skin="blue",
dashboardHeader(
title="Dashboard",
titleWidth = 250
),
dashboardSidebar(
sidebarMenu(
menuItem("Calculator ", tabName = "calculator", icon = icon("calculator"))
)
),
#
dashboardBody(
tabItems(
tabItem(tabName = "calculator",
h1("Calculator"),
fluidRow(
column(width = 1,
selectInput("device","Device:",
#choices= Results()$Devices,
multiple=TRUE, selectize=TRUE)
),
column(width = 1,
selectInput("product","Product:",
#"choices= Results()$Products",
multiple=TRUE, selectize=TRUE)
)
),
fluidRow(
column(width = 6,
box(title="Overall Conversion rate %",status="primary",solidHeader = TRUE,
htmlOutput("CRABCalcl"),width = "100%",height=275)
),
column(width = 6,
box(title="Overall AOV £",status="primary",solidHeader = TRUE,
htmlOutput("AOVABCalcl"),width = "100%",height=275)
)
),
fluidRow(
column(width = 6,
box(title="Ecommerce Conversion rate %",status="primary",solidHeader = TRUE,
htmlOutput("CRABCalclEHC"),width = "100%",height=275)
),
column(width = 6,
box(title="Ecoomerce AOV £",status="primary",solidHeader = TRUE,
htmlOutput("AOVABCalclEHC"),width = "100%",height=275)
)
)
)
)#End of tab Item
) #end of tabItems
)#End of Dashboard body
)#End of dashboardPage
server <- function(input, output,session) {
Results <- reactive({
myDataRAW<-data.frame(
c("mobile","mobile","desktop","desktop","tablet","tablet"),
c("Control","Challenger","Control","Challenger","Control","Challenger"),
c(34355,34917,28577,29534,15337,13854),
c(15011,15427,32190,32548,40299,40858),
c(14636,14990,19609,19702,7214,7785),
c(123273.70,20936.92,45179.05,46359.91,65765.27,92771.36),
c(10370,13403,19241,26965,4468,8796)
)
myDataRAWEHC<-data.frame(
c("desktop","desktop","mobile","mobile","tablet","tablet","desktop","desktop","mobile","mobile","desktop","desktop","mobile","mobile","tablet","tablet","tablet","tablet","desktop","desktop"),
c("Card","Card","Card","Card","Card","Card","Card","Card","Gift","Gift","Gift","Card","Card","Card","Card","Card","Card","Card","Flower","Flower"),
c("Standard","Standard","Standard","Standard","Standard","Standard","Large","Large","Large","Large","Square","Square","Square","Square","Large","Large","Square","Square","Flowers","Flowers"),
c("Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger"),
c(8767,18072,5729,13017,2908,7086,1655,2971,1008,2177,984,2369,599,1422,449,1052,402,1001,233,355),
c(9055,18624,5908,13302,3015,7288,1691,3000,1013,2192,1009,2455,623,1450,455,1068,413,1017,233,356),
c(21699.60,44480.95,14464.85,32590.30,7232.47,17483.35,8309.85,14684.68,5024.92,10844.67,2405.07,5826.83,1529.16,3556.38,2220.21,5192.92,992.14,2447.78,5196.08,8021.95)
)
names(myDataRAW)<-c("Device.Category","Segment","Users","Sessions","Transactions","Revenue","Quantity")
names(myDataRAWEHC)<-c("Device.Category","Product.Category..Enhanced.Ecommerce.","Product.Variant","Segment","Unique.Purchases","Quantity","Product.Revenue")
Devices<-myDataRAW$Device.Category
Products<-unique(myDataRAWEHC$Product.Category..Enhanced.Ecommerce.)
# DeviceFilter<-input$device
# ProductFilter<-input$product
#the below is replacing the above input to act as filters
DeviceFilter<-c("desktop","mobile")
ProductFilter<-c("Flower","Gift")
myData<-myDataRAW %>% filter(Device.Category %in% DeviceFilter)
myDataEHC<-myDataRAWEHC %>% filter(Device.Category %in% DeviceFilter) %>% filter(`Product.Category..Enhanced.Ecommerce.` %in% ProductFilter)
myData<-bind_rows(myData,myData %>% group_by(Device.Category="All",Segment) %>% summarise(Users=sum(Users),Sessions=sum(Sessions),Transactions=sum(Transactions),Revenue=sum(Revenue),Quantity=sum(Quantity)))
myDataEHC<-rbind(myDataEHC %>% group_by(Device.Category,Segment) %>% summarise(Transactions=sum(Unique.Purchases),Quantity=sum(Quantity),Revenue=sum(Product.Revenue)),
myDataEHC %>% group_by(Device.Category="All",Segment) %>% summarise(Transactions=sum(Unique.Purchases),Quantity=sum(Quantity),Revenue=sum(Product.Revenue)) )
myDataEHC<-left_join(myDataEHC,myData %>% select(Segment,Device.Category,Users,Sessions))
myData$Analysis<-"Overall"
myDataEHC$Analysis<-"Ecommerce"
myDataForAnalysis<-rbind(as.data.frame(myData),as.data.frame(myDataEHC))
myDataForAnalysis$CVR<-myDataForAnalysis$Transactions/myDataForAnalysis$Sessions
myDataForAnalysis$AOV<-myDataForAnalysis$Revenue/myDataForAnalysis$Transactions
DisplayResultsEHC<-myDataForAnalysis %>% filter(Analysis %in% "Ecommerce")
DisplayResults<-myDataForAnalysis %>% filter(Analysis %in% "Overall")
list(DisplayResultsEHC=DisplayResultsEHC,DisplayResults=DisplayResults,Devices=Devices,Products=Products)
})
output$CRABCalcl <- renderGvis({
DataABCalcl<-Results()$DisplayResults
F<-cast(DataABCalcl, Device.Category~Segment, value = 'CVR')
X<-gvisColumnChart(F,options=list(legend="{position:'top'}",width="100%"))
})
output$AOVABCalcl <- renderGvis({
DataABCalcl<-Results()$DisplayResults
F<-cast(DataABCalcl, Device.Category~Segment, value = 'AOV')
X<-gvisColumnChart(F,options=list(legend="{position:'top'}",width="100%"))
})
output$CRABCalclEHC <- renderGvis({
DataABCalcl<-Results()$DisplayResultsEHC
F<-cast(DataABCalcl, Device.Category~Segment, value = 'CVR')
X<-gvisColumnChart(F,options=list(legend="{position:'top'}",width="100%"))
})
output$AOVABCalclEHC <- renderGvis({
DataABCalcl<-Results()$DisplayResultsEHC
F<-cast(DataABCalcl, Device.Category~Segment, value = 'AOV')
X<-gvisColumnChart(F,options=list(legend="{position:'top'}",width="100%"))
})
}
shinyApp(ui, server)
From what I see a good start would be to create a global.R file containing (and remove from server.R):
global.R
myDataRAW<-data.frame(
c("mobile","mobile","desktop","desktop","tablet","tablet"),
c("Control","Challenger","Control","Challenger","Control","Challenger"),
c(34355,34917,28577,29534,15337,13854),
c(15011,15427,32190,32548,40299,40858),
c(14636,14990,19609,19702,7214,7785),
c(123273.70,20936.92,45179.05,46359.91,65765.27,92771.36),
c(10370,13403,19241,26965,4468,8796)
)
myDataRAWEHC<-data.frame(
c("desktop","desktop","mobile","mobile","tablet","tablet","desktop","desktop","mobile","mobile","desktop","desktop","mobile","mobile","tablet","tablet","tablet","tablet","desktop","desktop"),
c("Card","Card","Card","Card","Card","Card","Card","Card","Gift","Gift","Gift","Card","Card","Card","Card","Card","Card","Card","Flower","Flower"),
c("Standard","Standard","Standard","Standard","Standard","Standard","Large","Large","Large","Large","Square","Square","Square","Square","Large","Large","Square","Square","Flowers","Flowers"),
c("Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger","Control","Challenger"),
c(8767,18072,5729,13017,2908,7086,1655,2971,1008,2177,984,2369,599,1422,449,1052,402,1001,233,355),
c(9055,18624,5908,13302,3015,7288,1691,3000,1013,2192,1009,2455,623,1450,455,1068,413,1017,233,356),
c(21699.60,44480.95,14464.85,32590.30,7232.47,17483.35,8309.85,14684.68,5024.92,10844.67,2405.07,5826.83,1529.16,3556.38,2220.21,5192.92,992.14,2447.78,5196.08,8021.95)
)
names(myDataRAW)<-c("Device.Category","Segment","Users","Sessions","Transactions","Revenue","Quantity")
names(myDataRAWEHC)<-c("Device.Category","Product.Category..Enhanced.Ecommerce.","Product.Variant","Segment","Unique.Purchases","Quantity","Product.Revenue")
This allows you to access myDataRAW and myDataRAWEHC from ui.R. Modify the ui.R accordingly:
fluidRow(
column(width = 3,
selectInput("device","Device:",
choices= levels(myDataRAW$Device.Category),
multiple=TRUE, selectize=TRUE)
),
column(width = 3,
selectInput("product","Product:",
choices= unique(levels(myDataRAWEHC$Product.Category..Enhanced.Ecommerce.)),
multiple=TRUE, selectize=TRUE)
)
),
after that you still have some work left to rearrange the server.R part.