根据另一个向量

时间:2016-09-04 20:29:40

标签: javascript r shiny

我有一个数据矩阵,我将在tableOutput格式的Shiny应用中显示。我有一个由代码外生确定的颜色名称向量。我需要将列[i]的背景设置为color [i]。

例如,如果我有以下内容:

helpme <- data.frame(matrix(rnorm(200),nrow=20))
helpme.colors <- c(rep("white",6),
               "chocolate4",
               "darkblue",
               rep("black",2))

然后,我要做的就是这样的功能(R&#39; ish伪代码):

for(i in 1:dim(helpme)[2]){
  BackgroundColor(helpme[,i]) <- helpme.colors[i]
} 

我想答案是一个相当直接的javascript循环,但我还没有找到它(我在javascript方面的经验很少)。

1 个答案:

答案 0 :(得分:1)

你可以使用一些r包

1)//Configuration class public class Config { public string Profile { get; set; } public string Connection { get; set; } public int interval {get; set;} public bool isEnabled {get; set;} } //Worker class public class Worker { Config _config; bool IsCompleted = false; Timer timer = new Timer(); public Worker() {} public Worker(Config config) { _config = config; timer.interval = _config.interval; timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); } public void timer_Elapsed(object sender, ElapsedEventArgs e) { //To avoid collision, if previous process is not yet finish. if (!IsComplated) { return; } //Calling Method to sync data. .... .... IsComplated = true; //refresh config, if disabled than stop the timer. _config = RefreshConfig(_config); if (!_config.IsEnabled) { StopTimer(); } } public void StartWorking() { //Switch on timer; timer.Start(); } private void StopTimer() { timer.Stop(); timer.Enabled = false; }

  //The main service thread.
  public class myService:ServiceBase
  {
      List<Config> configs;
      public myService()
      {
         //Download configurations
         Configs = DownloadConfigurations()
      }

      protected override void OnStart(string[] args) 
      {
         if (configs.Length > 0)
         {
            //Run in Sync, How do I create N number of thread base on N                      number of configs
            foreach(Config _config in configs)
            {
               Worker doWork = new Worker(_config); 
               StartWorking();
            }
         }
      }

      private List<Config> DownloadConfigurations()
      {
          //Method to return Configurations
          List<Config> configurations = new List<Config>();
          ....
          ....
          return configurations; 

      }
  }

2)DT

library(DT)
ddd=datatable(helpme)
for (i in  1:ncol(helpme)){
  ddd=ddd%>%formatStyle(i,backgroundColor=helpme.colors[i])
}
ddd

PS“chocolate4”存在问题

更新

DT的光辉示例

htmlTable