This question builds on this previous question R Shiny: keep old output.
I would like to view the output at the top of the page. How to automatically scroll the output to the top of the page?
library(shiny)
library(broom)
library(dplyr)
library(shinyjs)
library(shinydashboard)
header <- dashboardHeader(title = "My Dashboard")
sidebar <- dashboardSidebar(
sidebarMenu(
checkboxGroupInput(inputId = "indep",
label = "Independent Variables",
choices = names(mtcars)[-1],
selected = NULL),
actionButton(inputId = "fit_model",
label = "Fit Model"),
numericInput(inputId = "model_to_show",
label = "Show N most recent models",
value = 20)
)
)
body <- dashboardBody(
includeScript("www/scrolldown.js"),
tags$head(includeCSS('www/style.css')),
htmlOutput("model_record")
)
ui <- dashboardPage(header, sidebar, body)
server <-
shinyServer(function(input, output, session){
Model <- reactiveValues(
Record = list()
)
observeEvent(
input[["fit_model"]],
{
fit <-
lm(mpg ~ .,
data = mtcars[c("mpg", input[["indep"]])])
#Model$Record <- c(Model$Record, list(fit))
#Last result up
Model$Record <- c(list(fit),Model$Record)
}
)
output$model_record <-
renderText({
tail(Model$Record, input[["model_to_show"]]) %>%
lapply(tidy) %>%
lapply(knitr::kable,
format = "html") %>%
lapply(as.character) %>%
unlist() %>%
paste0(collapse = "<br/><br/>")
})
})
shinyApp(ui, server)
style.css file:
.sidebar {
color: #FFF;
position: fixed;
width: 220px;
white-space: nowrap;
overflow: visible;
}
.main-header {
position: fixed;
width:100%;
}
.content {
padding-top: 60px;
}
EDIT: Javascript added based on Waldi's answer:
scrolldown.js
$(document).on('shiny:value', function(event) {
// Scroll down after model update
if (event.target.id === 'model_record') {
window.scrollTo(0,document.body.scrollHeight);
}
});