在闪亮的内部使用tapply产生摘要输出

时间:2018-06-28 16:36:56

标签: r shiny leaflet tapply

下面的代码是可复制的:

public class JUnitRunnerWithTags extends JUnitStories {

    public JUnitRunnerWithTags() {
        configuredEmbedder().useMetaFilters(Arrays.asList("**+TestType Smoke**"));
    }

...

对于统计信息输出,我收到一条错误消息:

  

错误:参数必须具有相同的长度。

有人知道如何解决该问题吗?除此之外,我还将所有小部件显示为红色,但是我还有另一条帖子询问小部件问题。如果有人可以帮助我,将不胜感激。先感谢您!

1 个答案:

答案 0 :(得分:1)

调试:

如果我们调试您的代码,我会看到您尝试这样做:

crime2 <- crime[1:50,]
with(crime2, tapply("Hour", list(type), summary))

除了闪亮,我想您想要的输出是:

with(crime2, tapply(X = hour, INDEX = type, FUN = summary))

在发亮的状态下,您想通过输入进行访问,因此要使用字符。因此,您可以将代码重写为:

tapply(X = unlist(crime2["hour"]), INDEX = crime2$type, FUN = summary)

或动态:

tapply(X = unlist(crime2[input$var]), INDEX = crime2$type, FUN = summary)

完整的可复制示例为:

({input$var选项必须更改为小写才能启用索引,...)

library(shiny)
library(Rcpp)
library(ggmap)
library(htmlwidgets)
library(leaflet)

crime2 <- crime[1:50,]

ui <- fluidPage(
  titlePanel("Unusual Observations"),

  sidebarLayout(
    sidebarPanel(
      helpText("Create maps with 
               information from the Crime Data"),

      selectInput("var", 
                  label = "Choose a variable to display",
                  choices = c("hour",
                              "number"),
                  selected = "hour"),

      sliderInput("range", 
                  label = "Range of interest:",
                  min = 0, max = 10, value = c(1, 2))
      ),

    mainPanel(leafletOutput("map"))
  ),

  verbatimTextOutput("stats")
  )

server <- function(input, output) {
  output$map <- renderLeaflet({
    data <- switch(input$var,
                   "hour" = crime2$hour,
                   "number" = crime2$number)

    getColor <- function(data){sapply(data, function(var){
      if(input$var< input$range[1]) {
        "green"
      } else if(input$var <= input$range[2]) {
        "orange"
      } else {
        "red"
      } })
    }

    icons <- awesomeIcons(
      icon = 'ios-close',
      iconColor = 'black',
      library = 'ion',
      markerColor = getColor(crime2)
    )

    leaflet(crime2) %>%
      addTiles() %>%
      addAwesomeMarkers(~lon, ~lat, icon=icons)

  })

  output$stats <- renderPrint({
    tapply(X = unlist(crime2[input$var]), INDEX = crime2$type, FUN = summary)
  })
}

shinyApp(ui = ui, server = server)