Shiny error when generating HTML

Asked

Viewed 119 times

3

I am trying to "create an odds calculator for the Normal and Binomial distributions, however this giving some error when generating HTML.inserir a descrição da imagem aqui.Follow the codes below.

PS: it would be possible to exchange mi and sigma for Greek letters?

library(shiny)

ui<-fluidPage(
titlePanel("Calculadora"),  
sidebarLayout(

sidebarPanel(
  selectInput(inputId="dist",
    label="Escolha sua distribuição:",
    choices=c("Binomial"="bin","Normal"="normal"),selected = "bin"),
    sliderInput(inputId="x",label="Escolha o valor de X:",
     min=0,max=50,step = 1,value = 10),
     conditionalPanel(condition = "input.dist=='bin'",
    sliderInput(inputId = "n",label = "Escolha o valor de N:",
     min=0,max=50,step=1,value=25),
    sliderInput(inputId ="p",label="Escolha o valor de P",
      min=0,max = 1,value=0.5)),
    conditionalPanel(condition = "input.dist=='normal'",
    sliderInput(inputId = "s",label = "Escolha o valor de sigma",
      min=0,max=50,step=1,value=25),
    sliderInput(inputId ="m",label="Escolha o valor de mi",
     min=0,max=50,step=1,value=25))
    ),
    mainPanel(textOutput("text_calc"))
  )
)

server <- function(input, output) {
  output$text_calc<-renderPrint({
    if (input$dist=="bin"){
      N<-input$n
      P<-input$p
      k<-input$x
      D<-pbinom(k,N,P)
      paste("Probabilidade pedida:", D)
    }
    if (input$dist=="normal"){
      S<-input$s
      M<-input$m
      k<-input$x
      D<-pnorm(k,M,S)
      paste("Probabilidade pedida:", D)
    }
  }
)

}

shinyApp(ui,server)

1 answer

3


Apparently the problem was both if chained. I took one of them and replaced it with a else. It’s all right now.

If it’s in your interest to expand this calculator to more distributions, just add more else after the second.

Greek letters can be placed through the function withMathJax(), which loads the Mathjax library, responsible for Latex-based mathematical notation. See the second line of the ui to see how I loaded the library. Then see sliderInput corresponding to sigma and mi to see how I wrote them with the corresponding Greek letter.

Research Latex to find out how to make other Greek letters and more complex mathematical expressions, such as integrals, partial derivatives and, well, anything else imaginable.

library(shiny)

ui<-fluidPage( titlePanel("Calculadora"),
           withMathJax(),
           sidebarLayout(
             sidebarPanel(
               selectInput(inputId="dist",
                           label="Escolha sua distribuição:",
                           choices=c("Binomial"="bin","Normal"="normal"),selected = "bin"),
               sliderInput(inputId="x",label="Escolha o valor de X:",
                           min=0,max=50,step = 1,value = 10),
               conditionalPanel(condition = "input.dist=='bin'",
                                sliderInput(inputId = "n",label = "Escolha o valor de N:",
                                            min=0,max=50,step=1,value=25),
                                sliderInput(inputId ="p",label="Escolha o valor de P",
                                            min=0,max = 1,value=0.5)),
               conditionalPanel(condition = "input.dist=='normal'",
                                sliderInput(inputId = "s",label = "Escolha o valor de \\(\\sigma\\)",
                                            min=0,max=50,step=1,value=25),
                                sliderInput(inputId ="m",label="Escolha o valor de \\(\\mu\\)",
                                            min=0,max=50,step=1,value=25))
             ),
             mainPanel(textOutput("text_calc"))
           ) )

server <- function(input, output) { 
  output$text_calc<-renderPrint({ 
    if (input$dist=="bin"){ 
      N<-input$n 
      P<-input$p 
      k<-input$x 
      D<-pbinom(k,N,P) 
      paste("Probabilidade pedida:", D) 
      } else { 
        S<-input$s 
        M<-input$m 
        k<-input$x 
        D<-pnorm(k,M,S) 
        paste("Probabilidade pedida:", D) } 
  }
)
}

shinyApp(ui,server)

inserir a descrição da imagem aqui

  • Thank you Marcus :)

  • It’s great to know that my response has helped you in some way. So consider vote and accept the answer, so that in the future other people who experience the same problem have a reference to solve it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.