当前位置:  开发笔记 > 前端 > 正文

在R-markdown中创建具有光泽的动态图

如何解决《在R-markdown中创建具有光泽的动态图》经验,为你挑选了1个好方法。

我正在尝试使用RStudio中的Shiny创建带有动态图的RMarkdown。我的数据描述了销售点交易,如下所示:

Date|Time|Timestamp|CustomerID|Amount|City
11-Nov-15,0:00:59,11/11/15 00:00:59,6350000476,214584,City A
11-Nov-15,0:00:59,11/11/15 00:01:00,7800004763,165,City B
11-Nov-15,0:01:00,11/11/15 00:01:01,4547000063,65132,City C
11-Nov-15,0:01:01,11/11/15 00:01:34,6350000476,4676,City C
11-Nov-15,0:01:34,11/11/15 00:01:36,4657000063,16556,City A
11-Nov-15,0:01:36,11/11/15 00:02:55,7800009476,684,City A

数据集约有250,000行。

我有一个供读者选择的十个城市的清单,打算从下拉列表中进行选择,并Timestampx-axisAmount上有一个散点图y-axis。为了使可视化更清晰,我还希望在Amount滑块上选择该值。

简而言之,我正在尝试重新创建在这里找到的内容。

我尝试过的代码在这里:

{r, echo=FALSE}
require(ggplot2)

inputPanel(
 selectInput("city_choose", label = "Select city",
              choices = c("City A"="City A","City B"="City B", "City C"="City C",multiple=TRUE ), selected ="City C" ),

 sliderInput("amount_adjust", label = "Amount",min = 0, max = 350000, value = 1, step = 10000)
)

renderPlot({
  ggplot(data=d2, aes(x=timestamp,y=amount_adjust,color=city_choose))+ geom_point()
})

问题

我的数据在d2。RStudio无法“找到”它。我通过使用RScript在会话开始时将其手动加载到内存中来解决它,但是有更好的方法吗?

该图使用来自selectInput和的值sliderInput,但是我不确定如何在绘图函数中调用它们。我已经尝试过了input$city_chooseas.Text(input$city_choose)只是city_choose。没有一个可以被“发现”。我对Amount专栏有同样的问题。如示例所示,如何通过从下拉列表和滑块中获取值来绘制图形?

我也不能使用 server.R/ ui.R设置。我希望我可以自由共享RMarkdown中的输出。

Mike Wise.. 5

好吧,这基本上是可行的,尽管我不确定您打算从事哪种情节。这是R-markdown文件:

---
title: "City Plot"
author: "Someone"
date: "December 24, 2015"
runtime: shiny
output: html_document
---

Test 

```{r,echo=F}
require(ggplot2)
shinyApp(
  ui = fluidPage(
    selectInput("city_choose", label = "Select city",
               choices = c("City A"="City A","City B"="City B", 
                           "City C"="City C",multiple=TRUE ), selected ="City C" ),

    sliderInput("amount_adjust", 
                 label = "Amount",min = 1, max = 10, value = 1, step = 1),
    plotOutput("cityplt")
  ),
  server = function(input, output) {
    set.seed(1234)
    n <- 200
    sdate <- as.POSIXct("2015-11-11 00:00:00",tz="UCT")
    edate <- as.POSIXct("2015-11-11 23:59:59",tz="UCT")
    d3 <- data.frame(
      Timestamp =  as.POSIXct(runif(n,sdate,edate),tz="UCT",origin="1970-01-01"),
      Amount = runif(n,10000,1000000),
      City = sample(c("City A","City B","City C"),n,replace=T)
    )
    d3 <- d3[ order(d3$Timestamp), ]

    output$cityplt <- renderPlot({
      d4 <- d3[ d3$City==input$city_choose, ] 
      d4$AdjAmount <- input$amount_adjust*d4$Amount
      ggplot(data=d4,aes(x=Timestamp,y=AdjAmount)) + 
        geom_bar(stat="Identity") +
        labs(title=input$city_choose)
    })
  },
  options = list(height = 800)
)

```

Finished

产量:



1> Mike Wise..:

好吧,这基本上是可行的,尽管我不确定您打算从事哪种情节。这是R-markdown文件:

---
title: "City Plot"
author: "Someone"
date: "December 24, 2015"
runtime: shiny
output: html_document
---

Test 

```{r,echo=F}
require(ggplot2)
shinyApp(
  ui = fluidPage(
    selectInput("city_choose", label = "Select city",
               choices = c("City A"="City A","City B"="City B", 
                           "City C"="City C",multiple=TRUE ), selected ="City C" ),

    sliderInput("amount_adjust", 
                 label = "Amount",min = 1, max = 10, value = 1, step = 1),
    plotOutput("cityplt")
  ),
  server = function(input, output) {
    set.seed(1234)
    n <- 200
    sdate <- as.POSIXct("2015-11-11 00:00:00",tz="UCT")
    edate <- as.POSIXct("2015-11-11 23:59:59",tz="UCT")
    d3 <- data.frame(
      Timestamp =  as.POSIXct(runif(n,sdate,edate),tz="UCT",origin="1970-01-01"),
      Amount = runif(n,10000,1000000),
      City = sample(c("City A","City B","City C"),n,replace=T)
    )
    d3 <- d3[ order(d3$Timestamp), ]

    output$cityplt <- renderPlot({
      d4 <- d3[ d3$City==input$city_choose, ] 
      d4$AdjAmount <- input$amount_adjust*d4$Amount
      ggplot(data=d4,aes(x=Timestamp,y=AdjAmount)) + 
        geom_bar(stat="Identity") +
        labs(title=input$city_choose)
    })
  },
  options = list(height = 800)
)

```

Finished

产量:

推荐阅读
echo7111436
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有