Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useShinyjs() in navbarPage() #16

Closed
homerhanumat opened this issue Jun 30, 2015 · 7 comments
Closed

useShinyjs() in navbarPage() #16

homerhanumat opened this issue Jun 30, 2015 · 7 comments

Comments

@homerhanumat
Copy link

Hi Dean,

I've got an app set up laid out with navbarPage(). I'm having trouble figuring out where to place shinyjs::useShinyjs() so that the app opens correctly on the first tab panel. If you run the following example you'll see what I mean. (Note: in the example I'm using conditional panels instead of things like toggleState(). Getting that to work in navbarPage() is a separate issue; I hope to clear this one up first.)

library(shiny)
library(shinyjs)

fieldsMandatory <- c("name")

ui <- navbarPage(
  shinyjs::useShinyjs(),
  title = "A Little Survey",
  tabPanel(
    title = "Survey",
    fluidPage(
      conditionalPanel(
        condition = "input.submit == 0",
        textInput("name", "Name", ""),
        actionButton("submit", "Submit", class = "btn-primary")
      ),
      conditionalPanel(
        condition = "input.submit > 0",
        h3("Thanks for your submission!")
      )
    )
  ),
  tabPanel(
    title = "Map"
    # BLANK
  )
)

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

  observe({
    mandatoryFilled <-
      vapply(fieldsMandatory,
             function(x) {
               !is.null(input[[x]]) && input[[x]] != ""
             },
             logical(1))
    mandatoryFilled <- all(mandatoryFilled)

    shinyjs::toggleState(id = "submit", condition = mandatoryFilled)
  })

}

shinyApp(ui = ui, server = server)
@daattali
Copy link
Owner

2 things:

  1. useShinyjs() can be called literally anywhere (at least I think) in the UI, it doesn't necessarily have to be at the very beginning. So if you move it for example inside the tabPanel (either one), it will work.
  2. The problem you're seeing is related to this issue I opened with Shiny, which Joe Cheng gave me a good solution for. Look at that issue for the solution (TL;DR: use ui = tagList(useShinyjs(), navbarPage(...))

@homerhanumat
Copy link
Author

I had already tried putting the command in many other places, but the ghost panel always came up. The tagList() idea worked, though. Thanks.

@daattali
Copy link
Owner

If you put useShinyjs() inside tabPanel , the ghost tab doesn't happen. But The other solution is more proper.

library(shiny)
library(shinyjs)

fieldsMandatory <- c("name")

ui <- navbarPage(

  title = "A Little Survey",
  tabPanel(
    shinyjs::useShinyjs(),
    title = "Survey",
    fluidPage(
      conditionalPanel(
        condition = "input.submit == 0",
        textInput("name", "Name", ""),
        actionButton("submit", "Submit", class = "btn-primary")
      ),
      conditionalPanel(
        condition = "input.submit > 0",
        h3("Thanks for your submission!")
      )
    )
  ),
  tabPanel(
    title = "Map"
    # BLANK
  )
)

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

  observe({
    mandatoryFilled <-
      vapply(fieldsMandatory,
             function(x) {
               !is.null(input[[x]]) && input[[x]] != ""
             },
             logical(1))
    mandatoryFilled <- all(mandatoryFilled)

    shinyjs::toggleState(id = "submit", condition = mandatoryFilled)
  })

}

shinyApp(ui = ui, server = server)

@daattali
Copy link
Owner

Thanks for putting up the issue btw, I much prefer this than peopl emailing me, hopefully others in the future will also find this helpful

@charliejhadley
Copy link

@daattali a heads up that shiny <1.0.3 was happy enough with useShinyjs() in the first argument of navbarPage

remove.packages("shiny")
#> Removing package from '/Library/Frameworks/R.framework/Versions/3.4/Resources/library'
#> (as 'lib' is unspecified)
devtools::install_version("shiny", version = "1.0.3", repos = "http://cran.us.r-project.org")
#> Downloading package from url: http://cran.us.r-project.org/src/contrib/Archive/shiny/shiny_1.0.3.tar.gz
#> Installing shiny
#> '/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file  \
#>   --no-environ --no-save --no-restore --quiet CMD INSTALL  \
#>   '/private/var/folders/5g/g2ms7qb140j1js402p4m37jm0000gn/T/RtmpPz8R5a/devtools8cff3247c36f/shiny'  \
#>   --library='/Library/Frameworks/R.framework/Versions/3.4/Resources/library'  \
#>   --install-tests
#> 
library("shiny")
library("shinyjs")
#> 
#> Attaching package: 'shinyjs'
#> The following object is masked from 'package:shiny':
#> 
#>     runExample
#> The following objects are masked from 'package:methods':
#> 
#>     removeClass, show

ui <- shinyUI(
  navbarPage(
    useShinyjs(),
    "Foo",
    tabPanel(
      "this",
      "that"
    )
  )
)

server <- function(input, output){
  
}

shinyApp(ui, server)

But from version 1.0.4 onwards the following error is returned:

remove.packages("shiny")
#> Removing package from '/Library/Frameworks/R.framework/Versions/3.4/Resources/library'
#> (as 'lib' is unspecified)
install.packages("shiny")
#> 
#> The downloaded binary packages are in
#>  /var/folders/5g/g2ms7qb140j1js402p4m37jm0000gn/T//RtmpufpyYJ/downloaded_packages
library("shiny")
library("shinyjs")
#> 
#> Attaching package: 'shinyjs'
#> The following object is masked from 'package:shiny':
#> 
#>     runExample
#> The following objects are masked from 'package:methods':
#> 
#>     removeClass, show
ui <- shinyUI(
  navbarPage(
    useShinyjs(),
    "Foo",
    tabPanel(
      "this",
      "that"
    )
  )
)
#> Error in divTag$attribs: $ operator is invalid for atomic vectors

server <- function(input, output){
  
}

shinyApp(ui, server)
#> Error in force(ui): object 'ui' not found

@daattali
Copy link
Owner

daattali commented Sep 18, 2017 via email

@charliejhadley
Copy link

@daattali it's a royally stupid place to put it, firmly agreed and your advice pages are golden. I'm never going to trouble the shiny repo with this, the new behaviour is not a bug.

Historical Martin made a bad decision in a rush, pleasingly present day me found a reprex within 5mins of utter terror from a big thing suddenly failing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants