简体   繁体   中英

R: Shiny - Subset data based on selections from checkboxGroupInput"

I've a data frame with 3 columns: Mes, Visitas, Pedidos.

Code:

    structure(list(Mes = structure(1:12, .Label = c("Enero", "Febrero", 
"Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", 
"Octubre", "Noviembre", "Diciembre"), class = "factor"), Visitas = c(100L, 
200L, 300L, 400L, 500L, 600L, 700L, 800L, 900L, 1000L, 1100L, 
1200L), Pedidos = c(20L, 40L, 60L, 80L, 100L, 120L, 140L, 160L, 
180L, 200L, 220L, 240L)), .Names = c("Mes", "Visitas", "Pedidos"
), row.names = c(NA, -12L), class = "data.frame")

I'm doing a Shiny App to show months based on the selection from: "checkboxGroupInput".

I'm following this tutorial: http://shiny.rstudio.com/gallery/datatables-demo.html . Except that i'm doing a subset based on the rows (for "Mes" and not by columns (as in the example).

But i get this error:

Listening on http://127.0.0.1:7026
Error in mapply(ids, choices, names(choices), FUN = function(id, value,  : 
  zero-length inputs cannot be mixed with those of non-zero length

ui.R

library(shiny)
library(ggplot2)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput('meses', 'Elige meses:',
                         names(OmarSessiones$Meses),
                         selected = names(OmarSessiones$Meses))
    ),

    # Show a plot of the generated distribution
    mainPanel(
      dataTableOutput('mytable1'))
    )
  )
)

server.R

library(shiny)


OmarSessiones <- read.csv2("D:\\omarmeses.csv", 
                          header = T)







# Define server logic required to draw a histogram
shinyServer(function(input, output) {





    output$mytable1 <- renderDataTable({
      library(ggplot2)
      OmarSessiones[input$meses,]
    })




  # Expression that generates a histogram. The expression is
  # wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should re-execute automatically
  #     when inputs change
  #  2) Its output type is a plot


})

You were almost there. You have to use row.names() instead of names() . Then change rownames of your data to be first column of your data.

library(shiny)
library(ggplot2)
OmarSessiones <- structure(list(Mes = structure(1:12, .Label = c("Enero", "Febrero", 
"Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", 
"Octubre", "Noviembre", "Diciembre"), class = "factor"), Visitas = c(100L, 
200L, 300L, 400L, 500L, 600L, 700L, 800L, 900L, 1000L, 1100L, 
1200L), Pedidos = c(20L, 40L, 60L, 80L, 100L, 120L, 140L, 160L, 
180L, 200L, 220L, 240L)), .Names = c("Mes", "Visitas", "Pedidos"
), row.names = c(NA, -12L), class = "data.frame")
# Change rownames
row.names(OmarSessiones) <- OmarSessiones$Mes
server <- function(input, output, session) {


     output$mytable1 <- renderDataTable({
      library(ggplot2)
      OmarSessiones[input$meses,]
    })


}

ui <- fluidPage(

     # Application title
  titlePanel("Hello Shiny!"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput('meses', 'Elige meses:',
                         row.names(OmarSessiones),
                         selected = row.names(OmarSessiones))
    ),

    # Show a plot of the generated distribution
    mainPanel(
      dataTableOutput('mytable1'))
    )
    )


shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM