【问题标题】:How to choose between histogram and nothing in sidebarPanel?如何在直方图和侧边栏面板中的空白之间进行选择?
【发布时间】:2021-03-31 06:55:16
【问题描述】:

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

由代码创建:

服务器

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]))
    })
  }
)

用户界面

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。选择定量变量”),我可以在其中指定我想要“直方图”还是“无”。如果选择了直方图,那么我应该有与上面相同的东西。当“没有'被选择时,我应该看到空白页面。你知道它是如何执行的吗?

编辑

我按照@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")
        
    )
    
)
)

但是在运行“运行应用程序”后,我看到了错误:

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

我做错了吗?

【问题讨论】:

  • radioButtons("rb", "Plot type:", choiceNames = c("Histogram", "Nothing")。在output$myhist 内,以req(input$rb == "Histogram") 开头。
  • 你能否更严格地描述一下我应该把这个单选按钮放在哪里?
  • 您的问题是“如何有条件地不绘制直方图”,还是“如何添加第二个侧面板”?它们是完全不同的,我建议一种方法来解决第一个问题。
  • 我的问题是 - 如何添加第二个侧面板 - 最后我希望在第二个面板上有不同于直方图的东西(例如数据摘要)。但是,为了更简单,我没有把它放在我的问题中。
  • John - @r2evans 推荐的 radioButtons 应该可以正常工作......我想知道这里是否有误会......当你说第二个侧面板时,我在想你只是想要另一个输入(如单选按钮)来选择“直方图”或“无” - 对吗?听起来您希望在您的ui 中再次使用额外且单独的sidebarPanel()。如果这是正确的,也许我们中的一个人可以提供一个完整的例子来演示。如果我离开了,请告诉我。

标签: r shiny histogram


【解决方案1】:

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多