0

I have create a box in the UI for passing parameters to the server before run a task(after a runButton is clicked the box should be hidden or collapsed, i have done it through js collapse function), and when the a menuSubItem is clicked, the box should appear again, and exactly the box should be hidden when click menuSubItem again, and show when click again(something like toggle() do).

I have add the the below code in the server, but it does not works, would you please give me some help: shinyjs::onclick("menusub1",shinyjs::toggle(id="parinbox",anim = TRUE))

and the useShinyjs() has already been added in the dashboardBody. simple code is as below:

library(shiny)
library(shinydashboard)
library(shinyjs)

jsboxcollapsecode <- "shinyjs.collapse = function(boxid) {
$('#' + boxid).closest('.box').find('[data-widget=collapse]').click();
}
"
selDateRange=dateRangeInput('dateRange',label='time:',start=Sys.Date()-7,end=Sys.Date()-1) 
selcompyear=textInput("compyear",label="compyear:")
selmetsInput=selectInput(inputId="selmets",label="item:",choices=c("a","b","c"),selected=c("a","b"),multiple=TRUE)
condselDateRange=conditionalPanel("input.sidebarmenu=='subMenu1'",selDateRange)
condselcompyear =conditionalPanel("input.sidebarmenu=='subMenu2'",selcompyear)
condselmetsInput=conditionalPanel("input.sidebarmenu=='subMenu3'",selmetsInput)

runButton=actionButton(inputId="runButton",label=strong("run"),width=100)
opendirButton=actionButton(inputId="opendirButton",label=strong("opendir"),width=100)
fluidrunopenButton=fluidRow(column(4,offset=1,runButton),column(width=4,offset=1,opendirButton))

parInbox=box(id="parbox",title="parameters",status="primary",solidHeader=TRUE,collapsible=TRUE,collapsed=FALSE,width='auto',
             condselDateRange,condselmetsInput,fluidrunopenButton)
absParInPanel=absolutePanel(id="parinbox",top=80,right=0,width=300,draggable=TRUE,parInbox)                                           




ui <- dashboardPage(
  dashboardHeader(title = "Simple tabs"),
  dashboardSidebar(
    sidebarMenu(
      id ="sidebarmenu",
      actionButton('switchtab', 'Switch tab'),
      menuItem("Widgets", tabName = "widgets", icon = icon("th")),
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"),
               menuSubItem("Sub Menu 1",icon = icon("folder-open"), tabName = "subMenu1"),
               menuSubItem("Sub Menu 2",icon = icon("folder-open"), tabName = "subMenu2"),
               menuSubItem("Sub Menu 3",icon = icon("folder-open"), tabName = "subMenu3")
      )
    )
  ),
  dashboardBody(
    useShinyjs(), 
    extendShinyjs(text=jsboxcollapsecode),
    absParInPanel,
    tabItems(
      tabItem(tabName = "subMenu1",h2("Dashboard tab / Sub menu 1 content")),
      tabItem(tabName = "subMenu2",h2("Dashboard tab / Sub menu 2 content")),
      tabItem(tabName = "subMenu3",h2("Dashboard tab / Sub menu 3 content")),
      tabItem(tabName = "widgets",h2("Widgets tab content")
      )
    )
  )
  )

server <- function(input, output, session) {

  shinyjs::onclick("subMenu1",shinyjs::toggle(id="parinbox",anim = TRUE))
  shinyjs::onclick("subMenu2",shinyjs::toggle(id="parinbox",anim = TRUE))
  shinyjs::onclick("subMenu3",shinyjs::toggle(id="parinbox",anim = TRUE))
  observeEvent(input$runButton,
               {
                 js$collapse("parbox")  
               })
}

shinyApp(ui, server)
earclimate
  • 375
  • 1
  • 14
  • 1
    Are you trying to run code when the submenu is clicked? If so, the problem here is that "subMenu1" is not actually going to be the ID of the submenu element in the app. Tabs and menus in shiny are a bit awkward in the sense that their elements don't end up having the ID that you expect them to. You'll need to figure out the correct CSS selector to use. You can see [this example](https://github.com/daattali/advanced-shiny/tree/master/hide-tab) to see a related issue and how I solved it. It's using tabs instead of menu items, but hopefully you can take it from there – DeanAttali Feb 15 '17 at 04:30
  • Thanks Dean Attali. I do not want to run code when submenu is clicked. A task will be execute when the runButton is clicked, which is not included in the sample script, I want to the parameter box show/hide when the submenu is clicked, or collapsed/expand when submenu is clicked. because after each run of runButton, there is a html page generated for each tabItem. I am sorry that I am a R programer, and have little knowledge on HTML and javascript, do you mean I need specific a id for each menuSubItem? – earclimate Feb 15 '17 at 07:23
  • I am interesting in your comment: "subMenu1" is not actually going to be the ID of the submenu element in the app, but I am not sure I have understand it. I will look for more information about it. – earclimate Feb 15 '17 at 08:46

0 Answers0