R: Shiny - 基于 checkboxGroupInput 选项的子集数据”

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

我有一个包含 3 列的数据框:Mes、Visitas、Pedidos。

代码:

    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")

我正在做一个闪亮的应用程序来根据以下选项显示月份: "checkboxGroupInput"。

我正在学习本教程:http://shiny.rstudio.com/gallery/datatables-demo.html。除了我正在做一个基于行的子集(对于 "Mes" 而不是按列(如示例中所示)。

但是我得到这个错误:

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


})

你快到了。您必须使用 row.names() 而不是 names()。然后将数据的行名更改为数据的第一列。

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)