How to enable a particular sub-menu in Rshiny? - r

I am developing a shinyApp using a dashboard package. In that a menu item has 2 sub-menus and the application has to react according to the selection of the sub-menu. But without selecting the sub-menu I have my data displayed. Can anyone help me to resolve this issue?
This is the code snippet used. Thanks in advance.
dashboardSidebar(
sidebarMenu(
menuItem('Modify',
menuSubItem('Edit details', tabName = 'edit'),
)
)),
dashboardBody(
tabItems(
tabItem(tabName = 'edit',
hotable('hotable1'),
downloadButton('downloadData', 'Download')
)
)

I don't think I completely understand your question but from what I gather you are questioning why the table appears on application initialization without the user clicking the menuSubItem. This is the default behavior in Shiny Dashboard, the app will start with the first menuSubItem as the default value, if you desire a particular menuSubItem as the starting sub-tab that can be achieved using the selected option under menuItem
Here is a reproducible example exhibiting the same behavior, in order to explicitly highlight this behavior I've used startExpanded = TRUE. Here you can observe the first subMenuItem is selected by default. More on childfull menuItem() can be referred here
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Modify",startExpanded = TRUE,
menuSubItem("Sub-item 1", tabName = "subitem1"),
menuSubItem("Sub-item 2", tabName = "subitem2")
)
)
),
dashboardBody(
tabItems(
tabItem("subitem1", "Sub-item 1 tab content"),
tabItem("subitem2", "Sub-item 2 tab content")
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)

Related

Can I use RShiny to open RMarkdown Files?

Is there a way I can embed RMarkdown documents in an RShiny app? That way when someone clicks on the document it opens in a new window? I know it's straightforward to embed RShiny into RMarkdown documents, but does it go the other way?
I'm working in a RShiny Dashboard, and I'd like to be able to open my menuSubItem and have the list of Rmds there.
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "MWE"),
dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem("Menu1", tabName = "dashboard", icon = icon("folder-open"),
menuSubItem("Sub Menu 1",icon = icon("folder-open"), tabName = "subMenu1")
)
)
),
dashboardBody(
tabItems(
tabItem(tabName = "subMenu1",
h2("How do I list RMarkdown files here?")
)
)
)
)
server <- function(input, output, session) {
}
shinyApp(ui, server)

Data table will not work in shinydashboard

If shinydashboard was a person I would like to meet him in a dark alley with a baseball bat. I am simply trying show a data frame as a table in my shiny app so I looked online at a bunch websites and questions on stack exchange and this is what I got.
UI
library(shiny)
library(shinydashboard)
library(data.table)#For fread.
library(tidyverse)
library(htmlwidgets)
library(DT)#For the interactive table.
# Header -----------------------------------------------------------------------|
header<-dashboardHeader( title = "Marketing Dashboard"
)
# Sidebar ----------------------------------------------------------------------|
sidebar<-dashboardSidebar(
sidebarMenu(
menuItem("Overview", tabname ="overview", icon = icon("dashboard")),
menuItem("Weather", tabname ="weather", icon = icon("bolt"))
)
)
# Body -------------------------------------------------------------------------|
body<-dashboardBody(
tabItems(
tabItem(tabName = 'overview',
DT::dataTableOutput("overviewtable")
),
tabItem(tabName = 'weather',
fluidRow(
)
)
)
)
# UI ---------------------------------------------------------------------------|
ui = dashboardPage(
header,
sidebar,
body
)
shinyApp(ui,server)
Server
server <- function(input,output){
#Table for overview
output$overviewtable<- DT::renderDataTable({
DT::datatable(tib1)
})
}
I have tried using tableOutput and renderTable
I have tried to see if my tib1 was somehow the problem so I tried using the data set mpg. That didnt work. I tried placing DT::dataTableOutput("overviewtable") in a column(), in a fluidRow. I tried reinstalling the packages for DT and refreshing R but that didnt work. I looked at the gallery website from R here. I took that code and ran it and I was able to see their table they made but when I ran my code I cant see my table at all or other tables for that matter. I have a feeling it has something to do with shinydashboard because it seems that when I ran the code from the website above that was just using shiny it worked.
The problem is a typo in the tabName = argument:
sidebar<-dashboardSidebar(
sidebarMenu(
menuItem("Overview", tabName ="overview", icon = icon("dashboard")),
menuItem("Weather", tabName ="weather", icon = icon("bolt"))
)
)

Shiny Dashboard- How to add text for Sidebar items

I was trying to add website information on my shiny dashboard and for the "About" section (See image) I want few lines to be displayed on the dashboard body when clicked on that tab. how could i possibly achieve it? I could successfully add href for the "contact" section.
Maybe I do not understand your question properly, but what about:
library(shiny)
library(shinydashboard)
header <- dashboardHeader()
sidebar <- dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem("About", icon = icon("info"), tabName = "about"),
menuItem("Contact", icon = icon("phone"), tabName = "contact")
)
)
)
body <- dashboardBody(
tabItems(
tabItem("about",
h1("About")),
tabItem("contact",
h1("Contact"))
)
)
shinyApp(
ui = dashboardPage(header, sidebar, body),
server = function(input, output) { }
)
When you click on About you get a new tab in the dashboardBody where you can display whatever you want.
Update
Based on your clarification you can use shinyjs to hide/show the relevant part:
library(shiny)
library(shinydashboard)
library(shinyjs)
header <- dashboardHeader()
sidebar <- dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem("About", icon = icon("info"), tabName = "about"),
menuItem("Contact", icon = icon("phone"), tabName = "contact")
)
)
)
body <- dashboardBody(
useShinyjs(),
fluidPage(
fluidRow(id = "mainContent",
column(12, h1("Main Content"))
),
hidden(fluidRow(id = "contact", h1("Contact Info")))
)
)
shinyApp(
ui = dashboardPage(header, sidebar, body),
server = function(input, output) {
observe({
if (input$tabs == "contact") {
hideElement("mainContent")
showElement("contact")
} else {
hideElement("contact")
showElement("mainContent")
}
})
}
)
When you now click on Contact the main part is hidden and the contact is shown. I have, however, the feeling that is a bit mis-using the idea of shinydashboard.
#thothal, it did not allow me to add as a comment coz of the length, hence posting my comment(below) as an answer.
I am sorry if I was unclear. However, your answer helped me partially. I have incorporated tabItems part in my dashboardBody section, as shown below:
dashboardBody(
fluidPage(
fluidRow(
column(12, div(dataTableOutput("dataTable")))
)
),
tabItems(
tabItem("About", h1("text to be displayed"))
)
)
but the "text to be displayed" shows up below the table.
What I want is, About section (when clicked) should display only the text and not the table. I understand this is just formatting of the code in dashboardBody section but I don't know how to do it.
Just to be more clear, my dashboard's section should display the datatable at all times and the about section when clicked should show up the text and not the datatable. I really hope this is clear. Thanks a ton for your help :)

sidebarMenu with subItems is non-responsive in dashboardbody shiny

I have the below code and upon clicking the "Dataset" in sidebar menu with sub-items, the dashboardbody does not change to the text in the tabItem. Ideally, when "Dataset" is clicked the body should display "Dataset menu sub-items content". I referred similar threads but no success. Could anyone help.
library(shiny)
library(shinydashboard)
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Documentation", tabName = "documentation"),
menuItem("Dataset", tabName="dataset",icon = icon("info"),
menuSubItem(fileInput("Input", "Upload input file:")),
menuSubItem(numericInput("samples",label="Samples",value=0,max=10)))))
body <- dashboardBody(
tabItems(
tabItem(tabName = "documentation",div(p("Dashboard tab content"))),
tabItem(tabName = "dataset",div(p("Dataset menu sub-items content")))))
ui <- dashboardPage(dashboardHeader(title = "Test"),
sidebar,
body)
server <- function(input, output, session) {
}
shinyApp(ui, server)

How can I change the background color of the selected menuItem in shinydashboard?

I have included a simple example code. If I selected Widgets, the background color of the Widgets menuItem should change.
The code is as follows
## Only run this example in interactive R sessions
if (interactive()) {
ui <- dashboardPage(
dashboardHeader(title = "Simple tabs"),
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h2("Dashboard tab content")
),
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
)
server <- function(input, output, session) {
}
}
shinyApp(ui, server)
Should I add a CSS tag? Thank You in advance!
Try the toggleClass function to toggle between CSS classes.
Usage and examples can be found here.

Resources