0

I am trying to make an app that allow user to input data and then let shiny server to calculate some results, for example, I could make shiny generate a plot or a data table.

However, due to the space of the UI, I kind of "run out of" space. The input box, and documentation of the app take a whole screen. And when the shiny generate the results it will show at the very bottom of the screen.

Is there a way that I can make shiny pop-up a message box to show the result?

My sudo-code would be:

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")
    })}

When clicking the "demo", a message box popup or I can make the screen move to the result part and allow the text to be at the top of the screen.

Bill Chen
  • 1,699
  • 14
  • 24

1 Answers1

0

There are options to show your results in a separated window. But maybe will be easier to have everything on the same window.

You can use the shinyBS library to create a modal window to show the plot. Another option is to use JavaScript to move the scroll to the bottom of the page. I put the two options in the following example, so you can see which one is better for you.

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)
    })

  })
))

If you think that the JavaScript option works better for you, you could consider start using the shinyjs library, it includes very useful functions and you can easily add your own JavaScript code to your Shiny Apps.

Geovany
  • 5,389
  • 21
  • 37