R使用开关进行直方图时的闪亮错误:&&amp ;: error' x'输入' x&& ý'

时间:2018-02-18 21:42:26

标签: r shiny switch-statement histogram

我试图构建一个R闪亮的应用程序来显示来自不同数据源的直方图,遵循r闪亮主页(https://shiny.rstudio.com/tutorial/written-tutorial/lesson5/)上的示例。

当我添加" color"时,我遇到了问题。和"传奇"到我的开关功能。我尝试添加input $ range [1],将$ range [2]输入到我的hist()语句中,但这并没有帮助。任何人都可以提供有关我为什么会收到错误的见解我在这个脚本的底部?

如果你添加color = color和legend = legend,你会看到它应该是什么样的(当然没有颜色或图例)。

这会引发以下错误:

聆听http://127.0.0.1:6511 警告:错误&&:无效' x'输入' x&& ý' 堆栈跟踪(最里面的第一个):     106:plot.histogram     105:情节     104:hist.default     103:hist     102:renderPlot [#24]      92:      81:plotObj      80:origRenderFunc      79:输出$ distPlot       4:       3:do.call       2:print.shiny.appobj       1:

N<- 500
M<-31

Data1 <-matrix( rnorm(N*M,mean=30,sd=3.5), N, M)
Data2 <-matrix( rnorm(N*M,mean=23,sd=3), N, M)
Data3 <- matrix( rnorm(N*M,mean=30,sd=4), N, M) 

# Define UI  ----
ui <- fluidPage(
  titlePanel(code(strong("Tool"), style = "color:black")),
  sidebarLayout(
    sidebarPanel(
      strong("Tools:"),
      selectInput("Test", 
                  label = "Choose a measure to display",
                  choices = c("Test1", 
                              "Test2",
                              "Test3"
                  ),
                  selected = "Test1"),

      sliderInput(inputId="slider1", label = "Bins",
                  min = 1, max = 300, value = 200)),
    mainPanel(
      code(strong("Study Readout")),
      plotOutput(outputId = "distPlot")
    )))

# Define server logic ----
server <- function(input, output) {
  output$distPlot <- renderPlot({
    slider1 <- seq(floor(min(x)), ceiling(max(x)), length.out = input$slider1 + 1)
    x    <- switch(input$Test, 
                   "Test1" = Data1,
                   "Test2" = Data2, 
                   "Test3" = Data3)
    color <- switch(input$Test, 
                    "Test1" = "darkgreen",
                    "Test2" = "darkorange",
                    "Test3" = "darkviolet")

    legend <- switch(input$Test, 
                     "Test1" = "Test1",
                     "Test2" = "Test2",
                     "Test3" = "Test3")


    #hist = qplot(x, breaks = slider1, fill=..count.., geom="histogram") +
    #  scale_fill_gradient(low="orangered2",high="yellow",guide = 'none')+
    #  theme_bw()+labs(x="Population Range of Executive Functioning") + labs(y="")


    hist(x, color, legend, breaks = slider1)

  })
}


# Run that shit ----
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

问题原来是hist的标签。我正在关注网站上的例子,但没有考虑如何使用hist标签颜色和图例。这是修复:

hist(x,col = color,main = legend,breaks = slider1)