Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

"Error in: Length of logical index vector" when creating ggvis stack bar chart with Shiny & DT DataTable

$
0
0

I am trying to build a shiny dashboard which has a chart and a HTML5 DataTable from DT Package below the chart. The chart is connected to the DataTable such that filtering the DataTable will filter the chart.

I am able to successfully bring this functionality for a ggvis scatter plot but when I try to do the same for a bar plot I get the error Error in: Length of logical index vector must be 1 or 10, got: 0

Here is the server.R for the scatter plot which works

server <- function(input, output) { 


  server_DF <- data.frame(dataset)


  output$html_data_table <- DT::renderDataTable({
    DT::datatable(unique(server_DF[, input$show_vars, drop = FALSE]),
                  filter = "top", selection = "multiple")
  })



  filtered_rows <- reactive({
    input$html_data_table_rows_all
  })



  vis <- reactive({
        # filtered_rows <- input$html_data_table_rows_all

        xvar <- prop("x", as.symbol(input$xvar))
        yvar <- prop("y", as.symbol(input$yvar))
        colvar <- prop("fill", as.symbol(input$colvar))                                   


        p <- server_DF[filtered_rows(),] %>%
        ggvis(x = xvar, y = yvar, fill := colvar) %>%
        layer_points() %>%
        set_options(width = "auto", height = "auto", resizable=FALSE)

        p


          })



  vis %>% bind_shiny("plot", "ggvis_ui")


}

Here is the code for the bar plot that gives me the error. Note that the only thing different is the layer_bars() which was layer_points() in the above code.

server <- function(input, output) { 


  server_DF <- data.frame(dataset)


  output$html_data_table <- DT::renderDataTable({
    DT::datatable(unique(server_DF[, input$show_vars, drop = FALSE]),
                  filter = "top", selection = "multiple")
  })



  filtered_rows <- reactive({
    input$html_data_table_rows_all
  })



  vis <- reactive({
        # filtered_rows <- input$html_data_table_rows_all

        xvar <- prop("x", as.symbol(input$xvar))
        yvar <- prop("y", as.symbol(input$yvar))
        colvar <- prop("fill", as.symbol(input$colvar))                                   


        p <- server_DF[filtered_rows(),] %>%
        ggvis(x = xvar, y = yvar, fill := colvar) %>%
        layer_bars() %>%
        set_options(width = "auto", height = "auto", resizable=FALSE)

        p


          })



  vis %>% bind_shiny("plot", "ggvis_ui")

}

Found that if I remove the fill := colvar it produces the histogram. But when I put in the fill := colvar like I did for the scatter plot I get an error.

Here is the complete code with ui.R and the sample data for the scatter plot. Just want to replicate the below for the stacked bar.

library(shiny)
library(data.table)
library(ggvis)
library(DT)


data("diamonds")

dataset <- diamonds
rm(diamonds)

axis_vars <- names(dataset)

ui <- fluidPage(pageWithSidebar(

  headerPanel("Sample Dashboard"),

  sidebarPanel(

    selectInput('xvar', 'X Axis', axis_vars, selected = axis_vars[2]),
    selectInput('yvar', 'Y Axis', axis_vars, selected = axis_vars[7]),
    selectInput("colvar", "Colour", axis_vars, selected = axis_vars[4])

  ),

  mainPanel(
    fluidRow(
      ggvisOutput("plot")
    ),

    fluidRow(
    column(width = 4,
           checkboxGroupInput('show_vars', 'Select Columns To Display',
                              names(dataset),selected = names(dataset))
           ),
    column(width = 8,
           div(style = 'overflow-x: scroll', DT::dataTableOutput('html_data_table'))
           )

    )

  )
))


server <- function(input, output) { 


  server_DF <- data.frame(dataset)


  output$html_data_table <- DT::renderDataTable({
    DT::datatable(unique(server_DF[, input$show_vars, drop = FALSE]),
                  filter = "top", selection = "multiple")
  })



  filtered_rows <- reactive({
    input$html_data_table_rows_all
  })



  vis <- reactive({
    # filtered_rows <- input$html_data_table_rows_all

    xvar <- prop("x", as.symbol(input$xvar))
    yvar <- prop("y", as.symbol(input$yvar))
    colvar <- prop("fill", as.symbol(input$colvar))                                   


    p <- server_DF[filtered_rows(),] %>%
      ggvis(x = xvar, y = yvar, fill = colvar) %>%
      layer_points()

    p


  })



  vis %>% bind_shiny("plot")


}

shinyApp(ui, server)

Been struggling with this for a couple of days. Any help will be greatly appreciated.


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>