如何在 sidebarPanel 中的直方图和无之间进行选择?

How to choose between histogram and nothing in sidebarPanel?

让我们考虑一下我的最基本的应用程序:

由代码创建:

服务器

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")
        
    )
    
)
)

我想要另一个 sidebarPanel(类似于“1.Select 定量变量”),我可以在其中指定我想要 'Histogram' 还是 'nothing' .如果选择直方图,那么我应该有与上面相同的东西。当“没有选择”时,我应该看到空白页。您知道如何执行它吗?

编辑

我按照@r2evans 的建议添加了单选按钮。现在看起来是这样的:

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")
        
    )
    
)
)

但是在 运行 'Run App' 之后我看到错误:

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

我是不是做错了什么?

也许您正在寻找这样的解决方案

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)