是什么让我的小闪亮应用程序显示我的ggplot?当我使用基本绘图函数替换renderPlot()中的代码时,它汇集在一起.我在Windows Vista上使用RStudio,R v3.0.1,输出到Chrome浏览器.
ui.r
library(ggplot2)
cities <- c("Anchorage","Fairbanks","Juenau","Wasilla","Homer")
years <- 2003:2013
Table <- "Capital Assets"
Account <- c("Land", "Art", "Buildings", "Equipment")
dat <- data.frame(City = sort(rep(cities, length(years))), Year = rep(years,length(cities)), Table)
sampleDat <- rbind(data.frame(dat,Acount = Account[1]), data.frame(dat, Acount = Account[2]), data.frame(dat, Acount = Account[3]), data.frame(dat, Acount = Account[4]))
finalDat <- data.frame(sampleDat, Value = runif(length(sampleDat[,1]), 1000,10000) )
shinyUI(pageWithSidebar(
headerPanel("CAFR Explorer"),
selectInput("city","City", as.list(levels(finalDat$City)), selected = NULL, multiple = FALSE),
mainPanel(
h3(textOutput("caption")),
plotOutput("CAFRplot")
)))
Run Code Online (Sandbox Code Playgroud)
server.r
library(shiny)
library(ggplot2)
cities <- c("Anchorage","Fairbanks","Juenau","Wasilla","Homer")
years <- 2003:2013
Table <- "Capital Assets"
Account <- c("Land", "Art", "Buildings", "Equipment")
dat <- data.frame(City = sort(rep(cities, length(years))), Year = rep(years,length(cities)), Table)
sampleDat <- rbind(data.frame(dat,Acount = Account[1]), data.frame(dat, Acount = Account[2]), data.frame(dat, Acount = Account[3]), data.frame(dat, Acount = Account[4]))
finalDat <- data.frame(sampleDat, Value = runif(length(sampleDat[,1]), 1000,10000) )
shinyServer(function(input, output) {
formulaText <- reactive({
paste(input$city)
})
output$caption <- renderText({
formulaText()
})
output$CAFRplot <- renderPlot({
## this one isn't working.
ggplot(finalDat, aes(x = finalDat[which(finalDat$City == input$city),2],
y = finalDat[which(finalDat$City == input$city),5])) +
geom_point()
## this one is working
#plot(finalDat[which(finalDat$City == input$city),2], y = finalDat[which(finalDat$City == input$city),5])
})
})
Run Code Online (Sandbox Code Playgroud)
GSe*_*See 18
这里有两个问题.
首先,您不应该在子集中aes- 它需要列名称.相反,将您提供的data.frame分组ggplot(感谢来自R chat的@Roland)
其次,你必须print在闪亮的应用程序中明确你的ggplot对象.
试试这个:
p <- ggplot(finalDat[finalDat$City == input$city,], aes(x = Year, y = Value))
p <- p + geom_point()
print(p)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7936 次 |
| 最近记录: |