r - 带有 Shiny 的多页 intro.js

标签 r shiny intro.js

我正在尝试在 Shiny 应用程序上实现 into.js 多页功能。

下面的代码是一种不起作用的尝试。第一个选项卡运行良好,显示第二页的弹出窗口但不切换选项卡。

用户界面

library(shiny)


    shinyUI(tagList(
            tags$head(
                    HTML("<link rel='stylesheet' type='text/css' href='css/introjs.min.css'>")
            ),
            navbarPage("Old Faithful Geyser Data",




      tabPanel(id = "fTab", "First tab",
            HTML("<h1 data-step='1' data-intro='This is a tooltip!'>Basic Usage</h1>"),
           sliderInput("bins",
                       "Number of bins:",
                       min = 1,
                       max = 50,
                       value = 30),

           plotOutput("distPlot"),
           HTML("<a id='startButton' class='btn btn-large btn-success' href='javascript:void(0);'>Help</a>")

      ),
      tabPanel(tabName = "sTab", "Second tab", id = "tt", 
               HTML("<h1 data-step='2' data-intro='This is a second tooltip!'>Basic Usage</h1>"),
                  sliderInput("bins2",
                              "Number of bins:",
                              min = 1,
                              max = 50,
                              value = 30),

                  plotOutput("distPlot2")
                  )
    ),
    HTML("<script type='text/javascript' src='js/intro.min.js'></script>"),
    HTML("<script type='text/javascript'>document.getElementById('startButton').onclick = function() {
         introJs().setOption('doneLabel', 'Next page').start().oncomplete(function() {
         window.location.hash = '#!tt?multipage=true';
         });
         };</script>")  
    ))

服务器
library(shiny)


    shinyServer(function(input, output) {

      output$distPlot <- renderPlot({

        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = input$bins + 1)


        hist(x, breaks = bins, col = 'darkgray', border = 'white')

      })
      output$distPlot2 <- renderPlot({


              x    <- faithful[, 2] 
              bins <- seq(min(x), max(x), length.out = input$bins2 + 1)


              hist(x, breaks = bins, col = 'darkgray', border = 'white')

      })

    })

intro.js 中的 js 和 css 文件位于 www 文件夹内的 js 和 css 文件夹中。可以找到 intro.js 文件 here

我的猜测是我在 ui.R 底部的 javascript 代码中的函数做错了。我试图改编 here 中的例子通过用 window.location.hash 替换 window.location.href 并引用“tt”选项卡 ID。

最佳答案

@warmoverflow 给出了一个很好的答案。这是使用 rintrojs 的相同答案的版本:

library(shiny)
library(rintrojs)

ui = shinyUI(tagList(
  introjsUI(),
  navbarPage(
    "Old Faithful Geyser Data",

    tabPanel(
      id = "fTab",
      "First tab",
      introBox(
        h1("Basic Usage"),
        data.step = 1,
        data.intro = "This is a tooltip"
      ),
      sliderInput(
        "bins",
        "Number of bins:",
        min = 1,
        max = 50,
        value = 30
      ),

      plotOutput("distPlot"),
      actionButton("startButton", "Help")
    ),
    tabPanel(
      tabName = "sTab",
      "Second tab",
      id = "tt",
      introBox(
        h1("Basic Usage 2"),
        data.step = 2,
        data.intro = "This is a second tooltip"
      ),
      sliderInput(
        "bins2",
        "Number of bins:",
        min = 1,
        max = 50,
        value = 30
      ),

      plotOutput("distPlot2")
    )
  )
))

server = shinyServer(function(input, output, session) {
  output$distPlot <- renderPlot({
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)


    hist(x,
         breaks = bins,
         col = 'darkgray',
         border = 'white')

  })
  output$distPlot2 <- renderPlot({
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins2 + 1)


    hist(x,
         breaks = bins,
         col = 'darkgray',
         border = 'white')

  })

  observeEvent(input$startButton, {
    introjs(
      session,
      events = list(
        "onchange" = "if (this._currentStep==0) {
        $('a[data-value=\"Second tab\"]').removeClass('active');
        $('a[data-value=\"First tab\"]').addClass('active');
        $('a[data-value=\"First tab\"]').trigger('click');
  }
        if (this._currentStep==1) {
        $('a[data-value=\"First tab\"]').removeClass('active');
        $('a[data-value=\"Second tab\"]').addClass('active');
        $('a[data-value=\"Second tab\"]').trigger('click');
        }"
)
      )

})

  })

shinyApp(ui = ui, server = server)

但是,如果您返回到导览的第一步,选项卡不会切换,因此代码只能在导览中向前运行。

注意:在 rintrojs版本 0.1.2.900 及更高版本,原始 javascript 需要包含在 I()

关于r - 带有 Shiny 的多页 intro.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39545002/

相关文章:

r - 如何退出 Shiny 的应用程序并返回一个值

r - 如何在 Shiny 的 navbarPage() 上将图像插入导航栏

javascript - 跳过或完成后的 IntroJS 回调函数?

javascript - IntroJS 添加指令到步骤标记

intro.js - 在 intro.js 中使用单击功能进行单步执行

R 重新编码变量 - 意外的 INCOMPLETE_STRING

R:根据两行中的连续值,填充第三行

r - 使用 data.table 加速 rollapply

r - 如何检测条件连续发生 3 次且具有优先级的情况?

css - Shiny :标签位置,文本输入