【问题标题】:R: Shiny - Subset data based on selections from checkboxGroupInput"R: Shiny - 基于 checkboxGroupInput 选择的子集数据"
【发布时间】:2015-03-14 20:35:21
【问题描述】:

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

我正在做一个闪亮的应用程序来显示基于以下选择的月份: “复选框组输入”。

我正在关注本教程: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'))
    )
  )
)

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


})

【问题讨论】:

    标签: r checkbox shiny mapply


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2014-06-16
      • 2017-05-03
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多