![](/img/trans.png)
[英]R Shiny does not plot the bar plot on dialogue box
[英]Box Plot in Shiny R
我正在尝试通过Shiny创建/学习一个交互式箱形图,在id下面是我要使用的代码。 这给我错误
警告:model.frame.default中的错误:变量长度不同(为“ input $ p”找到)
[没有可用的堆栈跟踪]
我无法弄清楚,任何帮助将不胜感激
码:
library(shiny)
ui <- fluidPage(
selectInput("p","p",choices = names(mtcars)),
plotOutput("myplot"))
server <- function(input, output, session) {
output$myplot <- renderPlot({
boxplot(mpg ~ input$p , data=mtcars)
})
}
shinyApp(ui, server)
boxplot
期望boxplot(mpg ~ cyl , data=mtcars)
而input$p
将返回如下字符向量
Browse[1]> input$p
[1] "mpg"
一种解决方案是使用as.formula
library(shiny)
ui <- fluidPage(
#use setdiff to avoid this Error 'Error in .subset2: attempt to select less than one element in integerOneIndex'
selectInput("p","p",choices = setdiff(names(mtcars),"mpg")),
plotOutput("myplot"))
server <- function(input, output, session) {
output$myplot <- renderPlot({
m <- paste0('mpg','~',input$p)
boxplot(as.formula(m) , data=mtcars)
})
}
shinyApp(ui, server)
要获得更多解释/见解,请参阅此问题
你为什么不只使用get
library(shiny)
ui <- fluidPage(
selectInput("p","p",choices = names(mtcars)),
plotOutput("myplot"))
server <- function(input, output, session) {
output$myplot <- renderPlot({
boxplot(mpg ~ get(input$p) , data=mtcars)
})
}
shinyApp(ui, server)
如果有机会,也许您想签出ggplot2库。 它们具有非常非常好用且易于使用的功能和漂亮的情节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.