我试图使用RStudio中的闪亮来创建带有动态图的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行。
我有一个十个城市的列表,我打算从一个下拉列表中选择读者,Timestamp
上的散点图为x-axis
Amount
上的y-axis
。为了在可视化中提供清晰度,我还希望在滑块上选择Amount
值。
简单地说,我试图重新创建找到的内容here。
我试过的代码在这里:
{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_choose
,as.Text(input$city_choose)
和city_choose
。其中没有一个可以找到'。我对Amount
列有同样的问题。如何通过从下拉列表和滑块中获取值来绘制图形,如示例所示?server.R
/ ui.R
设置。我想要RMarkdown中的输出,我可以自由分享。答案 0 :(得分:4)
这里有一些基本上有用的东西,虽然我不确定你追求的是什么样的情节。这是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
的产率: