Kevin Burnham Kevin Burnham - 25 days ago 10
R Question

fix selectInput error on initial shinyr app load

When the shiny app below is run I initially get the error -

invalid type/length (symbol/0) in vector allocation
. However, as soon as I click "Submit" the app functions as intended.

Is there a way to avoid this launch error and have it work correctly from the start?

plot_and_summary <- function(dat, col){
summary <- dat %>% summarize_(mean = interp(~mean(x), x = as.name(col)),
sd = interp(~sd(x), x = as.name(col)))
plot <- ggplot(dat, aes_string(x = col)) + geom_histogram()
return(list(summary = summary, plot = plot))
}


library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel(""),
sidebarLayout(
sidebarPanel(
uiOutput("column_select"),
submitButton("Submit")
),
mainPanel(
tableOutput("summary"),
plotOutput("plot")
)
)

)

# Define server logic required to draw a histogram
server <- function(input, output){
dat <- reactive({iris})


output$column_select <- renderUI({selectInput("col", label = "select column", choices = as.list(names(dat())))})



pas <- reactive({plot_and_summary(dat(), input$col)})
output$plot <- renderPlot({pas()$plot})
output$summary <- renderTable({pas()$summary})


}


shinyApp(ui = ui, server = server)

Answer

The req function should solve your problem

http://shiny.rstudio.com/reference/shiny/latest/req.html

pas <- reactive({plot_and_summary(dat(), req(input$col))})
Comments