![](/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.