Shiny building 2 graphs one below the other

浪尽此生 提交于 2021-01-28 06:01:00

问题


I have downloaded an example of using shiny and I want to add a simple technical indicator to it.

My problem is that I cannot really see the second graph. Any help suggestion? I have read this: Plotting the same output in two tabPanels in shiny and R Shiny: How to assign the same plot to two different plotOutput I DO exactly the same or at least I think. So I need a small help on that, please

library(shiny)
ui <- shinyUI(fluidPage(
  titlePanel("Simple Stock Charting App"),

      sidebarLayout(
        sidebarPanel(

          textInput("symb", label = h3("Input a Valid Stock Ticker"), value = "GE") 
        ),

           ### uncomment for dygraphs chart
        mainPanel(dygraphOutput("plot")),
        mainPanel(plotOutput("plot2"))
      )
    ))

    library(quantmod)
    library(dygraphs)
    library(TTR)
    server <- shinyServer(function(input, output) {

      dataInput <- reactive({

        prices <- getSymbols(input$symb, auto.assign = FALSE)

      })

      output$plot <- renderDygraph({renderPlot

        prices <- dataInput()

        dygraph(Ad(prices)) %>%
          dyRangeSelector() 
        })

        output$plot2 <- renderPlot({

          prices <- dataInput()
          prices <- Ad(prices)
          plotOutput((RSI(prices, n = 14))) %>%  dyRangeSelector()

           })
    })


    shinyApp(ui,server)

回答1:


This should do the job

library(shiny)
library(quantmod)
library(dygraphs)
library(TTR)
ui <- shinyUI(fluidPage(
  titlePanel("Simple Stock Charting App"),

  sidebarLayout(
    sidebarPanel(
      textInput("symb", label = h3("Input a Valid Stock Ticker"), value = "GE") 
    ),

    ### uncomment for dygraphs chart
    mainPanel(dygraphOutput("plot"),plotOutput("plot2"))
  )
))


server <- shinyServer(function(input, output) {

  dataInput <- reactive({
    prices <- getSymbols(input$symb, auto.assign = FALSE)
  })

  output$plot <- renderDygraph({renderPlot
    dygraph(Ad(dataInput())) %>%dyRangeSelector() 
  })

  output$plot2 <- renderPlot({
    plot((RSI(Ad(dataInput()), n = 14))) 
  })
})


shinyApp(ui,server)




回答2:


Assuming that the first is dygraph and the second one a normal one

library(shiny) 
library(quantmod)
library(dygraphs)
library(TTR)

ui <- shinyUI(fluidPage( titlePanel("Simple Stock Charting App"),

                                        sidebarLayout(
                                          sidebarPanel(

                                            textInput("symb", label = h3("Input a Valid Stock Ticker"), value = "GE") 
                                          ),


                                          mainPanel(fluidRow(dygraphOutput("plot"),
                                                     plotOutput("plot2")))
                                        )
))


server <- shinyServer(function(input, output) {

  dataInput <- reactive({

    prices <- getSymbols(input$symb, auto.assign = FALSE)

  })

  output$plot <- renderDygraph({renderPlot

    prices <- dataInput()

    dygraph(Ad(prices)) %>%
      dyRangeSelector() 
  })

  output$plot2 <- renderPlot({

    prices <- dataInput()
    #prices <- Ad(prices)
    plot(prices)

  })
})


shinyApp(ui,server)

-output



来源:https://stackoverflow.com/questions/46344792/shiny-building-2-graphs-one-below-the-other

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!