gpt4 book ai didi

R Shiny 根本没有绘图

转载 作者:行者123 更新时间:2023-12-04 10:14:37 25 4
gpt4 key购买 nike

我是 Shiny 的新手,我试图用它来模拟猎物/捕食者模型。

首先,我想生成包含每个动物所有初始位置的数据框;并尝试使用 ggplot 绘制它;但是当我点击 actionButton 时,情节从未显示。我不明白为什么,并且有任何错误消息让我至少知道出了什么问题。

这是代码:

library(shiny)
library(tidyverse)
library(ggplot2)

ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("nPrey", "select total number of preys", 1, 100, 10, 1),
sliderInput("nHunter", "select total number of Hunters", 1, 100, 10, 1),
actionButton ("play", "Begin simulation")
),
mainPanel(
plotOutput("plot")
)
)
)

server <- function(input, output, session) {
zMax = 20
simulation <- eventReactive(input$play, {
createInitialTable(input$nPrey, input$nHunter)
})
output$plot <- renderPlot({
p <- ggplot() +
geom_point(aes_string(x="X",y="Y"), data=simulation()) +
coord_cartesian(xlim =c(0, zMax), ylim = c(0, zMax))
})
createInitialTable <- function (nPrey, nHunter){
aAnimal <- data.frame()
cVar <- c("X", "Y")
for (i in 1:nPrey){
aAnimal <- rbind(aAnimal, c(round(runif(1)*zMax), round(runif(1)*zMax)))
}
for (i in 1:nHunter){
aAnimal <- rbind(aAnimal, c(round(runif(1)*zMax), round(runif(1)*zMax)))
}
colnames(aAnimal) <- cVar
return (aAnimal)
}
}

shinyApp(ui, server)

谢谢您阅读此篇

最佳答案

简单的修复:删除 p <- 你应该很高兴。但是,为了改进,您需要在 nPrey 和 Hunter 动态变化时检查执行的 react 性。

关于R Shiny 根本没有绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61140842/

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