使用Shiny制作一个boxplot交互式

时间:2022-02-17 13:02:15

I've been trying to develop an interactive boxplot with selective input in Shiny.

我一直在尝试用Shiny中的选择性输入开发一个交互式的boxplot。

current code:

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])

  })
})

I can't get the boxplot to react to the selection made in the UI. I suspect it has to do with the levels as when I call:

我无法让boxplot对UI中的选择作出反应。我怀疑它与我打电话时的等级有关:

> Categories["riches!"]
[1] <NA>
Levels: low mid high riches!

' Do i need to add factors to these? Or am I missing the point entirely? Thanks in advance!

'我需要为这些添加因素吗?还是我完全忽略了这一点?提前致谢!

1 个解决方案

#1


2  

Have a look how to access the column by name. Example below is with mtcars dataset

看看如何按名称访问列。以下示例使用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)

#1


2  

Have a look how to access the column by name. Example below is with mtcars dataset

看看如何按名称访问列。以下示例使用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)