gpt4 book ai didi

r - Shiny 没有像我期望的那样显示我的 ggplot

转载 作者:行者123 更新时间:2023-12-04 01:38:11 25 4
gpt4 key购买 nike

是什么让我的 Shiny 小应用程序无法显示我的 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")
)))

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


})
})

最佳答案

这里有两个问题。

首先,您不应该在 aes 中设置子集-- 它需要列名。相反,将您提供给 ggplot 的 data.frame 子集化(感谢来自 R 聊天的@Roland)

其次,必须明确print您 Shiny 的应用程序中的 ggplot 对象。

尝试这个:

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

关于r - Shiny 没有像我期望的那样显示我的 ggplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com