简体   繁体   English

R Shiny 根本没有绘图

[英]R Shiny not plotting at all

I'm new to shiny and I'm trying to use it for a simulation of a prey/predator model.我是 shiny 的新手,我正在尝试用它来模拟猎物/捕食者 model。

First, I wanted to generate the dataframe with all the initial positions for each animal;首先,我想生成 dataframe 以及每只动物的所有初始位置; and try to plot it usign ggplot;并尝试使用 plot 它使用 ggplot; but when I hit the actionButton, the plot never showed.但是当我点击 actionButton 时,plot 从未出现。 I dont interstand why and there is any error message to let me at least know what is wrong.我不明白为什么,并且有任何错误消息让我至少知道出了什么问题。

Here is the code:这是代码:

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)

Thank you for reading this谢谢您阅读此篇

Simple fix: Remove p <- and you should be good to go.简单修复:删除 p <- 你应该对 go 很好。 However, to improve you need to check the reactivity of your execution when the nPrey and Hunter are dynamically changing.但是,为了改进,您需要在 nPrey 和 Hunter 动态变化时检查执行的反应性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM