【发布时间】:2018-07-29 03:24:04
【问题描述】:
我正在尝试构建一个 R Shiny 应用程序,以按照 r shiny 主页 (https://shiny.rstudio.com/tutorial/written-tutorial/lesson5/) 上的示例显示来自不同数据源的直方图。
当我将“颜色”和“图例”添加到我的开关功能时,我遇到了问题。我尝试将 input$range[1], input$range[2] 添加到我的 hist() 语句中,但这没有帮助。任何人都可以提供有关为什么我在此脚本底部收到错误的见解吗?
如果您添加颜色=颜色和图例=图例,您会看到它应该是什么样子(当然没有颜色或图例)。
这会引发以下错误:
收听http://127.0.0.1:6511 警告:&& 中的错误:“x && y”中的“x”类型无效 堆栈跟踪(最内层优先): 106:绘图.直方图 105:情节 104: 历史默认值 103:历史 102:渲染图[#24] 92: 81:情节对象 80:原始渲染函数 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)
【问题讨论】:
-
你能解决这段代码 sn-p 中的问题吗?我无法在没有错误的情况下运行它。我还在
output$distPlot中注意到x、Matrix1等都被使用而没有在任何地方定义。 -
@gregL,感谢您的评论。我已经修复了代码并添加了一些数据来运行它。现在它坏了。如果您添加 color=color 和 legend = legend,您会看到它应该是什么样子(当然没有颜色或图例)。
标签: r shiny switch-statement histogram