I need help in shiny :(

Hi! i would like to know how i can conect my shiny code with this formula that i make:

Bernoulli=function(am,pm,nummuestrasm,tamañom,dis){
  a=am
  p=pm
  numues=numuestrasm
  tam=tamañon
  if(dis=="Bernoulli"){
    x<-c()
    for (i in 1:a){
      if(runif(1)<p){x[i]<<-1}
      else
      {x[i]<<-0}}
    medias2<-c()
    for(i in 1:numues){
      medias2[i]<-mean(sample(x,size =tam,replace=FALSE))}
    return(x)
  }}

this code its the bernoulli, and it generates a list of number.. but how i can put in shiny.. i have this in shiny..

ui...

# Define UI for application that draws a histogram
shinyUI(fluidPage(
  
  # Application title
  titlePanel("Distribuciones Continuas y Discretas"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      
       
       sliderInput("bins",
                   "Numero de Intervalos:",
                   min = 1,
                   max = 50,
                   value = 30),
       selectInput("dis","Selecciona la distribucion", choices = c("Uniforme Discreta","Bernoulli","Binomial","Geometrica","Binomial Negativa",
                              "Poisson","Uniforme","Exponencial","Gamma","Beta","Normal","Weibull","Lognormal")),
       sliderInput("numues","Numero de muestras",min = 1,max = 100,value = 20),
       sliderInput("tam","tamano", min = 1,max = 50,value = 3),
       textInput("a","Numero de iteraciones",1),
       conditionalPanel(condition = "input.dis == 'Uniforme Discreta'", 
                        textInput("b","Escribe b",1),
                        textInput("n","Escribe n",2)),
       conditionalPanel(condition ="input.dis ==  'Bernoulli'",
                        textInput("p","escribe p",0.1)),
       conditionalPanel(condition="input.dis == 'Binomial'", textInput("p","escribe p",0.1)),
       conditionalPanel(condition="input.dis == 'Geometrica'",textInput("p","escribe p",0.1)),
       conditionalPanel(condition="input.dis == 'Binomial Negativa'",textInput("r","escribe r",1),
                        textInput("p","escribe p",0.1)),
       conditionalPanel(condition = "input.dis == 'Poisson'",textInput("lamda","escribe lambda",10)),
       conditionalPanel(condition = " input.dis == 'Uniforme'",textInput("b","escribe b",1),
                        textInput("j","escribe j",1)),
       conditionalPanel(condition = "input.dis == 'Exponencial'",textInput("beta","escribe beta",1)),
       conditionalPanel(condition =" input.dis == 'Gamma'", textInput("alfa","escribe alfa",1)),
       conditionalPanel(condition ="input.dis == 'Beta'",textInput("alfa","escribe alfa",1),textInput("beta","escribe beta",1)),
       conditionalPanel(condition ="input.dis == 'Normal'",textInput("med","ingresa media",1),textInput("sd","ingresa la desviacion estandar",2)),
       conditionalPanel(condition ="input.dis == 'Weibull'",textInput("alfa","ingresa alfa",1),textInput("beta","ingresa beta",1)),
       conditionalPanel(condition ="input.dis == 'Lognormal'",textInput("med","ingresa la media",1),textInput("sd","ingresa la desviacion estandar",2))
       
    )
    ,
    
    # Show a plot of the generated distribution
    mainPanel(
       plotOutput("distPlot")
    )
  )
))

this code its to put the parameters..

and i have this in server..

library(shiny)



# Define server logic required to draw a histogram
shinyServer(function(input, output) {
   
  output$distPlot <- renderPlot({
    
    
   
    
    # generate bins based on input$bins from ui.R
    x<- faithful[,2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'blue', border = 'white',freq = FALSE,main = "Histograma",xlab = "Datos Generados")
    lines(density(x),lwd = 3, col = "red")
    
  })
  
})

.. my question is how i can use the things that i put in ui to use with the code ( bernoulli) please help me ( the idea is conect the things that i put in ui, with server and the code that i put)

I am not entirely sure where in you r server code you wish to put your bernoulli() function, however, if this function is defined in the same script as your app, then you can simply call it like any other function. If you want it to be reactive to different inputs then you should put it either in a reactive() or eventReactive() depending on whether you want the changes to occur with or without a button click or some other event.

It looks like your function is trying to work based on whether or not input$dis equals "Bernoulli" or not. This is likely going to cause you issues. I would recommend taking that if statement out of the function, and rather putting it in the actual shiny code so your function would look like this:

Bernoulli=function(am,pm,nummuestrasm,tamañom,dis){
  a=am
  p=pm
  numues=numuestrasm
  tam=tamañon
  x<-c()
  for (i in 1:a){
    if(runif(1)<p){x[i]<<-1}
  else {x[i]<<-0}}

  medias2<-c()
  for(i in 1:numues){
    medias2[i]<-mean(sample(x,size =tam,replace=FALSE))}
  return(x)
}

then in your server code, you can do something like this:

shinyServer(function(input, output) {

  x <- reactive({
     if (input$dis == "Bernoulli") {
       output <- Bernoulli(...) # not sure what inputs you want for your function but you would replace ... with them
     } else {
        x<- faithful[,2]
     }
  })

  output$distPlot <- renderPlot({

     # generate bins based on input$bins from ui.R
    bins <- seq(min(x()), max(x()), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x(), breaks = bins, col = 'blue', border = 'white',freq = FALSE,main = "Histograma",
         xlab = "Datos Generados")
    lines(density(x()),lwd = 3, col = "red")
  })

})

I am not 100% sure that this is what you are looking for as your code is pretty hard to read in the current format but I think this is what you are looking for. Let me know if I misinterpreted

1 Like