简体   繁体   English

带有DT和闪亮效果的R字形图

[英]R - dygraphs with DT and shiny

Here is my first question. 这是我的第一个问题。 Here is the graph. 这是图。 Why my graph does not show numbers on each lines and how can I change left side axis to display numbers rather than exponential sign 为什么我的图形未在每行上显示数字,以及如何更改左侧轴以显示数字而不是指数符号

In addition, I have some questions about DT , dygraphs and shiny . 另外,我对DT书本光泽有一些疑问。
1. I have already read my csv data in R and simply run the dygraphs as the attaching image. 1.我已经在R中读取了我的csv数据,并简单地将字形图作为附加图像。 However, if I want my image to have some numbers on lines when my mouse stay on it, how can I do? 但是,如果希望我的图像在鼠标停留在行上时有一些数字,该怎么办? SOLVED 解决了
2. Also, I want to change the y axis to display numbers rather than exponential sign. 2.另外,我想将y轴更改为显示数字而不是指数符号。 SOLVED 解决了
3. I have an idea about using shiny to show my excel data. 3.我有一个关于使用Shiny来显示我的excel数据的想法。 Thus, I use DT package with datatable function. 因此,我将DT包与datatable函数一起使用。 Moreover, how can I combine DT and dygraphs in shiny . 而且,如何将DT书本结合起来发亮 I mean when I click the datatable and select some rows or columns, there will show the dynamic line graph. 我的意思是,当我单击数据表并选择一些行或列时,将显示动态折线图。 Maybe there are other useful and convenient packages? 也许还有其他有用且方便的软件包?
4. Where can I learn some reference about shiny combining with DT . 4.在哪里可以找到有关与DT结合使用的一些参考。

following is my test.csv data input to R. 以下是我输入到R的test.csv数据。

   year month company      wp wn       ep en       pa pan npa npan
1  2015     1      Ch 6497985  1  1471586  2  4833118   3   0    0
2  2015     1      SK     350  1        0  0        0   0   0    0
3  2015     2      Ca       0  0   159703  2    36131   5   0    0
4  2015     2      Ch   88289  1 11227345  3  4305786   2   0    0
5  2015     2      Fu       0  0   794601  1        0   0   0    0
6  2015     2      Zu       0  0    70818  1   218310   9   0    0
7  2015     3      Ca       0  0    93577  1     9586   3   0    0
8  2015     3      Ch       0  0 11114302  3  1149480   4   0    0
9  2015     3      Fu       0  0   847562  1        0   0   0    0
10 2015     3      Zu       0  0   229086  2        0   1   0    0
11 2015     4      Ca       0  0    59999  1     9375   3   0    0
12 2015     4      Ch       0  0  9927702  3 16706470   8   0    0
13 2015     4      Fu       0  0  1000049  1    84655   2   0    0
14 2015     4      Zu       0  0   173894  1    74300   2   0    0

also, here is raw data 还有,这是原始数据

year,month,company,wp,wn,ep,en,pa,pan,npa,npan
2015,1,Ch,6497985,1,1471586,2,4833118,3,0,0
2015,1,SK,350,1,0,0,0,0,0,0
2015,2,Ca,0,0,159703,2,36131,5,0,0
2015,2,Ch,88289,1,11227345,3,4305786,2,0,0
2015,2,Fu,0,0,794601,1,0,0,0,0
2015,2,Zu,0,0,70818,1,218310,9,0,0
2015,3,Ca,0,0,93577,1,9586,3,0,0
2015,3,Ch,0,0,11114302,3,1149480,4,0,0
2015,3,Fu,0,0,847562,1,0,0,0,0
2015,3,Zu,0,0,229086,2,0,1,0,0
2015,4,Ca,0,0,59999,1,9375,3,0,0
2015,4,Ch,0,0,9927702,3,16706470,8,0,0
2015,4,Fu,0,0,1000049,1,84655,2,0,0
2015,4,Zu,0,0,173894,1,74300,2,0,0

I want to display the plot (test$ep) ordered by year and month(xaxis) , and the plot contains all companies. 我想显示按年和月(xaxis)排序的图表(test $ ep ,该图表包含所有公司。 SOLVED maybe other users have similar question in the future. 解决了将来其他用户可能也有类似的问题。 I post my way in the following. 我在下面发布自己的方式。

test$year <- as.Date(test$year) test $ year <-as.Date(test $ year)
time_series <- xts(test$ep, order.by = test$year) time_series <-xts(test $ ep,order.by = test $ year)
dygraph(time_series , group = test$company) dygraph(time_series,group = test $ company)

However, the plot did not show anything. 但是,该图未显示任何内容。

My solving way: 我的解决方法:

test$year <- as.yearmon(test$year , "%m %Y) test $ year <-as.yearmon(test $ year,“%m%Y)
testre <- dcast(test, year~company ,value.var = "ep") testre <-dcast(test,year〜company,value.var =“ ep”)
testre[is.na(testre)] <- 0 testre [is.na(testre)] <-0
test_xts <- xts(testre[,-1], order.by = testre$year) test_xts <-xts(testre [,-1],order.by = testre $ year)
dygraph(test_xts) dygraph(test_xts)

very appreciate. 非常感谢。
thanks for this platform so I can ask questions and learn many things from other kind users. 感谢您使用此平台,因此我可以提出问题并向其他同类用户学习很多东西。

Here is something to help you started. 这里有一些可以帮助您入门的东西。 Assuming test is your data frame, you should first (while as you did) convert from long to wide. 假设test是您的数据帧,则应首先(与您一样)将长度从长转换为宽。 Once your shiny app is running, select several rows in your DT table. 闪亮的应用程序运行后,在DT表中选择几行。

library(dygraphs)
library(xts)
library(reshape2)
library(dplyr)
library(zoo)
library(shiny)

ui = fluidPage(

      DT::dataTableOutput("x6"),
  tags$br(),
  tags$br(),
      dygraphOutput("dygraph")
  )


server = function(input, output) {

  output$x6 <- DT::renderDataTable(DT::datatable({

    test   

  }))


  output$dygraph <- renderDygraph({
    test_select <- test %>% select(year, month, company, ep)
    test_select <- test_select[c(input$x6_rows_selected), ]

    test_select_wide <- dcast(test_select, year + month ~ company, value.var="ep")

    test_select_wide$Date <- as.yearmon(paste(test_select_wide$year, test_select_wide$month, sep = "-"))
    test_select_wide$Date <- as.Date(test_select_wide$Date)

    test_select_wide$year <- NULL
    test_select_wide$month <- NULL
    #test_select_wide <- test_select_wide[,c(6,1,2,3,4,5)]

    test_select_wide_xts <- xts(test_select_wide[,-ncol(test_select_wide)], order.by=test_select_wide[,ncol(test_select_wide)])

    dygraph(test_select_wide_xts)
  })


}

shinyApp(ui=ui, server=server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM