Shiny with Rstan

I want to run rstan on Shiny GUI.
But I cannot success even if the following very simple Shiny code with rstan.
Someone has any idea?

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- 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)




        model <- rstan::stan_model(
            model_code = "parameters {real y;} model {y ~ normal(0,1);}            "
        )
        
        fit <- rstan::sampling(model)
        
        
        
        # draw the histogram with the specified number of bins
        # hist(x, breaks = bins, col = 'darkgray', border = 'white')
        rstan::stan_hist(fit)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

When rstan file foo.stan is compiled then foo.rds is created, so, I should upload together ? or it is meaningless?

My credit card is declined,.. I am not sure why but maybe my credit card is domestic only. so I cannot upgrade my plan for more than 1Gb.

Hi,

I'm unfamiliar with Stan, but running your code (without Shiny) gives me the following errors/problems:

  • You want your graph to be dependent on an input slider I presume, but nowhere in your Stan code do you use the variable x or bins, so the model is not using any user-data at this point.
  • Creating the stan_model with your provided (easy) function takes a long time, is this normal? You can add the argument verbose = T to stan_model to see its intermediate steps
  • Finally, when I try and run the sampling method, I get an error:
> fit <- rstan::sampling(model)
Error in cpp_object_initializer(.self, .refClassDef, ...) : 
  could not find function "cpp_object_initializer"
failed to create the sampler; sampling not done

Try and create a piece of normal R code that works first, then we'll worry about the Shiny :slight_smile:
You can find help for creating an example here:

Good luck,
PJ

I forget to attach library, so and I also use a bins for histogram.

Someone tells me that the amazon web server is required for free web application.
I am not sure but, someone told me that Stan compiling needs more RAM than R studio providing ram.






library(shiny);library(Rcpp);library(rstan);

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  
  
  
  
  fit <- shiny::reactive({
    
    model <- rstan::stan_model(
      model_code = "parameters {real y;} model {y ~ normal(0,1);}            "
    )
    
    fit <- rstan::sampling(model)
    
    return(fit)
    
  })
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  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 = 'darkgray', border = 'white')
    rstan::stan_hist(fit(),bins=input$bins)
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.