2

I am making my first shiny app- a histogram with a slider input. However, I want to use a user-defined function to produce output for this histogram. Here is the chunk of code regarding the histogram:

#Define server logic required to draw a histogram
server <- function(input, output) {
  output$distPlot <- renderPlot({

 #Use the function for a constant risk, but sample uniformly over CI for OR
 #95% CI: 1.19-1.41

  myrisk<-input$risk
  random<-runif(1000, min = 1.19, max = 1.41)

  output<-sapply(random, myfunction, risk=myrisk)

 hist(output, xlab="Number of Cases Prevented", 
      main="Histogram of 1000 Simulations")
  })
}

Note: I have 'myfunction' defined earlier in the code, before I call in the shiny library. Does anyone see any indication why this won't be running?

I tried debugging it, and the trouble is coming from the 'output' line.

Thanks!

  • 5
    What's the exact error? When asking for help, it's best if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Also, it's unclear why you are assigning to `output` here. What are you hoping that will accomplish? You don't see to use it in the histogram. The `output` that gets passed to the server function is usually how you send stuff to the UI layer. – MrFlick Oct 04 '18 at 21:08
  • What happens if you define `myfunction` inside `server`? Does it work? – antonioACR1 Oct 04 '18 at 22:47
  • @MrFlick - thank you! You are right, it was getting what I defined as output confused with the output of the server function. Once I renamed it, everything worked properly. Thanks for your help! – statistics123 Oct 10 '18 at 14:28

0 Answers0