0

I am trying to build an app in Shiny that (1) asks user for number of Assets in his/her portfolio. Depending on the numeric entry, (2) the user is presented with one numeric box to enter the % of portfolio owned AND the Ticker/name of the Asset. For instance, If the user enters 3 for number of assets in portfolio, he will be presented with something like this:

Asset1 ----> Enter ticker______      Enter Wight______

Asset2 ----> Enter ticker______      Enter Wight______

Asset3 ----> Enter ticker______      Enter Wight______

This is suppose to be dynamic so the greater the number of assets, the greater the input fields. Finally, in step (3), I want to save the entered information for each Asset in a table and display the table.

Here is what I have got and it is no where near what I need it to be. I am totally new to Shiny and that is half the reason for my troubles:

UI.R

 shinyUI(pageWithSidebar (

  headerPanel( "Portfolio Returns"),

  sidebarPanel(

    numericInput("assets", label = "Enter Total Assets", value="")
    
  ),
 mainPanel(

tableOutput("table"))     
 )
) 

server.R

shinyServer(
  function(input,output) {
  output$DynamicAssets <- renderUI ({
  
  Assets <- as.integer(input$assets)
  
  for(i in 1:Assets,function(i) {
  "ticker" = textInput("Ticker", label="Enter Ticker", value="Enter Ticker"),
  
  "weight" = numericInput ("AssetWeight", label="weights of Assets", value="")
  
     })
    })

  })
 })

I know the code is in complete because i have no idea what to do next. this is all that i figured out from seraching the net. Your help would be greatly appreciated.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
Malik
  • 71
  • 1
  • 9
  • I think this might be what you're looking for - make sure to read the comments: http://stackoverflow.com/questions/19130455/create-dynamic-number-of-input-elements-with-r-shiny?rq=1 – treysp Mar 27 '15 at 17:58
  • No. I saw that post before i posted. Tried working with what was in there but couldn't get far. My problem is I am trying to enter data (rather than select) and save it. – Malik Mar 27 '15 at 18:16

1 Answers1

3

ui.R

library(shiny)

shinyUI(pageWithSidebar (

  headerPanel( "Portfolio Returns"),

  sidebarPanel(
    numericInput("assets", label = "Enter Number of Assets in Portfolio", value="1"),
    uiOutput("tickers")
  ),
  mainPanel()
)) 

server.R
-Note that to pass multiple items in the renderUI() function you have to group them into a list, and here lapply() is creating a list of lists.

library(shiny)

shinyServer( function(input, output, session) {

  output$tickers <- renderUI({
    numAssets <- as.integer(input$assets)

    lapply(1:numAssets, function(i) {
      list(tags$p(tags$u(h4(paste0("Asset ", i)))),
           textInput(paste0("ticker", i), label = "Ticker Name", value = "Enter ticker..."),
           numericInput(paste0("weight", i), label = "Weight of Asset", value=0))  
    })
  })
})  
treysp
  • 663
  • 6
  • 17
  • This is perfect. Exactly what I wanted. Thanks. But I do have a follow up question - what does tag$p/tag$u do? – Malik Mar 27 '15 at 21:15
  • It allows you to use html to alter various aspects of the app's style, but instead of passing standard html code you can use Shiny "tags." http://shiny.rstudio.com/articles/tag-glossary.html – treysp Mar 27 '15 at 21:33
  • Also, it is customary to click the up arrow and check mark at the top left of an answer to indicate to future users that it answered your question or was most helpful in answering your question. – treysp Mar 27 '15 at 21:37
  • 1
    Would you know how I would save the entered data into a table or matrix and display it? – Malik Mar 30 '15 at 15:40