Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R shinydashboard在渲染新输出元素时自动滚动到底部_R_Shiny_Shinydashboard_Autoscroll - Fatal编程技术网

R shinydashboard在渲染新输出元素时自动滚动到底部

R shinydashboard在渲染新输出元素时自动滚动到底部,r,shiny,shinydashboard,autoscroll,R,Shiny,Shinydashboard,Autoscroll,考虑下面的代码。它生成了两个菜单项的闪亮仪表板,每次用户选中任一选项卡上的框时,都会显示一个直方图。但是,在某个点之后(在每个选项卡上的第二个直方图渲染之后),每次渲染新元素直方图时,用户都必须手动向下滚动 我要寻找的是一种解决方案,每当一个新元素在一个闪亮的仪表板的选项卡上向下呈现时,该选项卡会立即向下滚动到底部,这样新元素就会被完全看到,并且它会应用于仪表板的所有选项卡。此示例使用图(直方图),但它可以是任何类型的输出 library(shiny) library(shinydashboar

考虑下面的代码。它生成了两个菜单项的闪亮仪表板,每次用户选中任一选项卡上的框时,都会显示一个直方图。但是,在某个点之后(在每个选项卡上的第二个直方图渲染之后),每次渲染新元素直方图时,用户都必须手动向下滚动

我要寻找的是一种解决方案,每当一个新元素在一个闪亮的仪表板的选项卡上向下呈现时,该选项卡会立即向下滚动到底部,这样新元素就会被完全看到,并且它会应用于仪表板的所有选项卡。此示例使用图(直方图),但它可以是任何类型的输出

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "My dashboard"),
  
  dashboardSidebar(
    menuItem(text = "Tab 1", tabName = "tab1"),
    menuItem(text = "Tab 2", tabName = "tab2")
    
  ),
  
  dashboardBody(
    tabItems(
      tabItem(tabName = "tab1",
              h1(textOutput(outputId = "header1")),
              checkboxInput(inputId = "sepalLng", label = "Histogram - iris, Sepal.Length"),
              conditionalPanel(condition = "input.sepalLng",
                               plotOutput(outputId = "histSepalLng", height = "500px")
              ),
              checkboxInput(inputId = "sepalWdt", label = "Histogram - iris, Sepal.Width"),
              conditionalPanel(condition = "input.sepalWdt",
                               plotOutput(outputId = "histSepalWdt", height = "500px")
              ),
              checkboxInput(inputId = "petalLng", label = "Histogram - iris, Petal.Length"),
              conditionalPanel(condition = "input.petalLng",
                               plotOutput(outputId = "histPetalLng", height = "500px")
              ),
              checkboxInput(inputId = "petalWdt", label = "Histogram - iris, Petal.Width"),
              conditionalPanel(condition = "input.petalWdt",
                               plotOutput(outputId = "histPetalWdt", height = "500px")
              )
      ),
      tabItem(tabName = "tab2",
              h1(textOutput(outputId = "header2")),
              checkboxInput(inputId = "mpg", label = "Histogram - mtcars, mpg"),
              conditionalPanel(condition = "input.mpg",
                               plotOutput(outputId = "histMpg", height = "500px")
              ),
              checkboxInput(inputId = "drat", label = "Histogram - mtcars, drat"),
              conditionalPanel(condition = "input.drat",
                               plotOutput(outputId = "histDrat", height = "500px")
              ),
              checkboxInput(inputId = "wt", label = "Histogram - mtcars, wt"),
              conditionalPanel(condition = "input.wt",
                               plotOutput(outputId = "histWt", height = "500px")
              ),
              checkboxInput(inputId = "qsec", label = "Histogram - mtcars, qsec"),
              conditionalPanel(condition = "input.qsec",
                               plotOutput(outputId = "histQsec", height = "500px")
              )
      )
    )
  )
)

server <- function(input, output) {
  
  output$header1 <- renderText({"Tab 1 - iris data"})
  output$histSepalLng <- renderPlot(hist(iris$Sepal.Length))
  output$histSepalWdt <- renderPlot(hist(iris$Sepal.Width))
  output$histPetalLng <- renderPlot(hist(iris$Petal.Length))
  output$histPetalWdt <- renderPlot(hist(iris$Petal.Width))
  
  output$header2 <- renderText({"Tab 2 - mtcars data"})
  output$histMpg <- renderPlot(hist(mtcars$mpg))
  output$histDrat <- renderPlot(hist(mtcars$drat))
  output$histWt <- renderPlot(hist(mtcars$wt))
  output$histQsec <- renderPlot(hist(mtcars$qsec))
}

shinyApp(ui, server)
库(闪亮)
图书馆(shinydashboard)

ui下面的代码就是您想要的。但首先,您需要注意一些小细节:

  • 您需要为
    menuItem
    添加
    sidebarMenu()
    包装,否则,这是难看的要点,当您单击了tab1和tab2时,您无法单击tab1返回到tab1。我不知道你使用的是什么版本的仪表板,至少在我这边是这样
  • #只需将其添加到仪表板主体中即可
    仪表板主体(
    标记$script(
    “$(文档).ready(函数(){
    $(\”.tab内容[type='checkbox']\”)。在('click',function()上{
    setTimeout(函数(){
    scrollTo(0,document.body.scrollHeight);
    }, 200)
    })
    })"
    ),
    ...
    )
    
    像这样:


    ui下面的代码就是您想要的。但首先,您需要注意一些小细节:

  • 您需要为
    menuItem
    添加
    sidebarMenu()
    包装,否则,这是难看的要点,当您单击了tab1和tab2时,您无法单击tab1返回到tab1。我不知道你使用的是什么版本的仪表板,至少在我这边是这样
  • #只需将其添加到仪表板主体中即可
    仪表板主体(
    标记$script(
    “$(文档).ready(函数(){
    $(\”.tab内容[type='checkbox']\”)。在('click',function()上{
    setTimeout(函数(){
    scrollTo(0,document.body.scrollHeight);
    }, 200)
    })
    })"
    ),
    ...
    )
    
    像这样:


    ui非常感谢您花时间和精力提供这个全面的答案,它也帮助了我的学习之旅。我没有使用
    sidebarMenu
    ,只是想提供一个最小的可复制示例,而不是一个漂亮的示例。我是Shiny的新手,对JS或
    Jquery
    没有专业知识。因此,如果我理解正确,自动向下滚动总是需要绑定到对象的类型(本例中为复选框),从而导致呈现新元素的事件(本例中为绘图)。也就是说,不可能有一些通用的解决方案适用于所有仪表板选项卡,而不管其内容如何?可以有一个JS
    监听器来监视选项卡内容。将文档高度存储在JS变量中,当侦听器发现高度变化时,将其与存储的变量进行比较。如果高度较大(渲染新内容),则滚动到底部,如果较小(删除内容),则不执行任何操作。所以这是可能的,但你需要学习JS。非常感谢你花时间和精力提供这个全面的答案,它也帮助了我的学习之旅。我没有使用
    sidebarMenu
    ,只是想提供一个最小的可复制示例,而不是一个漂亮的示例。我是Shiny的新手,对JS或
    Jquery
    没有专业知识。因此,如果我理解正确,自动向下滚动总是需要绑定到对象的类型(本例中为复选框),从而导致呈现新元素的事件(本例中为绘图)。也就是说,不可能有一些通用的解决方案适用于所有仪表板选项卡,而不管其内容如何?可以有一个JS
    监听器来监视选项卡内容。将文档高度存储在JS变量中,当侦听器发现高度变化时,将其与存储的变量进行比较。如果高度较大(渲染新内容),则滚动到底部,如果较小(删除内容),则不执行任何操作。所以这是可能的,但你需要学习JS。