[英]Make a boxplot interactive using Shiny
我一直在嘗試使用Shiny開發具有選擇性輸入的交互式箱線圖。
當前代碼:
library(shiny)
shinyUI(fluidPage(
titlePanel("Sample 1"),
sidebarLayout(
sidebarPanel(
selectInput("p", "Choose your salaries", choices = c("low"='a',"mid"='b',"high"='c',"riches!"='d'), selected = 4)
),
mainPanel(
plotOutput("boxplot")
)
)
))
library(shiny)
read.csv("Salaries.csv")
Categories <- cut (Salaries$TotalPay, breaks = c(0,36466,73678,104359,567595), labels = c("low","mid","high","riches!"))
shinyServer(function(input, output){
output$boxplot <- renderPlot({
if(input$p=='a'){
i<"1"
}
if(input$p=='b'){
i<-"2"
}
if(input$p=='c'){
i<-"3"
}
if(input$p=='d'){
i<- "riches!"
}
boxplot(TotalPay~Categories[i])
})
})
我無法讓箱形圖對UI中做出的選擇做出反應。 我懷疑這和我打電話時的水平有關:
> Categories["riches!"]
[1] <NA>
Levels: low mid high riches!
'我需要添加這些因素嗎? 還是我完全忘記了要點? 提前致謝!
看一下如何通過名稱訪問列 。 以下示例是mtcars
數據集
library(shiny)
ui <- fluidPage(
selectInput("p","p",choices = names(mtcars)),
plotOutput("myplot"))
server <- function(input, output, session) {
output$myplot <- renderPlot({
boxplot(mtcars[,input$p])
})
}
shinyApp(ui, server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.