1

Fowllowing the description of dynamic shiny app at topic [R Shiny Dynamic Input , i want to get a data into shiny app. I wrote in ui.R

library(fPortfolio)
library(quantmod)
library(shiny)


ui <- shinyUI(fluidPage(
  titlePanel("Portfolio optimization"),

  sidebarLayout(
    sidebarPanel(
      numericInput("numInputs", "A number of stocks", 2),
      # place to hold dynamic inputs
      uiOutput("inputGroup")
    ),


mainPanel(
  tabPanel("Trading Statistics",
           fixedRow(
             column(8,
                    fixedRow(column(4,tableOutput("tablePerformance")),
                             column(4,tableOutput("tableRisk"))),
                    fixedRow(column(4,tableOutput("tableDaily")),
                             column(4,tableOutput("tableMonthly"))))
             ))
  )
  )
))

and in server.r

library(fPortfolio)
library(quantmod)
library(shiny)

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

  observeEvent(input$numInputs, {
    output$inputGroup = renderUI({
      input_list <- lapply(1:input$numInputs, function(i) {
        # for each dynamically generated input, give a different name
        inputName <- paste("input", i, sep = "")
        textInput(inputName, inputName, value = 1)
      })
      do.call(tagList, input_list)
    })
  })


  data <- read.csv("E:/stock vn data/dulieuvietnam/metastock_all_data.txt", header = TRUE, stringsAsFactors = FALSE)
  Tickers <- data[!duplicated(data$X.Ticker.),1]
  Tickers <- subset(Tickers,substr(Tickers,1,1)!= "^")
  PriceList <- list()
  for (i in 1:length(Tickers)){
    PriceList[[i]] <- subset(data[,c(2,6)],data$X.Ticker. == Tickers[i])
    names(PriceList[[i]]) <- c("Date",Tickers[i])
    PriceList[[i]][PriceList[[i]]==0]<-NA
    PriceList[[i]] <- na.locf(PriceList[[i]])
  }
  PriceList[[(length(Tickers)+1)]]<-subset(data[,c(2,6)],data$X.Ticker. == "^VNINDEX")
  names(PriceList[[(length(Tickers)+1)]]) <- c("Date","VNINDEX")
  PriceList[[(length(Tickers)+1)]][PriceList[[(length(Tickers)+1)]]==0]<-NA
  PriceList[[(length(Tickers)+1)]] <- na.locf(PriceList[[(length(Tickers)+1)]])

  dataPrice <- PriceList[[1]]
  for (k in 2:length(PriceList)){
    dataPrice <-merge(dataPrice,PriceList[[k]],all=TRUE)
  }

    output$tablePerformance<-renderTable({

  })
})

. When i run runApp(), the app only shows input with label "A number of stocks" that has default value is 2. However, interface of app did not show two text input. Please help me!

6
  • Try to run all code below read.csv (including that line) within an observe. Apr 6, 2016 at 19:18
  • I have just tried as your guess, but the app did not show two text input. It only shows input with label "A number of stocks" that has default value is 2. Apr 7, 2016 at 1:51
  • If you comment off everything below (including) read.csv line, does it show two inputs? I've tested here and it does. Apr 7, 2016 at 1:53
  • If I comment off everything below (including) read.csv line, does it show two inputs, it show two inputs. However, I want to include data in shiny app. How can I do that? Apr 7, 2016 at 2:02
  • Must be something wrong with that section of code, because I used read.csv on a test file and render it as table and I still get two inputs. Apr 7, 2016 at 2:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.