【问题标题】:Q: How to make shiny app output result in a new page?问:如何使闪亮的应用程序输出结果在新页面中?
【发布时间】:2017-02-10 04:32:05
【问题描述】:

我正在尝试制作一个允许用户输入数据然后让闪亮服务器计算一些结果的应用程序,例如,我可以让闪亮生成一个绘图或数据表。

但是,由于 UI 的空间,我有点“用完”空间。输入框和应用程序的文档占据了整个屏幕。当闪亮生成结果时,它会显示在屏幕的最底部。

有没有办法让闪亮的弹出消息框显示结果?

我的 sudo 代码是:

ui <- fluidPage(
    textInput("text", "Name"),
    numericInput("age", "Age", 20),
    actionButton("demo", "Fill in fields with demo"))
server <- function(input, output, session) {
    observeEvent(input$demo, {

            ****************************
            OpenNewPage/MoveScreenDown()
            ****************************

            updateTextInput(session, "text", value = H)
            updateNumericInput(session, "age", value = "30")
    })}

当点击“demo”时,会弹出一个消息框,或者我可以让屏幕移动到结果部分并让文本位于屏幕顶部。

【问题讨论】:

    标签: shiny shinydashboard shinyjs


    【解决方案1】:

    有一些选项可以在单独的窗口中显示您的结果。但也许将所有东西都放在同一个窗口上会更容易。

    您可以使用 shinyBS 库创建一个模式窗口来显示绘图。另一种选择是使用 JavaScript 将滚动条移动到页面底部。我将这两个选项放在下面的示例中,以便您查看哪个更适合您。

    library(shiny)
    library(shinyBS)
    runApp(list(
      ui = shinyUI(fluidPage(
        textInput("text", "Name"),
        numericInput("age", "Age", 20),
        # option 1, using ShinyBS with a modal window
        actionButton("demo", "Using a modal"),
        # modal window to show the plot
        bsModal("largeModalID","Results", "demo", size = "large", plotOutput('plot')),       
        # Option 2, action button with a JavaScript function to move the scroll to the bottom
        # after drawing the plot.
        actionButton("demoJS", "Using JS", 
          # there is a delay to allow the renderPlot to draw the plot and you should
          # change it according to the processes performed
          onclick = "setTimeout( function() {
                      $('html, body').scrollTop( $(document).height() );},
                      300)"),         
        # to plot the results after click on "Using JS"
        uiOutput("plotUI")
       )
      ),
      server = shinyServer(function(input, output, session) {
    
        output$plot <- renderPlot({
          # simple plot to show
          plot(sin, -pi, 2*pi)
        })
    
        output$plotUI <- renderUI({
          # this UI will show a plot only if "Using JS" is clicked
          if (input$demoJS > 0)
            # the margin-top attribute is just to put the plot lower in the page
            div(style = "margin-top:800px", plotOutput('plot2'))
        })
    
        output$plot2 <- renderPlot({
          # another simple plot, 
          plot(sin, -pi, 2*pi)
        })
    
      })
    ))
    

    如果您认为 JavaScript 选项更适合您,您可以考虑开始使用 shinyjs 库,它包含非常有用的功能,您可以轻松地将自己的 JavaScript 代码添加到您的 Shiny 应用程序中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-09
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 2020-03-15
      • 1970-01-01
      相关资源
      最近更新 更多