0

I have this shiny app where I am taking inputs from the user and depending on the type of plot the user wants it will show that plot. But I am unable to take the value of the radio button on the app and use it to draw a specific ggvis plot because the "input$ " value can only be used within a render* function which I am not using here because I am drawing with ggvis.

My ui.R file -

library(ggvis)

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("Xvar", "Choose X variable", choices = colnames(the.data), selected = colnames(the.data)[[2]] ),
      selectInput("Yvar", "Choose Y variable", choices = colnames(the.data), selected = colnames(the.data)[[3]] ),
      selectInput("IDvar", "Choose ID variable", choices = colnames(the.data), selected = colnames(the.data)[[4]] ),
      uiOutput("choose_COVvar"),
      uiOutput("choose_COVn"),
      br()
    ),
    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Data Exploration", 
                           uiOutput("plot_type"), 
                           br(), 
                           ggvisOutput("ggvis_xy_plot")),
                           ggvisOutput("ggvis_profile_plot")  
      )
    )
  )
)) 

the server.R file -

library(ggvis)
library(shiny)


shinyServer(function(input, output, session) {
  the.data <<- mtcars

  output$plot_type <- renderUI({
    fluidRow(
      column(4, offset = 1,
             radioButtons("PlotMethod", h5("Plot type"), c("XY Scatter plot",
                                                           "profile plot"))
      )
    )
  })

  flex.data <- reactive({
    x.name <- input$Xvar
    y.name <- input$Yvar
    id.name <- input$IDvar

    x.data <- the.data[, x.name]
    y.data <- the.data[, y.name]
    ID.t <- the.data[, id.name]
    new.data <- data.frame(x.data, y.data, ID.t)
  })

  lb <- linked_brush(keys = 1:nrow(flex.data()), "red")

  flex.data %>%
    ggvis(~x.data, ~y.data) %>%
    layer_points(fill := lb$fill, fill.brush := "red") %>%
    lb$input()  %>%
    layer_points(fill := "red", data = reactive(flex.data()[flex.data()$ID.t %in%
                                                              flex.data()[lb$selected(), ]$ID.t, ])) %>% 
    bind_shiny("ggvis_xy_plot")

  flex.data %>%
    ggvis(~x.data, ~y.data) %>%
    layer_points() %>%
    layer_lines() %>%
    bind_shiny("ggvis_profile_plot")
})

As you can see, currently both the scatter plot and the line plot is there but I want it to be just one depending on the radio button above, the user has pressed.

Any help will be greatly appreciated. Thanks

nafizh
  • 185
  • 3
  • 14

1 Answers1

0

I edited both your ui.R and your server.R. Have a look at the comments:

ui.R

library(ggvis)

shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("Xvar", "Choose X variable", choices = colnames(the.data), selected = colnames(the.data)[[2]] ),
      selectInput("Yvar", "Choose Y variable", choices = colnames(the.data), selected = colnames(the.data)[[3]] ),
      selectInput("IDvar", "Choose ID variable", choices = colnames(the.data), selected = colnames(the.data)[[4]] ),
      uiOutput("choose_COVvar"),
      uiOutput("choose_COVn"),
      br()
    ),
    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Data Exploration", 
                           uiOutput("plot_type"), 
                           br(), 
                           ggvisOutput("plot"))
      )
    )
  )
)) 

server.R

library(ggvis)
library(shiny)

shinyServer(function(input, output, session) {
  the.data <<- mtcars

  output$plot_type <- renderUI({
    fluidRow(
      column(4, offset = 1,
             radioButtons("PlotMethod", h5("Plot type"), c("XY Scatter plot",
                                                           "profile plot"))
      )
    )
  })

  flex.data <- reactive({
    x.name <- input$Xvar
    y.name <- input$Yvar
    id.name <- input$IDvar

    x.data <- the.data[, x.name]
    y.data <- the.data[, y.name]
    ID.t <- the.data[, id.name]
    new.data <- data.frame(x.data, y.data, ID.t)
  })

  lb <- linked_brush(keys = 1:nrow(flex.data()), "red")

  # Set binder as a reactive expression, returning an object dependent on the radio buttons
  # By default, take the first one, i.e. if "profile plot" is selected or the radio button has not been loaded yet
  binder <- reactive({
    if(!is.null(input) & !is.null(input$PlotMethod)) {
      if(input$PlotMethod == "profile plot") {
        plt <- flex.data %>%
          ggvis(~x.data, ~y.data) %>%
          layer_points() %>%
          layer_lines()
      }
    }
    if (!exists("plt")) {
      plt <- flex.data %>%
        ggvis(~x.data, ~y.data) %>%
        layer_points(fill := lb$fill, fill.brush := "red") %>%
        lb$input()  %>%
        layer_points(fill := "red", data = reactive(flex.data()[flex.data()$ID.t %in%
          flex.data()[lb$selected(), ]$ID.t, ]))      
    }
    plt
  })
  binder %>% bind_shiny("plot") # Bind whatever is behind binder
})
Xiphias
  • 4,468
  • 4
  • 28
  • 51
  • Thanks for taking a look at this. It almost works. The problem is now the linked brush is not working anymore in the scatter plot. Before, if I brushed a point, all the other points with the same ID would become red as well. Now it is not working. – nafizh Aug 21 '15 at 16:02