R 如何在侧边栏面板中选择直方图和无? 让我们考虑一下我的基本应用:

R 如何在侧边栏面板中选择直方图和无? 让我们考虑一下我的基本应用:,r,shiny,histogram,R,Shiny,Histogram,由代码创建: 服务器 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.C

由代码创建:

服务器

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]))
    })
  }
)
我想要另一个
侧边栏面板
(类似于“1.选择定量变量”),我可以在其中指定我想要的是“直方图”还是“无”。若选择直方图,那个么我应该有和上面一样的东西。当选择“无”时,我会看到空白页。你知道如何执行吗

编辑

我按照@r2evans的建议添加了radiobutton。它现在看起来如下所示:

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)
库(闪亮)
图书馆(quantmod)

开始
单选按钮(“rb”,“绘图类型:”,choiceNames=c(“直方图”,“无”)
。在
输出$myhist
中,从
请求(输入$rb==“直方图”)
开始。您能更严格地描述一下我应该把这个单选按钮放在哪里吗?您的问题是“如何有条件地不绘制直方图”,还是?”如何添加第二个侧面板?它们完全不同,我建议一种解决第一个问题的方法。我的问题是-如何添加第二个侧面板-在第二个面板的末尾,我希望有一些不同于直方图的内容(例如,数据摘要)但是,为了更简单起见,我没有把这一点放在我的问题中。John-由@r2evans推荐的
单选按钮应该可以很好地工作…我想知道这里是否有沟通错误…当你说第二个侧面板时,我想你只需要另一个输入(如单选按钮)来选择“直方图”或“无”“-是这样吗?听起来你不想在你的
ui
中再次使用另一个单独的
sidebarPanel()
。如果是这样,也许我们中的一个人可以提供一个完整的例子来演示。如果我不想,请告诉我。”。
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)