0

Let's consider my very basic application :

enter image description here

Created by code :

Server

library(shiny) # Load shiny package


start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
#Apple stock 
getSymbols("AAPL", src = "yahoo", from = start, to = end)
apple <- AAPL$AAPL.Close
#Gold
getSymbols('GOLD', src = 'yahoo', from = start, to = end)
gold <- GOLD$GOLD.Close
#S&P500
getSymbols('^GSPC', src = 'yahoo', from = start, to = end)
sp <- as.numeric(`GSPC`[,4])
#Microsoft
getSymbols('MSFT', src = 'yahoo', from = start, to = end) 
msft <- MSFT$MSFT.Close

stock.frame <- data.frame(apple, gold, msft, sp)
colnames(stock.frame) <- c('apple', 'gold', 'msft', 'sp')


shinyServer(
  function(input, output) {
    output$myhist <- renderPlot({
      colm <- as.numeric(input$var)
      hist(stock.frame[, colm], col = input$colour, xlim = c(min(stock.frame[, colm]), max(stock.frame[, colm])), main = "Histogram of stock dataset", breaks = seq(min(stock.frame[, colm]), max(stock.frame[, colm]), l = input$bin + 1), xlab = names(stock.frame[colm]))
    })
  }
)

UI

library(shiny) # load the shiny package

# Define UI for application
shinyUI(fluidPage(
    
    # Header or title Panel 
    titlePanel(h4('Demostration of the renderPlot() - A Histogram with stock dataset', align = "center")),
    
    # Sidebar panel
    
        sidebarPanel(
        
        
        
        selectInput("var", label = "1. Select the quantitative Variable", 
                    choices = c("Apple" = 1, "Gold" = 2, "S&P" = 3, "BTC"=4),
                    selected = 3), 
        
        
        sliderInput("bin", "2. Select the number of histogram BINs by using the slider below", min=5, max=50, value=15),
        
        radioButtons("colour", label = "3. Select the color of histogram",
                     choices = c("Green", "Red",
                                 "Yellow"), selected = "Green")
    ),
    
    # Main Panel
    mainPanel(
        textOutput("text1"),
        textOutput("text2"),
        textOutput("text3"),
        plotOutput("myhist")
        
    )
    
)
)

I want to have another sidebarPanel (analogous to '1. Select the quantitative Variable') in which I can specify if I want 'Histogram' or 'nothing'. If histogram was choosed then I should have same thing as above. When "nothing' was choosed I should see blank page. Do you know how it can be performed ?

EDIT

I added radiobutton as @r2evans suggested. It now look's in the way following :

shinyUI(fluidPage(
    
    radioButtons("rb", "Plot type:", choiceNames = c("Histogram", "Nothing")),
    # Header or title Panel 
    titlePanel(h4('Demostration of the renderPlot() - A Histogram with stock dataset', align = "center")),
    
    # Sidebar panel
    
        sidebarPanel(
        
        
        
        selectInput("var", label = "1. Select the quantitative Variable", 
                    choices = c("Apple" = 1, "Gold" = 2, "S&P" = 3, "BTC"=4),
                    selected = 3), 
        
        
        sliderInput("bin", "2. Select the number of histogram BINs by using the slider below", min=5, max=50, value=15),
        
        radioButtons("colour", label = "3. Select the color of histogram",
                     choices = c("Green", "Red",
                                 "Yellow"), selected = "Green")
    ),
    
    # Main Panel
    mainPanel(
        textOutput("text1"),
        textOutput("text2"),
        textOutput("text3"),
        plotOutput("myhist")
        
    )
    
)
)

However after running 'Run App' i see error :

Error in normalizeChoicesArgs: Please specify a non-empty vector for `choices` (or, alternatively, for both `choiceNames` AND `choiceValues`).
  81: stop
  80: normalizeChoicesArgs
  79: radioButtons

Have I done something wrong ?

John
  • 1,849
  • 2
  • 13
  • 23
  • `radioButtons("rb", "Plot type:", choiceNames = c("Histogram", "Nothing")`. Within `output$myhist`, start with `req(input$rb == "Histogram")`. – r2evans Dec 21 '20 at 13:57
  • Could you please describe me more strictly where should I put this radioButtons ? – John Dec 21 '20 at 14:05
  • Is your question *"how to conditionally not plot a histogram"*, or is it *"how to add a second side panel"*? They are completely different, and I'm suggesting a way to address the first. – r2evans Dec 21 '20 at 14:22
  • My question is - How to add second side panel - at the end I want to have at second panel something different than histogram (e.g. summary of data). However I didn't put this in my question for bigger simplicity. – John Dec 21 '20 at 14:24
  • I don't know an easy way to have two side bars, though https://stackoverflow.com/q/37777302/3358272 is related. Since all you're talking about adding is a single radio button (or checkbox or whatever ... a single HTML object), I wouldn't think a second sidebar panel would be much more than a significant space-hog. – r2evans Dec 21 '20 at 14:49
  • 1
    John - the `radioButtons` recommended by @r2evans should work just fine...I'm wondering if there is a miscommunication here...when you say second side panel, I'm thinking you just want another input (like radiobuttons) to select "Histogram" or "nothing" - is that right? It sounds like you *don't* want another use of an additional and separate `sidebarPanel()` in your `ui`. If that's correct, perhaps one of us can provide a complete example to demonstrate. If I'm off, please let me know. – Ben Dec 21 '20 at 14:55

1 Answers1

1

Perhaps you are looking for a solution like this

library(shiny) 
library(quantmod)

start <- as.Date("2013-01-01")
end <- as.Date("2016-10-01")
#Apple stock 
getSymbols("AAPL", src = "yahoo", from = start, to = end)
apple <- AAPL$AAPL.Close
#Gold
getSymbols('GOLD', src = 'yahoo', from = start, to = end)
gold <- GOLD$GOLD.Close
#S&P500 
getSymbols('^GSPC', src = 'yahoo', from = start, to = end)
sp <- as.numeric(`GSPC`[,4])
#Microsoft
getSymbols('MSFT', src = 'yahoo', from = start, to = end) 
msft <- MSFT$MSFT.Close

stock.frame <- data.frame(apple, gold, msft, sp)
colnames(stock.frame) <- c('apple', 'gold', 'msft', 'sp')
cmat <- cor(stock.frame)
### plot_ly(z = cmat, type = "heatmap")

### Define UI for application
ui <- fluidPage(
  
  # Header or title Panel 
  titlePanel(h4('Demostration of the renderPlot() - A Histogram with stock dataset', align = "center")),
  
  # Sidebar panel
  sidebarPanel(
    selectInput("var", label = "1. Select the quantitative Variable", 
                choices = c("Apple" = 1, "Gold" = 2, "S&P" = 3, "BTC"=4),
                selected = 3), 
    sliderInput("bin", "2. Select the number of histogram BINs by using the slider below", min=5, max=50, value=15),
    radioButtons("graphtype", label = "Select Type of Graph",
                 choices = c("Heatmap", "Histogram", "DataTable"), selected = "Heatmap"),
    conditionalPanel(
      condition = "input.graphtype == 'Histogram' ", 
      radioButtons("colour", label = "3. Select the color of histogram",
                   choices = c("Green", "Red", "Yellow"), selected = "Green")
    )
    
  ),
  
  # Main Panel
  mainPanel(
    textOutput("text1"),
    textOutput("text2"),
    textOutput("text3"),
    conditionalPanel(
      condition = "input.graphtype == 'Heatmap' ", plotlyOutput("heatmap", width = "100%", height="600px")
    ),
    conditionalPanel(
      condition = "input.graphtype == 'Histogram' ", plotOutput("myhist") 
    ),
    conditionalPanel(
      condition = "input.graphtype == 'DataTable' ", DTOutput("tb1") 
    )
  )
  
)


server <-   function(input, output) {
    output$myhist <- renderPlot({
      colm <- as.numeric(input$var)
      hist(stock.frame[, colm], col = input$colour, xlim = c(min(stock.frame[, colm]), max(stock.frame[, colm])), main = "Histogram of stock dataset", breaks = seq(min(stock.frame[, colm]), max(stock.frame[, colm]), l = input$bin + 1), xlab = names(stock.frame[colm]))
    })
    
    output$heatmap <- renderPlotly({plot_ly(x = colnames(stock.frame), y = colnames(stock.frame), z = cmat, type = "heatmap") %>%
        layout(
          xaxis = list(title=colnames(stock.frame)),
          yaxis = list(title="ts")
        )
    })
    
    output$tb1 <- renderDT(stock.frame)
}

# Run the application 
shinyApp(ui = ui, server = server)

output

YBS
  • 19,324
  • 2
  • 9
  • 27