闪亮的dplyr和迷你线的例子

时间:2017-01-04 21:43:17

标签: r shiny sparklines

我很难将这些迷你图与Shiny一起使用。以下代码给出了所需的输出:

library(dplyr)
library(sparkline)

dfSpark <- data.frame(month = rep(1:12, len = 38),
                  year = floor(2010 + (1:38)/12),
                  value = runif(38, 0, 5))

dfSpark %>%
  group_by(month, month.abb[month]) %>%
  summarise(
    value = spk_chr(
      value, type = "line",
      chartRangeMin = 0, chartRangeMax = max(dfSpark$value)
    )
   ) %>%
  arrange(month) %>%
  ungroup() %>%
  select(-month) %>%
  setNames(., c("Month", "Trend")) %>%
  formattable() %>%
  formattable::as.htmlwidget() %>%
  spk_add_deps()

但是,当我尝试将它放在一个闪亮的应用程序中时,它返回&#34;错误:类型&#39;关闭&#39;不是子集表。&#34;

以下是Shiny App的代码:

library(shiny)
library(dplyr)
library(sparkline)

ui <- fluidPage(
  titlePanel("Sparkline Example"),
  sidebarLayout(
      sidebarPanel(
         sliderInput("mths",
                     "Number of months:",
                     min = 24,
                     max = 72,
                     value = (72+24)/2)
      ),

      mainPanel(
         sparklineOutput("sparkPlot", height = "300px")
      )
   )
)

server <- function(input, output) {

   sparkData <- reactive({
     mths <- input$mths
     dfSpark <- data.frame(month = rep(1:12, len = mths),
                           year = floor(2010 + (1:mths)/12),
                           value = runif(mths, 0, 5)
     )
     return(dfSpark)
   })
   output$sparkPlot <- renderSparkline({
     sparkData() %>%
       group_by(month, month.abb[month]) %>%
       summarise(
         value = spk_chr(
           value, type = "line",
           chartRangeMin = 0, chartRangeMax = max(sparkData$value)
         )
       ) %>%
       arrange(month) %>%
       ungroup() %>%
       select(-month) %>%
       setNames(., c("Month", "Trend")) %>%
       formattable() %>%
       formattable::as.htmlwidget() %>%
       spk_add_deps()
  })
}
shinyApp(ui = ui, server = server)

我通过添加虚拟迷你线来测试sparkPlot的输出,它也有效。

提前感谢您的帮助!

0 个答案:

没有答案