Reputation: 1493
I am new to shiny (and any web app stuff), but fairly well versed in R. I am trying to build a fairly basic page, which runs an API call before loading the page, takes some input based on the response, and then runs another API call and does some analysis. I am having trouble with the inputs.
Here is my UI:
shinyUI(fluidPage(
# Application title
titlePanel("IGP Risk Analysis"),
sidebarLayout(
sidebarPanel(
selectInput("portfolio", "Underlying Portfolio:",
choices = portfolioList),
selectInput("portDate", "Portfolio Date:",
choices = "Pick a portfolio..."),
width = 2),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)
)
)
))
My server code is below:
shinyServer(function(input, output, session) {
portfolioInput <- reactive({
temp <- setnames(sendRequest(theURL, myUN, myPW, action = "GetPortfolios"), "Available Portfolios")
portfolioList <- temp[!grepl("AAA|ZZZ",unlist(temp)),]
return(portfolioList)
})
observe({
portfolioDates <- setnames(sendRequest(theURL, myUN, myPW, action = "GetPortfolioDates",
portfolioName = input$portfolio, portfolioCurrency = "USD"),
"Available Dates")
updateSelectInput(session, "portDate",
choices = c("Pick One", portfolioDates),
selected = "Pick One")
})
})
It is working, without errors or warnings, but the first input box is displaying the results of sendRequest(). It is not setting the names, or subsetting the response. I.e. - in the first selectInput box I am getting:
theResponse.ArrayOfString.string
AAA - IGP\\Diver\\20151007
AAA - IGP\\Diver\\TEST
REAL
BD
Bvdh
Cap
Cas
Diver
IGP Aggregate
ZZZ - Archive
ZZZ - Archive\\AAA - IGP
Where I want:
Available Portfolios
REAL
BD
Bvdh
Cap
Cas
Diver
IGP Aggregate
This makes no sense to me, as it seems to be ignoring code.
Since the portfolioList is static, in that is only needs to be loaded once when you first load the page, I tried getting the list outside of the server function. I was thinking this would set a global variable I could then reference in the UI. This did not work. Any thoughts why that approach wouldnt work?
Does this have anything to do with the 'session' in the server function? Do I have old sessions running or something? Is 'session' the R session? Does it restart when I restart the app in RStudio?
Upvotes: 2
Views: 333
Reputation: 1493
Thanks everybody!!! I figured it out, or I figured out a solution to the problem. Much thanks to Sebastion for steering me in the right direction. (Also thanks to this post.) I only posted an answer to kind of put a bow on this. All props to Sebastion and others.
Here is the working ui:
shinyUI(fluidPage(
# Application title
titlePanel("IGP Risk Analysis"),
sidebarLayout(
sidebarPanel(
uiOutput("portfolio"),
uiOutput("portDate"),
width = 2),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Plot", plotOutput("plot")),
tabPanel("Summary", verbatimTextOutput("summary")),
tabPanel("Table", tableOutput("table"))
)
)
)
))
Here is the server:
shinyServer(function(input, output, session) {
output$portfolio <- renderUI ({
temp <- setNames(sendRequest(theURL, myUN, myPW, action = "GetPortfolios"), "Available Portfolios")
temp <- temp[sapply(temp, function (x) !grepl("AAA|ZZZ",x)),]
selectInput("portfolio", "Underlying Portfolio:", choices = c("Pick One",temp))
})
output$portDate <- renderUI ({
if (is.null(input$portfolio) || input$portfolio == "Pick One") return() else {
portfolioDates <- setNames(sendRequest(theURL, myUN, myPW, action = "GetPortfolioDates",
portfolioName = input$portfolio, portfolioCurrency = "USD"),
"Available Dates")
selectInput("portDate", "Portfolio Date",
choices = c("Pick One", portfolioDates),
selected = "Pick One")
}
})
})
Upvotes: 0
Reputation: 2570
To give you something to start with, minimal example of renderUI:
shinyApp(
ui = sidebarLayout(
sidebarPanel(
uiOutput("portfolio"),
selectInput("portDate", "Portfolio Date:",
choices = "Pick a portfolio..."),
width = 2),
mainPanel()),
server = function(input, output) {
ui1 <- reactive({
temp <- c("AAA","1","2","3","ZZZ")
temp[!grepl("AAA|ZZZ",temp)]
})
output$portfolio <- renderUI ({
selectInput("portfolio", "Underlying Portfolio:",
choices = ui1())
})
}
)
To add on my comments, you can't simply call functions or objects in ui.r, you render your objects in server.r and call those objects, marked as output$name in ui.r. I would advise you to do the shiny tutorials http://shiny.rstudio.com/tutorial/.
Upvotes: 2