0

Hi
I just create Shiny app. But there is a little problem when I want to present my Shiny app in navbarPage way. I mean, when I add new navbarPage named Introduction everithing works fine with plot till... I come back to Introduction navbarPage and then one again come back to Plot navbarPage, plot does not work well. What happended? How to fix it?

ui.R

library(dplyr)
library(shiny)
library(ggvis)

shinyUI(
  navbarPage("",
             tabPanel("Introduction",
                      fluidRow(
                        column(3,
                               wellPanel(
                               )

                        ),

                        column(9,
                               br(),br(),br(),
                               h5("This screen will soon display a brief introduction to the project.")
                        ) 
                      )
             ),     
             tabPanel("Plot",
                      fluidRow(
                        column(3,
                          radioButtons("dataset", label = h4("Choose dataframe"),
                                       choices = list("Item" = "df1", "Task" = "df2")),
                          selectInput("yvar", "Y-axis variable", axis_vars_y, selected = "number"),
                          uiOutput("slider")

                          ),
                    column(9,
                          ggvisOutput("plot")
        )
    )
  )
)
)

server.R

library(shiny)
library(dplyr)
library(magrittr)
library(lazyeval)

shinyServer(function(input, output) {

  datasetInput <- reactive({
    switch(input$dataset,
           df1 = df1,
           df2 = df2)
  })

  axis_vara_y <- reactive({
    switch(input$yvar,
           number = 2,
           number2 = 3)
  }) 

    output$slider <- renderUI({
      sliderInput("inslider","Slider", min   = round(min(datasetInput()[,axis_vara_y()]),0)-1, 
                                       max   = round(max(datasetInput()[,axis_vara_y()]),0)+1,
                                       value = c(round(min(datasetInput()[,axis_vara_y()]),0)-1, 
                                                 round(max(datasetInput()[,axis_vara_y()]),0)+1),
                                       step = 0.5)
})

data <- reactive({
  filteredData <- datasetInput()
  axisData <- axis_vara_y()

  if(!is.null(input$inslider)){
    filteredData <- filteredData %>%
      filter(filteredData[,axisData] >= input$inslider[1],
             filteredData[,axisData] <= input$inslider[2])
  }

  if (nrow(filteredData) == 0){
    return (datasetInput())
  }else{
    return(filteredData)
  }
})
  data_two <- reactive({
    data() %>%
      mutate(id = 1:n())

  })  

  vis <- reactive({

    yvar_name <- names(axis_vars_y)[axis_vars_y == input$yvar]
    yvar <- prop("y", as.symbol(input$yvar))

    data_two %>%
      ggvis(x = ~name, y = yvar) %>%    
      layer_points(size := 120,
                 fill = ~name,
                 fillOpacity := 0.6)

  })
  vis %>% bind_shiny("plot")
})

global.R

df1_number <-sample(seq(1,20,0.01),20,replace = T)
df2_number <-sample(seq(1,20,0.01),20,replace = T)
df1_number2 <-sample(seq(1,5,0.01),20,replace = T)
df2_number2 <-sample(seq(1,5,0.01),20,replace = T)

df1 <- data.frame(name = rep(letters[1:4],each = 5), number = df1_number, number2 = df1_number2)
df2 <- data.frame(name = rep(letters[1:4],each = 5), number = df2_number, number2 = df2_number2)
axis_vars_y <- c("number" = "number", "number2" = "number2")
Nicolabo
  • 1,337
  • 12
  • 30

1 Answers1

1

I've never had much luck putting ggvis inside of a reactive. Weird things happen like what you experienced. Try this.

shiny::runGist("https://gist.github.com/corynissen/9cdf1275f394e7945eb6")

You have to assign the y value in the data_two reactive, then run the ggvis outside of the reactive().

cory
  • 6,529
  • 3
  • 21
  • 41
  • You are correct to assign the y value to data_two but you can still make the `ggvis` reactive. It works fine once you add the y value as you demonstrate. The difference is whether you want only the data replotted or the other aspects (e.g. title) as well. See [this question](http://stackoverflow.com/questions/25011544/how-is-data-passed-from-reactive-shiny-expression-to-ggvis-plot/25060999#25060999) for a good explanation between reactive or non-reactive ggvis objects. – cdeterman Feb 12 '15 at 15:19
  • @ cdeterman @cory OK, thanks but this solution solve the problem partly. I mean try this scenario: `Introduction Panel` -> `Plot Panel` -> change to Task `dataframe` -> change `Y-axis variable` to `number2` -> come back to `Introduction Panel` -> go to `Plot Panel` once again -> change `Y-axis variable` to `number`. As a result , plot loses all `a` and `b` dot points and show only some `c` and `d` dot points. Is it possible to improve it? – Nicolabo Feb 12 '15 at 15:42
  • Try my gist again. I used @cdeterman's advice and tried the ggvis inside the reactive. It's a little clunkier in the browser (reloads entire plot on yvar change), but points don't randomly disappear. I replace data_two() and the ggvis with one reactive for data and plot – cory Feb 12 '15 at 16:18
  • @ cory Thanks!, I see difference.. in that case I think Shiny must be developed to manage with build in transition animations of ggvis , I hope it may possible one day! – Nicolabo Feb 12 '15 at 18:29