0

我找到了包sparklinehttps ://github.com/htmlwidgets/sparkline

但我不知道如何使用 sparkcharts 在 markdown/html data.frame 中创建。

我知道上面的链接中有一个示例,但我不知道如何在 html 中自动创建该数据框。

4

2 回答 2

1

我认为这就是你要找的东西:https ://leonawicz.github.io/HtmlWidgetExamples/ex_dt_sparkline.html

您还可以在此处找到一个虚拟示例: R 将数据表 DT 包和迷你图包箱线图与目标值组合

举个例子:归结为虚拟示例如下(忽略数据/表中的最后一列)

            library(data.table)
            library(DT)
            library(sparkline)



            hist.A<-rnorm(100)
            hist.B<-rnorm(100)
            hist.C<-rnorm(100)

            current.A<-rnorm(1)
            current.B<-rnorm(1)
            current.C<-rnorm(1)

            #whisker should show full range of data
            boxval.A<-paste(quantile(hist.A,probs=c(0,0.25,0.5,0.75,1)),collapse = ",")
            boxval.B<-paste(quantile(hist.B,probs=c(0,0.25,0.5,0.75,1)),collapse = ",")
            boxval.C<-paste(quantile(hist.C,probs=c(0,0.25,0.5,0.75,1)),collapse = ",")

            data<-data.frame(Variable=c("A","B","C"),Current=c(current.A,current.B,current.C),boxplot=c(boxval.A,boxval.B,boxval.C))
            data$boxWithTarget<-paste(data$boxplot,data$Current,Sep=",")

            cd <- list(list(targets = 2, render = JS("function(data, type, full){ return '<span class=sparkSamples>' + data + '</span>' }")))
            line_string <- "type: 'line', lineColor: 'black', fillColor: '#ccc', highlightLineColor: 'orange', highlightSpotColor: 'orange'"
            box_string <- "type: 'box', raw:true, showOutliers:false,lineColor: 'black', whiskerColor: 'black', outlierFillColor: 'black', outlierLineColor: 'black', medianColor: 'black', boxFillColor: 'orange', boxLineColor: 'black'"
            cb = JS("function (oSettings, json) {\n  $('.sparkSeries:not(:has(canvas))').sparkline('html', { ", 
                    line_string, " });\n  $('.sparkSamples:not(:has(canvas))').sparkline('html', { ", 
                    box_string, " });\n}")


            d <- datatable(data.table(data), rownames = FALSE, options = list(columnDefs = cd, 
                                                                                 fnDrawCallback = cb))
            d$dependencies <- append(d$dependencies, htmlwidgets:::getDependency("sparkline"))
            d
于 2017-02-15T14:31:11.463 回答
1

我不确定您的问题是什么或您指的是哪个数据框。您提供的链接上的示例运行良好。尝试以下步骤

  1. 启动 R Studio
  2. 如果您还没有安装迷你图包

    library(devtools)
    install_github('htmlwidgets/sparkline')
    
  3. 利用File -> New File -> R markdown

  4. 从编辑器中的 htmlwidgets 复制示例并点击 knitr 按钮。

你会得到一个html包含几个例子的文件。

于 2015-09-18T19:45:38.023 回答