0

我有一个包含 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

用户界面

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

服务器.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


})
4

1 回答 1

1

你快到了。您必须使用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)
于 2015-03-15T09:28:25.083 回答