呈现新的输出元素时,R Shinydashboard自动滚动到底部

如何解决呈现新的输出元素时,R Shinydashboard自动滚动到底部

请考虑以下代码。它产生带有两个菜单项的Shiny仪表板,每当用户在任一选项卡上选中一个框时,都会显示一个直方图。但是,在某个点之后(在每个选项卡上显示第二个直方图之后),每次绘制新的元素直方图时,用户都必须手动向下滚动。

我正在寻找的解决方案是,每次在Shiny仪表板的选项卡上将新元素向下进一步渲染时,该选项卡都会立即向下滚动到底部,因此可以完全看到新元素,并且适用于仪表板的所有选项卡。该示例带有曲线图(直方图),但可以是任何类型的输出。

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",Sepal.Width"),conditionalPanel(condition = "input.sepalWdt",plotOutput(outputId = "histSepalWdt",checkboxInput(inputId = "petalLng",Petal.Length"),conditionalPanel(condition = "input.petalLng",plotOutput(outputId = "histPetalLng",checkboxInput(inputId = "petalWdt",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",checkboxInput(inputId = "drat",drat"),conditionalPanel(condition = "input.drat",plotOutput(outputId = "histDrat",checkboxInput(inputId = "wt",wt"),conditionalPanel(condition = "input.wt",plotOutput(outputId = "histWt",checkboxInput(inputId = "qsec",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)

有人可以帮忙吗?

解决方法

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

  1. 您需要为sidebarMenu()添加menuItem包装器,否则,这是难看的要点,并且当您单击tab1和tab2时,您不能单击tab1返回到tab1。我不知道您使用的是什么版本的仪表板,至少在我看来是这样。
# just add this to your dash body
    dashboardBody(
        tags$script(
            "$( document ).ready(function() {
               $(\".tab-content [type='checkbox']\").on('click',function(){
                  setTimeout(function() {
                    window.scrollTo(0,document.body.scrollHeight);
                },200)
               })
             })"
        ),...
    )

看起来像这样:

ui <- dashboardPage(
    dashboardHeader(title = "My dashboard"),dashboardSidebar(
        sidebarMenu(
            menuItem(text = "Tab 1",tabName = "tab1"),menuItem(text = "Tab 2",tabName = "tab2")
        )
    ),dashboardBody(
        tags$script(
            "$( document ).ready(function() {
               $(\".tab-content [type='checkbox']\").on('click',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",Sepal.Width"),conditionalPanel(condition = "input.sepalWdt",plotOutput(outputId = "histSepalWdt",checkboxInput(inputId = "petalLng",Petal.Length"),conditionalPanel(condition = "input.petalLng",plotOutput(outputId = "histPetalLng",checkboxInput(inputId = "petalWdt",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",checkboxInput(inputId = "drat",drat"),conditionalPanel(condition = "input.drat",plotOutput(outputId = "histDrat",checkboxInput(inputId = "wt",wt"),conditionalPanel(condition = "input.wt",plotOutput(outputId = "histWt",checkboxInput(inputId = "qsec",qsec"),conditionalPanel(condition = "input.qsec",plotOutput(outputId = "histQsec",height = "500px")
                    )
            )
        )
    )
)

我使用了一些Jquery和JS函数。如果您更喜欢R方式,则可以使用shinyjs包运行JS。比我想的还要棘手。

基本上,我要做的是,只要单击选项卡内容下的复选框(与哪个或哪个选项卡无关),它就会自动滚动窗口。但是,单击按钮后将绘制图。如果在单击时滚动,则窗口高度将保持不变,直到将图从服务器发送到前端为止。因此,我添加了200ms的延迟,您可以在脚本中看到200。如果绘图需要200毫秒以上的渲染时间,则需要将此延迟时间更改为更长的时间。如果您说要自动确定延迟时间:

  1. renderPlot function上添加JS脚本,当渲染功能触发时,您可以滚动,但是您需要为每个渲染功能执行此操作,因此会有很多重复的代码不理想。由shinyjs包完成。
  2. 我的解决方案只需要在前端脚本上工作,就没有后端通信。因此,另一种选择是添加闪亮的服务器通信以告知JS触发时间。 sendCustomMessage可以完成此操作,但需要花费很多时间来学习。我不建议您是初学者。

也许只是估计时间,大多数情节应该花不到1秒的时间渲染。

编辑:

上面的脚本同时滚动选中/取消选中该框,但我认为您只希望选中该框:

        HTML(
        "<script>
           $( document ).ready(function() {
              $(\".tab-content [type='checkbox']\").on('click',function(){
                var is_checked = this.checked;
                if(typeof is_checked === 'boolean' && is_checked === true){
                  setTimeout(function() {
                    window.scrollTo(0,document.body.scrollHeight);
                  },200)
                }
              })
           })
        </script>"
        ),

tags$script将我的&更改为“&amp;”,而必须使用HTML

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-