0

Hello i am trying to reproduce an example of shiny apps of RStudio in this link: https://shiny.rstudio.com/articles/tabsets.html The shinyapp gives error with the d() reactive expression, and i try to change the expression but gives me error: The code is:

library(shiny)

Define UI for random distribution app ----

ui <- fluidPage(

# App title ----
titlePanel("Tabsets"),

# Sidebar layout with input and output definitions ----
sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
        
        # Input: Select the random distribution type ----
        radioButtons("dist", "Distribution type:",
                     c("Normal" = "norm",
                       "Uniform" = "unif",
                       "Log-normal" = "lnorm",
                       "Exponential" = "exp")),
        
        # br() element to introduce extra vertical spacing ----
        br(),
        
        # Input: Slider for the number of observations to generate ----
        sliderInput("n",
                    "Number of observations:",
                    value = 500,
                    min = 1,
                    max = 1000)
        
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
        
        # Output: Tabset w/ plot, summary, and table ----
        tabsetPanel(type = "tabs",
                    tabPanel("Plot", plotOutput("plot")),
                    tabPanel("Summary", verbatimTextOutput("summary")),
                    tabPanel("Table", tableOutput("table"))
        )
        
    )
)

)

Define server logic for random distribution app ----

server <- function(input, output) {

# Reactive expression to generate the requested distribution ----
# This is called whenever the inputs change. The output functions
# defined below then use the value computed from this expression
d <- reactive({
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)
    
    dist(input$n)
})

# Generate a plot of the data ----
# Also uses the inputs to build the plot label. Note that the
# dependencies on the inputs and the data reactive expression are
# both tracked, and all expressions are called in the sequence
# implied by the dependency graph.
output$plot <- renderPlot({
    dist <- input$dist
    n <- input$n
    
    hist(d(),
         main = paste("r", dist, "(", n, ")", sep = ""),
         col = "#75AADB", border = "white")
})

# Generate a summary of the data ----
output$summary <- renderPrint({
    summary(d())
})

# Generate an HTML table view of the data ----
output$table <- renderTable({
    d()
})

}

Create Shiny app ----

shinyApp(ui, server)

The error when i run the app is: Warning: Error in dist: invalid arguments reactive:d [C:\Users\pruebas\Documents\Shiny_examples\tabs/app.R#63]

Can someone help i am trying to learn my first apps looking in articles at RStudio, but this one is wrong, and it is supposed to be done to teach people like me. I also dearch in github but i found the same code.Thank you.

2
  • Your code works fine. Please try after restarting RStudio. Commented Dec 9, 2020 at 3:09
  • Thank you YBS and Ash. I have restarted RStudio and the apps goes fine.!! Commented Dec 9, 2020 at 11:24

1 Answer 1

0

Your app works fine.

Are you saving it as two separate files, i.e, ui.r and server.r, or a single file, i.e. app.r?

Your code is set up for it being saved as two separate files, but the error looks like you might have saved it as a single file.

Try saving it as two files:

ui.r

ui <- fluidPage(
    
    # App title ----
    titlePanel("Tabsets"),
    
    # Sidebar layout with input and output definitions ----
    sidebarLayout(
        
        # Sidebar panel for inputs ----
        sidebarPanel(
            
            # Input: Select the random distribution type ----
            radioButtons("dist", "Distribution type:",
                         c("Normal" = "norm",
                           "Uniform" = "unif",
                           "Log-normal" = "lnorm",
                           "Exponential" = "exp")),
            
            # br() element to introduce extra vertical spacing ----
            br(),
            
            # Input: Slider for the number of observations to generate ----
            sliderInput("n",
                        "Number of observations:",
                        value = 500,
                        min = 1,
                        max = 1000)
            
        ),
        
        # Main panel for displaying outputs ----
        mainPanel(
            
            # Output: Tabset w/ plot, summary, and table ----
            tabsetPanel(type = "tabs",
                        tabPanel("Plot", plotOutput("plot")),
                        tabPanel("Summary", verbatimTextOutput("summary")),
                        tabPanel("Table", tableOutput("table"))
            )
            
        )
    )
    
)

server.r

server <- function(input, output) {
    
    # Reactive expression to generate the requested distribution ----
    # This is called whenever the inputs change. The output functions
    # defined below then use the value computed from this expression
    d <- reactive({
        dist <- switch(input$dist,
                       norm = rnorm,
                       unif = runif,
                       lnorm = rlnorm,
                       exp = rexp,
                       rnorm)
        
        dist(input$n)
    })
    
    # Generate a plot of the data ----
    # Also uses the inputs to build the plot label. Note that the
    # dependencies on the inputs and the data reactive expression are
    # both tracked, and all expressions are called in the sequence
    # implied by the dependency graph.
    output$plot <- renderPlot({
        dist <- input$dist
        n <- input$n
        
        hist(d(),
             main = paste("r", dist, "(", n, ")", sep = ""),
             col = "#75AADB", border = "white")
    })
    
    # Generate a summary of the data ----
    output$summary <- renderPrint({
        summary(d())
    })
    
    # Generate an HTML table view of the data ----
    output$table <- renderTable({
        d()
    })
    
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Ash. I tried to save it in 2 files not only one but gives me error:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.