闪闪发光不显示我的ggplot,因为我期望 [英] Shiny not displaying my ggplot as I'd expect

查看:146
本文介绍了闪闪发光不显示我的ggplot,因为我期望的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么让我的小Shiny应用程序显示我的ggplot?当我用一个使用基本绘图函数的例子替换renderPlot()中的代码时,它将汇集在一起​​。我在Windows Vista上使用RStudio,R v3.0.1,输出到Chrome浏览器。



ui.r

$ (安克雷奇,费尔班克斯,朱诺,美国), (Wasilla,Homer)
年< - 2003:2013
表格< - 资产资产
账户<-c(Land,Art (城市,长度(年))),年份=代表(年份,长度(城市)),表格)
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)
)))

server.r

 图书馆(闪亮)
图书馆(ggplot2)

城市< - c(Anchorage,Fairbanks,Juenau,Wasilla,Homer)
年< - 2003:2013
表格< - 资本资产
账户< -c(土地,艺术,建筑物,设备)
dat< - data.frame(城市=排序(rep(城市,长度(年))) = rep(年,长度(城市)),表)
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 $城市)
})

输出$ caption< - renderText({
formulaText()
})

输出$ CAFRplot < - renderPlot({

##这一个不起作用。
ggplot(finalDat,aes(x = finalDat [which(finalDat $ City == input $ city),2],
y = finalDat [which(finalDat $ City == input $ city),5] ))+
geom_point()

##这个工作是
#plot(finalDat [which(finalDat $ City == input $ city),2],y = finalDat [which(finalDat $ City == input $ city),5])


})
})


解决方案

这里有两个问题。

首先,您不应该在 aes 中加入子集 - 它需要列名称。相反,将您提供给 ggplot 的data.frame(来自R聊天中的@Roland)进行子集化处理。



,你必须明确打印你闪光的应用程序中的ggplot对象。



试试这个:

  p < -  ggplot(finalDat [finalDat $ City == input $ city,],aes(x = Year,y = Value))
p < - p + geom_point()
print(p)


What's keeping my little Shiny app from displaying my ggplot? When I replace the code in renderPlot() with an example using the base plot function it comes together. I'm using RStudio, R v3.0.1 on Windows Vista, outputting to a Chrome browser.

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")
)))   

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])


  })
})

解决方案

There are two issues here.

First, you shouldn't subset in aes -- it expects column names. Instead, subset the data.frame that you provide to ggplot (thanks to @Roland from R chat)

Second, you must explicitly print your ggplot object in your shiny app.

Try this:

p <- ggplot(finalDat[finalDat$City == input$city,], aes(x = Year, y = Value))
p <- p + geom_point()
print(p)

这篇关于闪闪发光不显示我的ggplot,因为我期望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆