首页 文章

闪亮的反应箱图,具有交互式平均线

提问于
浏览
0

我是R的新手并且已经尝试了一些东西 . 现在我想创建一个相当简单的闪亮应用程序,目的是询问有关我在keggle找到的数据集的问题 . 该数据集涉及旧金山的工资 .

我的想法如下:

创建一个boxplot,如下所示:

类别< - cut(工资$ TotalPay,break = c(0,30000,60000,100000,500000),labels = c(“low”,“mid”,“high”,“highest”))boxplot(TotalPay~分类)

我想让用户选择他/她想要看到的这些或所有这些 .

目前我有:

UI:

library(shiny)

shinyUI(pageWithSidebar(


  headerPanel("Miles Per Gallon"),

  sidebarPanel(
    selectInput("variable", "Variable:",
                list("Low" = "low", 
                     "Mid" = "mid", 
                     "High" = "high",
                     "Highest"= "highest")),

  mainPanel(
    h3(textOutput("caption")),

    plotOutput("Plot")
  )
)
  )
)

Server:

library(shiny)

Categories <- cut(Salaries$TotalPay, breaks = c(0,30000,60000,100000,500000), labels=c("low","mid","high","highest"))

shinyServer(function(input, output) {


  formulaText <- reactive({
    paste("TotalPay~", input$variable)
  })

  output$caption <- renderText({
    formulaText()
  })


  Plot <- renderPlot({
    boxplot(as.formula(formulaText()), 
            data = Categories
            )
  })
})

我究竟做错了什么?我认为从“工资”导入数据是个问题 .

提前致谢 :) .

1 回答

  • 0

    为了澄清我的注释,你使用libary()来加载包,但这些包可能是来自CRAN或基础R的数据集 . 但是对于你的例子,你需要在server.r文件中这样的东西:

    salaries <- read.csv("Salaries.csv")
    

    确保csv文件与ui.r和server.r文件位于同一文件夹中

    由于您是R和闪亮的新手,这个示例应用程序可能有助于使用ui小部件:https://shiny.rstudio.com/gallery/telephones-by-region.html

    请注意,上面的示例使用“数据集”库中的数据集 .

    EDIT:

    我注意到其他一些项目引发了一些其他问题 . 您是否在Shiny Server上部署此应用程序?当我认为它需要更像这样时,你的服务器功能正在使用ShinyServer:

    server <- function(input, output) { 
    #server code
    
    shinyApp(ui= ui, server = server)
    }
    

    你的ui应该更像这样:

    ui <- fluidPage(
    #ui code
    )
    

相关问题