如何在闪亮的交互式功能中输入控制台等效命令?

时间:2017-02-14 20:14:32

标签: r shiny

好的,所以我确信我的问题措辞不对,但有没有办法完成相当于在Shiny中输入1进入控制台。

基本上我希望以下内容适用于Shiny:

library(quantmod)
library(fPortfolio)


# Get data

data1 <- getSymbols(c("VEU","SPY","VWO","AGG"),
                from = "2016-01-01",
                to = "2017-01-01")
data2 <- do.call(merge, lapply(data1, function(x) Ad(get(x))))
data3 <- na.omit(ROC(data2,n = 1,"discrete"))
colnames(data3) <- gsub(".Adjusted","",colnames(data3))
data4 <- as.timeSeries(data3)

# fPortfolio options

portfolioOptions<-portfolioFrontier(data4)
plot(portfolioOptions)

在此之后,我们在控制台中获得以下内容:

Make a plot selection (or 0 to exit): 

1:   Plot Efficient Frontier
2:   Add Minimum Risk Portfolio
3:   Add Tangency Portfolio
4:   Add Risk/Return of Single Assets
5:   Add Equal Weights Portfolio
6:   Add Two Asset Frontiers [LongOnly Only]
7:   Add Monte Carlo Portfolios
8:   Add Sharpe Ratio [Markowitz PF Only]

Selection: 

有没有办法在Shiny上的选择中输入1-8并相应地渲染图?

1 个答案:

答案 0 :(得分:0)

您可以使用which参数指定每次绘制的绘图:

library(shiny)
shinyApp(ui=fluidPage(plotOutput("plot"), 
                      textInput("num", "Enter choice", "1")),
         server=function(input, output, session){
           plotlist <- reactiveValues()
           observeEvent(input$num, {
             if(!is.null(input$num)){
               if(input$num=="1")
                 plotlist$l <- 1
               else
                 plotlist$l <- c(plotlist$l, as.numeric(input$num))
             }})
           output$plot <- renderPlot(plot(portfolioOptions, which=plotlist$l))
         })