使用shinyjs onclick并在menuSubItem上切换功能不起作用

时间:2017-02-15 04:00:14

标签: jquery shinyjs

我在UI中创建了一个框,用于在运行任务之前将参数传递给服务器(在单击runButton之后,应该隐藏或折叠框,我通过js collapse函数完成),以及当menuSubItem时单击,该框应该再次出现,并且再次单击menuSubItem时应该隐藏该框,并在再次单击时显示(类似toggle()do)。

我在服务器中添加了以下代码,但它不起作用,请你给我一些帮助: shinyjs :: onclick(“menusub1”,shinyjs :: toggle(id =“parinbox”,anim = TRUE))

并且dashSh中已添加了useShinyjs()。简单的代码如下:

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)

0 个答案:

没有答案