简体   繁体   English

条形图错误与Shiny R

[英]Bar plot error with Shiny R

I am unable resolve the error with a shiny app to create a bar plot.The error that shows up is no argument passed.The Code is given below. 我无法使用闪亮的应用程序解决该错误以创建条形图。显示的错误是未传递任何参数。代码如下。

Ui.R Ui.R

shinyUI(fluidPage(
  titlePanel("An Overview of my skillsets"),

  sidebarLayout(
    sidebarPanel(                  
                   selectInput("dataset", "Select Company", 
                               choices = c("Accenture", "Auroch", "Quintiles", "Others")),

                   numericInput("obs", "Number of observations to view:", 10) ),
    mainPanel(plotOutput(""))
  )
))

Server.R Server.R

library(shiny)

shinyServer(function(input, output) {

  TechSkills <- c( "X", "Y", "Z", "A", "B", "C")

  TS_ACC <-c(25,26,22,12,22,27)
  TS_AUR <- c(8,6,7,5,4,7)
  TS_QUIN <- c(12,12,14,24,17,27)
  ACCENTURE=data.frame(TechSkills, TS_ACC)
  AUROCH=data.frame(TechSkills, TS_AUR)
  QUINTILES=data.frame(TechSkills, TS_QUIN)

  datasetInput <- reactive({
    switch(input$dataset,
           "Accenture" = ACCENTURE,
           "Auroch" = AUROCH,
           "Quintiles" = QUINTILES,
            "Others" =OTHERS)
  })
  output$dataset <- renderPlot({
    barplot(data=dataset)
  })
 })

There are several things that could be wrong in this code: 此代码中可能有几处错误:

  1. plotOutput("") should have an outputId, ie 'dataset' plotOutput(“”)应该具有outputId,即“数据集”
  2. barplot does not take parameter data, you should provide 'height' and 'arg.names' barplot不接受参数数据,应提供“ height”和“ arg.names”

So, the code should look something like that: 因此,代码应如下所示:

ui.R ui.R

shinyUI(fluidPage(
  titlePanel("An Overview of my skillsets"),

  sidebarLayout(
    sidebarPanel(                  
               selectInput("dataset", "Select Company", 
                           choices = c("Accenture", "Auroch", "Quintiles", "Others")),

               numericInput("obs", "Number of observations to view:", 10) ),
mainPanel(plotOutput('dataset'))
  )
))

server.R server.R

library(shiny)

TechSkills <- c( "X", "Y", "Z", "A", "B", "C")

TS_ACC <-c(25,26,22,12,22,27)
TS_AUR <- c(8,6,7,5,4,7)
TS_QUIN <- c(12,12,14,24,17,27)
ACCENTURE=data.frame(TechSkills, TS_ACC)
AUROCH=data.frame(TechSkills, TS_AUR)
QUINTILES=data.frame(TechSkills, TS_QUIN)


shinyServer(function(input, output) {


  datasetInput <- reactive({
    switch(input$dataset,
           "Accenture" = ACCENTURE,
           "Auroch" = AUROCH,
           "Quintiles" = QUINTILES,
            "Others" =OTHERS)
  })
  output$dataset <- renderPlot({
    d <- datasetInput()
    barplot(height=d[,2],names.arg=d[,1])
  })
 })

A minute too late, but here a variant with the little known function setNames 一分钟为时已晚,但是这里是鲜为人知的函数setNames的变体

  output$mybarplot <- renderPlot({
    d1 = datasetInput()
    barplot(setNames(d1[,2],d1[,1]))
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM