How I am able to show the output of dashboardBody when the id of rightSidebarTabContent selected. If id = "tab_1", selected, show the verbatimTextOutput("tab1") and so on. I used shinyjs::show and shinyjs::hide, but it's not working. Any suggestion?
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPagePlus(
header = dashboardHeaderPlus(
enable_rightsidebar = TRUE,
rightSidebarIcon = "gears"
),
sidebar = dashboardSidebar(),
rightsidebar = rightSidebar(
id = "right_sidebar",
background = "dark",
rightSidebarTabContent(
id = "tab_1",
title = "Tab 1",
icon = "desktop",
active = TRUE,
sliderInput(
"obs",
"Number of observations:",
min = 0, max = 1000, value = 500
)
),
rightSidebarTabContent(
id = "tab_2",
title = "Tab 2",
textInput("caption", "Caption", "Data Summary")
),
rightSidebarTabContent(
id = "tab_3",
icon = "paint-brush",
title = "Tab 3",
numericInput("obs", "Observations:", 10, min = 1, max = 100)
)
),
dashboardBody(
div(id = "tab1_out", verbatimTextOutput("tab1")),
div(id = "tab2_out", verbatimTextOutput("tab2")),
div(id = "tab3_out", verbatimTextOutput("tab3"))
)
)
server <- function(input, output) {
output$tab1 <- renderPrint({
"tab1"
})
output$tab2 <- renderPrint({
"tab2"
})
output$tab3 <- renderPrint({
"Tab3"
})
observeEvent(input$right_sidebar,{
if(input$right_sidebar == "tab_1"){
shinyjs::show("tab1_out")
shinyjs::hide("tab2_out")
shinyjs::hide("tab3_out")
}else if(input$right_sidebar == "tab_2"){
shinyjs::hide("tab1_out")
shinyjs::show("tab2_out")
shinyjs::hide("tab3_out")
}else{
shinyjs::hide("tab1_out")
shinyjs::hide("tab2_out")
shinyjs::show("tab3_out")
}
})
}
shinyApp(ui, server)
I am not sure that you can hide and show the body content from right sidebar. However, you can control the outputs in display page. The code below shows that the body content is still controlled by left sidebar, but the plot display can be changed from the right sidebar. For each tabPanel, you can either choose to have a right sidebar or not.
library(shiny)
library(shinydashboard)
library(shinyjs)
library(shinydashboardPlus)
library(ggplot2)
header <- dashboardHeaderPlus(
enable_rightsidebar = TRUE,
rightSidebarIcon = "gears"
)
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Section A", tabName = "Section_A", icon = icon("map")),
menuItem("Section B", tabName = "Section_B", icon = icon("chart-line")),
menuItem("Section C", tabName = "Section_C", icon = icon( "gears")),
id = "nav"
)
)
rightsidebar <- rightSidebar(
shiny::tags$head(shiny::tags$style(shiny::HTML(
".control-sidebar-tabs {display:none;}
.tabbable > .nav > li > a:hover {background-color: #333e43; color:white}
.tabbable > .nav > li[class=active] > a {background-color: #222d32; color:white}"))),
# '{display:none;}' removes empty space at top of rightsidebar
background = "dark",
uiOutput("side_bar"),
title = "Right Sidebar"
)
body <- dashboardBody(
tabItems(
tabItem(
tabName = "Section_A",
p("Some content for section A"),
tabPanel(id = "tab_1o", "Tab 1 for Section A", verbatimTextOutput("tab1"), plotOutput("plot1")),
),
tabItem(
tabName = "Section_B",
p("Some content for section B"),
tabPanel(id = "tab_2o", "Tab 2 for Section B", verbatimTextOutput("tab2"), DTOutput("data2") ),
),
tabItem(
tabName = "Section_C",
p("Some content for section C"),
tabPanel(id = "tab_3o", "Tab 3 for Section C", verbatimTextOutput("tab3"), plotOutput("plot3"))
)
),
tags$script(
'$("a[data-toggle=\'tab\']").click(function(){
Shiny.setInputValue("tabactive", $(this).data("value"))
})'
)
)
ui <- tags$body(class="skin-blue sidebar-mini control-sidebar-open", dashboardPagePlus( ## keep the right sidebar open permanently
#ui <- dashboardPagePlus(
shinyjs::useShinyjs(),
header = header,
sidebar = sidebar,
body = body,
rightsidebar = rightsidebar
)
)
server <- function(input, output) {
output$tab1 <- renderPrint({
"tab1"
})
output$plot1 <- renderPlot({
set.seed(122)
histdata <- rnorm(500)
data <- histdata[seq_len(req(input$obs1))]
hist(data)
})
output$tab2 <- renderPrint({
"tab2"
})
output$plot2 <- renderPlot(qplot(rnorm(500),fill=I("green"),binwidth=0.2,title="plotgraph2"))
output$data2 <- renderDT(datatable(iris))
output$tab3 <- renderPrint({
"Tab3"
})
output$plot3 <- renderPlot(qplot(rnorm(req(input$obs3)),fill=I("blue"),binwidth=0.2,title="plotgraph3"))
observe({
if (req(input$nav) == "Section_A"){
message("tab_1 has been selected")
#shinyjs::addClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
shinyjs::removeClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
output$side_bar <- renderUI({
rightSidebarTabContent(
id = "tab_1",
title = "Right sidebar for Section A ",
icon = "desktop",
#active = TRUE,
sliderInput(
"obs1",
"Number of observations:",
min = 0, max = 1000, value = 500
)
)
})
}
if (req(input$nav) == "Section_B"){
message("tab_2 has been selected")
#shinyjs::addClass(selector = "aside.control-sidebar", class = "control-sidebar-open") ## to add right sidebar
shinyjs::removeClass(selector = "aside.control-sidebar", class = "control-sidebar-open") ## remove right sidebar
output$side_bar <- renderUI({
rightSidebarTabContent(
id = "tab_2",
title = "Right sidebar for Section B ",
textInput("caption", "Caption", "Data Summary")
)
})
}
if (req(input$nav) == "Section_C"){
message("tab_3 has been selected")
#shinyjs::addClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
shinyjs::removeClass(selector = "aside.control-sidebar", class = "control-sidebar-open")
output$side_bar <- renderUI({
rightSidebarTabContent(
id = "tab_3",
icon = "paint-brush",
title = "Right sidebar for Section C",
numericInput("obs3", "Observations:", 400, min = 1, max = 1000)
)
})
}
})
}
shinyApp(ui, server)
Related
I'm trying to customize how my R shiny app looks like and playing around with various elements on the page.
Just wondering how do I remove this toggle icon from the header? I've tried something like this but it doesn't work:
shinyjs::runjs("document.getElementsByClassName('skin-blue sidebar-mini')[0].style.visibility = 'hidden';")
reproducible example:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(DT)
ui <- shinydashboardPlus::dashboardPage(
options = list(sidebarExpandOnHover = TRUE),
header = dashboardHeader(
tags$li(
id = 'right-sidebar-toggle-list-item',
class = "dropdown",
actionLink("rightSidebarToggle", "Select Population"))
),
shinydashboardPlus::dashboardSidebar( disable = TRUE ,
sidebarMenu(
selectInput(
"countries", label = "Select Countries",
choices = c("B", "C", "A"), selected = "A",
multiple = TRUE
))
),# minified = TRUE, collapsed = F),
controlbar = shinydashboardPlus::dashboardControlbar(id = "controlbar", collapsed = F,
skin = "dark",
controlbarMenu(
id = "menu",
controlbarItem(
"Tab 1",
"Welcome to tab 1"
),
controlbarItem(
"Tab 2",
"Welcome to tab 2"
)
)
),
shinydashboard::dashboardBody(
useShinyjs(),
tabsetPanel( id="tabset",
tabPanel("Resource Allocation", value="tab1", plotOutput("plot")),
)
),
# controlbar = dashboardControlbar(collapsed = F),
title = "DashboardPage"
)
server <- function(input, output) {
output$plot <- renderPlot(plot(cars))
}
shinyApp(ui, server)
We can use some JS via tags$script to hide the icon:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(DT)
ui <- shinydashboardPlus::dashboardPage(
options = list(sidebarExpandOnHover = TRUE),
shinydashboardPlus::dashboardHeader(
tags$li(
id = 'right-sidebar-toggle-list-item',
class = "dropdown",
actionLink("rightSidebarToggle", "Select Population"))
),
shinydashboardPlus::dashboardSidebar(disable = TRUE ,
sidebarMenu(
selectInput(
"countries",
label = "Select Countries",
choices = c("B", "C", "A"),
selected = "A",
multiple = TRUE
)
)),
# minified = TRUE, collapsed = F),
controlbar = shinydashboardPlus::dashboardControlbar(
id = "controlbar",
collapsed = F,
skin = "dark",
controlbarMenu(
id = "menu",
controlbarItem("Tab 1",
"Welcome to tab 1"),
controlbarItem("Tab 2",
"Welcome to tab 2")
)
),
shinydashboard::dashboardBody(
useShinyjs(),
# hide icon
# tags$script(
# HTML(
# 'var e = document.querySelector("body > div.wrapper > header > nav > div:nth-child(4) > ul > li > a > i");
# e.setAttribute("style", "display: none;");'
# )
# ),
# hide hyperlink
tags$script(HTML('var e = document.querySelector("body > div.wrapper > header > nav > div:nth-child(4) > ul > li:last-child > a");
e.setAttribute("style", "display: none;");')),
tabsetPanel(
id = "tabset",
tabPanel("Resource Allocation", value = "tab1", plotOutput("plot")),
)
),
# controlbar = dashboardControlbar(collapsed = F),
title = "DashboardPage"
)
server <- function(input, output) {
output$plot <- renderPlot(plot(cars))
}
shinyApp(ui, server)
I have the shinydashboard below in which I have 3 tabPanels. In the 1st tabPanel "Resource Allocation" I want the left and right sidebar open by default. In the 2nd and 3rd tabpanels ("Time Series","Longitudinal View") I want only left sidebar and the right sidebar not just hidden but to not be able to open at all by pushing the "gears" icon above it which should be removed. And in the fourth panel "User Guide" I want no sidebar and no choise to open one of them at all.
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
shinyApp(
ui = dashboardPage(
options = list(sidebarExpandOnHover = TRUE),
header = dashboardHeader(
titleWidth = "0px"
),
sidebar = dashboardSidebar(minified = TRUE, collapsed = F),
body = dashboardBody(
useShinyjs(),#tags$head(tags$script(src="format_number.js")),
tags$script("document.getElementsByClassName('sidebar-toggle')[0].style.visibility = 'hidden';"),
tabsetPanel(
tabPanel("Resource Allocation"),
tabPanel("Time Series"),
tabPanel("Longitudinal View"),
tabPanel("User Guide")
)
),
controlbar = dashboardControlbar(collapsed = F),
title = "DashboardPage"
),
server = function(input, output) { }
)
I have a solution for the left sidebar. I am sure you can spend sometime and figure out the solution for the right sidebar. Please note that this requires some more work to fine tune to your needs. Try this
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
library(DT)
ui <- shinydashboardPlus::dashboardPage(
options = list(sidebarExpandOnHover = TRUE),
shinydashboardPlus::dashboardHeader(
#titleWidth = "0px"
),
shinydashboardPlus::dashboardSidebar( disable = TRUE ,
sidebarMenu(
selectInput(
"countries", label = "Select Countries",
choices = c("B", "C", "A"), selected = "A",
multiple = TRUE
))
),# minified = TRUE, collapsed = F),
controlbar = shinydashboardPlus::dashboardControlbar(id = "controlbar", collapsed = F,
skin = "dark",
controlbarMenu(
id = "menu",
controlbarItem(
"Tab 1",
"Welcome to tab 1"
),
controlbarItem(
"Tab 2",
"Welcome to tab 2"
)
)
),
shinydashboard::dashboardBody(
useShinyjs(),
tabsetPanel( id="tabset",
tabPanel("Resource Allocation", value="tab1", plotOutput("plot")),
tabPanel("Time Series", value="tab2", plotOutput("plot2")),
tabPanel("Longitudinal View", value="tab3", DTOutput("ir")),
tabPanel("User Guide", value="tab4", DTOutput("mt"))
)
),
# controlbar = dashboardControlbar(collapsed = F),
title = "DashboardPage"
)
server <- function(input, output) {
output$plot <- renderPlot(plot(cars))
output$plot2 <- renderPlot(plot(pressure))
output$mt <- renderDT(mtcars)
output$ir <- renderDT(iris)
observeEvent(input[["tabset"]], {
if(input[["tabset"]] == "tab4"){
addClass(selector = "body", class = "sidebar-collapse")
updateControlbar("controlbar")
}else{
removeClass(selector = "body", class = "sidebar-collapse")
}
})
}
shinyApp(ui, server)
Is it a way to create an shiny observeEvent dependant from shiny subitem selection?
In the following reproductible example, I would like to automaticaly execute button 1 when submenu 1 is clicked and automaticaly execute button 3 when submenu 2 is clicked.
library(shinydashboard)
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(
sidebarMenuOutput("menu")
),
dashboardBody(heigth = 800, tabItems(
tabItem(tabName = "submenu_1",
fluidRow(
actionButton(inputId = "button_1",label = "Button 1", icon = icon("fa"),width = '417px'),
actionButton(inputId = "button_2",label = "Button 2", icon = icon("fa"),width = '417px')
)
),
tabItem(tabName = "submenu_2",
fluidRow(
actionButton(inputId = "button_3",label = "Button 3", icon = icon("fa"),width = '417px'),
actionButton(inputId = "button_4",label = "Button 4", icon = icon("fa"),width = '417px')
)
)
),
textOutput("text")
)
)
server <- function(input, output) {
output$menu <- renderMenu({
sidebarMenu(
menuItem("Menu item 1",
menuSubItem('Submenu 1',tabName = 'submenu_1',icon = icon('line-chart')),
menuSubItem('Submenu 2',tabName = 'submenu_2',icon = icon('line-chart'))
)
)
})
observeEvent(input$button_1,{output$text <- renderText("Buutton 1 must be selected by default on Submenu 1")})
observeEvent(input$button_2,{output$text <- renderText("You have selected button 2")})
observeEvent(input$button_3,{output$text <- renderText("Buutton 3 must be selected by default on Submenu 2 ")})
observeEvent(input$button_4,{output$text <- renderText("You have selected button 4")})
}
shinyApp(ui, server)
Thanks in advance!
Is that what you need??
You can add an id argument in sidebarMenu, and then add an observeEvent object triggered by input$sidebarmenu
library(shinydashboard)
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(
sidebarMenuOutput("menu")
),
dashboardBody(heigth = 800, tabItems(
tabItem(tabName = "submenu_1",
fluidRow(
actionButton(inputId = "button_1",label = "Button 1", icon = icon("fa"),width = '417px'),
actionButton(inputId = "button_2",label = "Button 2", icon = icon("fa"),width = '417px')
)
),
tabItem(tabName = "submenu_2",
fluidRow(
actionButton(inputId = "button_3",label = "Button 3", icon = icon("fa"),width = '417px'),
actionButton(inputId = "button_4",label = "Button 4", icon = icon("fa"),width = '417px')
)
)
),
textOutput("text")
)
)
server <- function(input, output) {
output$menu <- renderMenu({
sidebarMenu(id = "sidebarmenu",
menuItem("Menu item 1",
menuSubItem('Submenu 1',tabName = 'submenu_1',icon = icon('line-chart')),
menuSubItem('Submenu 2',tabName = 'submenu_2',icon = icon('line-chart'))
)
)
})
observeEvent(input$sidebarmenu,{
output$text <- renderText({
if(input$sidebarmenu=="submenu_1"){
"Buutton 1 must be selected by default on Submenu 1"
}else if(input$sidebarmenu=="submenu_2"){
"Buutton 3 must be selected by default on Submenu 2 "
}
})
})
observeEvent(input$button_1,{
output$text <- renderText("Buutton 1 must be selected by default on Submenu 1")
})
observeEvent(input$button_2,{
output$text <- renderText("You have selected button 2")
})
observeEvent(input$button_3,{
output$text <- renderText("Buutton 3 must be selected by default on Submenu 2 ")
})
observeEvent(input$button_4,{
output$text <- renderText("You have selected button 4")
})
}
shinyApp(ui, server)
The trick is to set the parameter id on the UI part.
The code below does the job :
library(shinydashboard)
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "Dynamic sidebar"),
dashboardSidebar(
sidebarMenu(id="tabs",
sidebarMenuOutput("menu")
)
),
dashboardBody(heigth = 800, tabItems(
tabItem(tabName = "submenu_1",
fluidRow(
actionButton(inputId = "button_1",label = "Button 1", icon = icon("fa"),width = '417px'),
actionButton(inputId = "button_2",label = "Button 2", icon = icon("fa"),width = '417px')
)
),
tabItem(tabName = "submenu_2",
fluidRow(
actionButton(inputId = "button_3",label = "Button 3", icon = icon("fa"),width = '417px'),
actionButton(inputId = "button_4",label = "Button 4", icon = icon("fa"),width = '417px')
)
)
),
textOutput("text")
)
)
server <- function(input, output) {
output$menu <- renderMenu({
sidebarMenu(
menuItem("Menu item 1",
menuSubItem('Submenu 1',tabName = 'submenu_1',icon = icon('line-chart')),
menuSubItem('Submenu 2',tabName = 'submenu_2',icon = icon('line-chart'))
)
)
})
observeEvent(input$tabs, {
req(input$tabs)
if (input$tabs == "submenu_1") {
# Do whatever you want when submenu_1 is selected
print("submenu_1 selected")
} else if (input$tabs == "submenu_2") {
# Do whatever you want when submenu_2 is selected
print("submenu_2 selected")
}
})
observeEvent(input$button_1,{output$text <- renderText("Buutton 1 must be selected by default on Submenu 1")})
observeEvent(input$button_2,{output$text <- renderText("You have selected button 2")})
observeEvent(input$button_3,{output$text <- renderText("Buutton 3 must be selected by default on Submenu 2 ")})
observeEvent(input$button_4,{output$text <- renderText("You have selected button 4")})
}
shinyApp(ui, server)
hi i im trying to have the 'choices' in my pickerInput be dependent on what the user types in the search bar above. im using a spotify r package and if you search a certain artist the api returns a table of artists of the similar name and you need to choose which artist you want. anyways i want that table to go into the pickerInput and i can't seem to get it work.
ui <- dashboardPage(skin = "green",
dashboardHeader(title = "Lyric Prediction"),
dashboardSidebar(
sidebarMenu(
menuItem("Overview", tabName = "Overview", icon = icon("search")),
menuItem("Analysis", tabName = "Analysis", icon = icon("bar-chart-o"))
)
),
dashboardBody(
tags$head(
tags$style(HTML(".fa { font-size: 18px; }"))
),
tabItems(
# First tab content
tabItem(tabName = "Overview",
fluidRow(
column(12,
searchInput(
inputId = "search", label = "Search Artist on Spotify",
placeholder = "Search",
btnSearch = icon("search"),
btnReset = icon("remove"),
width = "500px"
)
), align = "center"
),
#HERE - how can i have pickerInput take in the output
#of "res" from the server?
fluidRow(pickerInput(choices = "res")
)),
)
)
server <- function(input, output) {
#function to take search input
#output list of possible artists
output$res <- renderTable({
#Rspotify
possibleArtists <- searchArtist(input$search,token=my_oauth)
possibleArtists <- as_tibble(possibleArtists)
myCols <- c("display_name","id")
colNums <- match(myCols,names(possibleArtists))
possibleArtists <- possibleArtists %>%
select(colNums)
possibleArtists
})
}
shinyApp(ui, server)
Use update method in an observeEvent like this:
observeEvent(input$search, {
#Rspotify
possibleArtists <- searchArtist(input$search,token=my_oauth)
possibleArtists <- as_tibble(possibleArtists)
myCols <- c("display_name","id")
colNums <- match(myCols,names(possibleArtists))
possibleArtists <- possibleArtists %>%
select(colNums)
updatePickerInput(
session = session,
inputId = "picker",
choices = possibleArtists
)
}, ignoreInit = TRUE)
Full example:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
ui <- dashboardPage(
skin = "green",
dashboardHeader(title = "Lyric Prediction"),
dashboardSidebar(
sidebarMenu(
menuItem("Overview", tabName = "Overview", icon = icon("search")),
menuItem("Analysis", tabName = "Analysis", icon = icon("bar-chart-o"))
)
),
dashboardBody(
tags$head(
tags$style(HTML(".fa { font-size: 18px; }"))
),
tabItems(
# First tab content
tabItem(
tabName = "Overview",
fluidRow(
column(12,
searchInput(
inputId = "search", label = "Search Artist on Spotify",
placeholder = "Search",
btnSearch = icon("search"),
btnReset = icon("remove"),
width = "500px"
)
), align = "center"
),
pickerInput(inputId = "picker", label = "Choose an artist:", choices = NULL)
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$search, {
updatePickerInput(
session = session,
inputId = "picker",
choices = c("The Beatles",
"The Beatles Recovered Band",
"Yesterday - A Tribute To The Beatles",
"The Beatles Revival Band & Orchestra")
)
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
I have this initial shiny dashboard that I assembeled together:
## app.R ##
library(shiny)
library(shinydashboard)
library(readxl)
ui <- dashboardPage(
dashboardHeader(dropdownMenuOutput("messageMenu"),
dropdownMenuOutput("notificationMenu")),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("ImportForcast", tabName = "ImportForcast", icon = icon("arrow-down")),
menuItem("Visualization", tabName = "Visualization", icon = icon("dashboard")),
menuItem("Help", tabName = "Help", icon = icon("list-alt") ),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
## Body content
dashboardBody(
tabItems(
# First tab content
tabItem(tabName = "dashboard",
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
title = "Controls",
sliderInput("slider", "Number of observations:", 1, 100, 50)
)
)
),
tabItem(tabName = "ImportForcast",
fluidRow(
span(headerPanel("Import forcast file"), style="color:red"),
sidebarPanel(
radioButtons("pdt", "Choisir produit:",
c("Option1" = "Option1",
"Option2" = "Option2")),
fileInput('file1', 'Choose CSV File',
accept=c('text/csv', 'text/comma-separated-values,text/plain', '.csv')),
tags$hr()
),
actionButton("do", "Export/View"),
dataTableOutput('table'),
mainPanel(
# dygraphOutput("hdpePlot"),
textOutput("text"),
tableOutput('contents')
)
)
),
tabItem(tabName = "Visualization",
titlePanel("Forcast Dataset"),
sidebarLayout(
sidebarPanel(
radioButtons("x", "Select X-axis:",
list("date"='a')),
radioButtons("y", "Select Y-axis:",
list('X000'='f', "inflation"='b',"X004"='c',"X008"='d', "X009"='e','X014'='j'))
),
mainPanel(
textOutput("msg"),
plotOutput("distPlot")
)
)
),
tabItem(tabName = "Help",
fluidRow(
h2(" Help : "),
h1(" Installation guide : "),
h3(" Contact : "),
box(title = "Histogram", status = "primary", plotOutput("plot2", height = 250)),
box(
title = "Inputs", status = "warning",
"Box content here", br(), "More box content",
sliderInput("slider", "Slider input:", 1, 100, 50),
textInput("text", "Text input:")
),
box(
title = "Histogram", background = "maroon", solidHeader = TRUE,
plotOutput("plot4", height = 250)
),
box(
title = "Inputs", background = "black",
"Box content here", br(), "More box content",
sliderInput("slider", "Slider input:", 1, 100, 50),
textInput("text", "Text input:")
)
)
),
# Second tab content
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
)
#Server
server <- function(input, output) {
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
#Show success message if data is successfuly imported
output$text<- reactive({
validate(
need(is.null(input$file1) , "Import Success")
)
"Not imported file yet"
})
#Show msg that inform the user that he must import file before visualization
output$msg<- reactive({
validate(
need(!is.null(input$file1) , "You must import file for visualization")
)
"Enjoy visualisation"
})
output$dataForcast<- reactive({
dataForcast<- read_excel(input$file1)
})
dataForcast <- read_excel("./Forcast.xlsx",
sheet = 1, na = "NA",
skip = 1)
output$table <- renderDataTable(dataForcast)
# Saving files
saveRDS(dataForcast,file="./forcast/RDS/dataForcast.Rds")
write.csv(dataForcast, file = "./forcast/RDS/dataForcastt.csv", row.names = FALSE)
df <- eventReactive(input$do, {
dataForcast <- read_excel("./Forcast.xlsx",
sheet = 1, na = "NA",
skip = 1)
output$table <- renderDataTable(dataForcast)
})
# output$do<- reactive({
#
# output$table <- renderDataTable(dataForcast)
# })
#The visualisation of the data
output$distPlot<- renderPlot({
if(!is.null(input$file1) )
{
if(input$x=='a'){
i<-1}
if(input$y=='b'){
j<-54}
if(input$y=='c'){
j<-4}
if(input$y=='d'){
j<-9}
if(input$y=='e'){
j<-10 }
if(input$y=='f'){
j<-2 }
if(input$y=='j'){
j<-14 }
s <- dataForcast[, i]
k <- dataForcast[, j]
x2 <- data.frame(s,k)
plot(x2)
}
})
#Notification is generated on the server
output$notificationMenu <- renderMenu({
#Initalisation of notification
col_headings <- c('message','status')
# notificationData <- data.frame(' 12 items delivered', 'success')
notificationData<- read.csv('msgs.csv')
names(notificationData) <- col_headings
#add a notification of success importation
if(!is.null(input$file1))
{
not_sucess<- data.frame('Import Success', 'success')
names(not_sucess) <- col_headings
notificationData<-rbind(not_sucess, notificationData)
}
nots <- apply(notificationData, 1, function(row) {
notificationItem(text = row[["message"]], status = row[["status"]])
})
dropdownMenu(type = "notifications", .list = nots)
})
#Message is generated on the server
output$messageMenu<- renderMenu({
dropdownMenu(type = "messages",
messageItem(
from = "Sales Dept",
message = "Sales are steady this month."
),
messageItem(
from = "New User",
message = "How do I register?",
icon = icon("question"),
time = "13:45"
),
messageItem(
from = "Support",
message = "The new server is ready.",
icon = icon("life-ring"),
time = "2014-12-01"
)
)
})
}
shinyApp(ui, server)
I tried to add a login page but it was not succesfull :
library(shiny)
library(shinydashboard)
source("admin.R")
my_username <- c("test","admin")
my_password <- c("test","123")
get_role=function(user){
if(user=="test") {
return("TEST")
}else{
return("ADMIN")
}
}
get_ui=function(role){
itog=list()
if(role=="TEST"){
itog$title=test_title
itog$main=test_main
itog$side=test_side
return(itog)
}else{
itog$title=admin_title
itog$main=admin_main
itog$side=admin_side
return(itog)
}
}
shinyServer(function(input, output,session) {
USER <- reactiveValues(Logged = FALSE,role=NULL)
ui1 <- function(){
tagList(
div(id = "login",
wellPanel(textInput("userName", "Username"),
passwordInput("passwd", "Password"),
br(),actionButton("Login", "Log in")))
,tags$style(type="text/css", "#login {font-size:10px; text-align: left;position:absolute;top: 40%;left: 50%;margin-top: -10px;margin-left: -150px;}")
)}
observe({
if (USER$Logged == FALSE) {
if (!is.null(input$Login)) {
if (input$Login > 0) {
Username <- isolate(input$userName)
Password <- isolate(input$passwd)
Id.username <- which(my_username == Username)
Id.password <- which(my_password == Password)
if (length(Id.username) > 0 & length(Id.password) > 0) {
if (Id.username == Id.password) {
USER$Logged <- TRUE
USER$role=get_role(Username)
}
}
}
}
}
})
observe({
if (USER$Logged == FALSE) {
output$page <- renderUI({
box(
div(class="outer",do.call(bootstrapPage,c("",ui1()))))
})
}
if (USER$Logged == TRUE) {
output$page <- ui # ui from the first dashboard
}
})
})
With the second code I get the login box as in the following screen capture:
After I change this block :
if (USER$Logged == TRUE) {
output$page <- ui # ui from the first dashboard
}
I get the new interface on displayed but the old side menu is still there, so it's dashboard inside of another dashboard, because I just integrated the whole interface in just output$page.
Is there an easier solution to add login page to the first dashboard ?
Or a way to integrate the login (second code) with the first dashboard (first code) so the user can login and the dashboard is displayed?