【问题标题】:Error in match.arg(position) : 'arg' must be NULL or a character vectormatch.arg(position) 中的错误:“arg”必须为 NULL 或字符向量
【发布时间】:2015-09-29 10:26:43
【问题描述】:

我正在尝试开发一个闪亮的应用程序,其中有多个选项卡上的数据输入。每个选项卡下的内容本身都可以正常工作,但是当我尝试将它们组合到一个应用程序中时,我继续收到此错误。 match.arg(position) 中的错误:“arg”必须为 NULL 或字符向量。我的代码如下:

library(shiny)
library(shinydashboard)
library(ggvis)
sidebar <- dashboardSidebar(
  hr(),
  sidebarMenu(id="tabs",
              menuItem("Import Data", tabName = "Import", icon=icon("list-alt")),
              menuItem("Bivariate Regression", tabName="Bivariate Regression", icon=icon("line-chart")),
              menuItem("Contingency", tabName = "Contingency", icon = icon("table"))
  ))

  body <- dashboardBody(
    tabItems(      
      tabItem(tabName= "Import",
              sidebarLayout(
                sidebarPanel(
                  fileInput("file","Upload the file"), 
                  tags$hr(),
                  h5(helpText("Select the table parameters below")),
                  checkboxInput(inputId = 'header', label= 'Header', value= TRUE),
                  checkboxInput(inputId = "stringsAsFactors", "stringsAsFactors", FALSE),
                  br(),
                  radioButtons(inputId = 'sep', label = 'Seperator', choices = c(Comma=',', Semicolon=';', Tab='\t', Space= ' '), selected= ',')
                ),
                mainPanel(
                  uiOutput("tb")
                )
              )),

      tabItem(tabName= "Bivariate Regression",
              sidebarLayout(
                div(),
                sidebarPanel(
                  fileInput('datfile', ''),
                  selectInput('x', 'x:' ,'x'),
                  selectInput('y', 'y:', 'y'),
                  uiOutput("plot_ui")
                ),
                mainPanel(
                  titlePanel("Plot Output"),
                  ggvisOutput("plot")
                )
              ))
    ))


dashboardPage(
 dashboardHeader(title = "COBE Dashboard"),
  sidebar,
  body)

和服务器

library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output){   
  #read the data and give import prefrences  
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()}
    read.table(file=file1$datapath, sep= input$sep, header= input$header, stringsAsFactors= input$stringsAsFactors)
  })

  # display summary of table output
  output$filledf <-renderTable({
    if(is.null(data())){return ()}
    input$file
  })

  output$sum <- renderTable({
    if(is.null(data())){return ()}
    summary(data())

  })
  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()

  })

  #generate tabsets when the file is loaded. 
  output$tb <- renderUI({
    if(is.null(data()))
      h2("App powered by", tags$img(src='Blue.png', height= 100, width=250))
    else
      tabsetPanel(tabPanel("About file", tableOutput("filledf")), tabPanel("Data", tableOutput("table")), tabPanel("Summary", tableOutput("sum")))
  })
########## Data import end #########

    ########## Bivariate regression begin ###########
    #load the data when the user inputs a file
  theData <- reactive({
    infile <- input$datfile        
    if(is.null(infile))
      return(NULL)        
    d <- read.csv(infile$datapath, header = T)
    d        
  })

  # dynamic variable names
  observe({
    data<-theData()
    updateSelectInput(session, 'x', choices = names(data))
    updateSelectInput(session, 'y', choices = names(data))

  }) # end observe

  #gets the y variable name, will be used to change the plot legends
  yVarName<-reactive({
    input$y
  })

  #gets the x variable name, will be used to change the plot legends
  xVarName<-reactive({
    input$x
  })

  #make the filteredData frame

  filteredData<-reactive({
    data<-isolate(theData())
    #if there is no input, make a dummy dataframe
    if(input$x=="x" && input$y=="y"){
      if(is.null(data)){
        data<-data.frame(x=0,y=0)
      }
    }else{
      data<-data[,c(input$x,input$y)]
      names(data)<-c("x","y")
    }
    data
  })

  #plot the ggvis plot in a reactive block so that it changes with filteredData
  vis<-reactive({
    plotData<-filteredData()
    plotData %>%
      ggvis(~x, ~y) %>%
      layer_points() %>%
      add_axis("y", title = yVarName()) %>%
      add_axis("x", title = xVarName()) %>%
      add_tooltip(function(df) format(sqrt(df$x),digits=2))
  })
  vis%>%bind_shiny("plot", "plot_ui")

 ##### add contingency table ########
  # display contingcy table output
  output$foo <- renderTable({
    if(is.null(data())){return ()}
    as.data.frame.matrix(table((data())))
  })

})

【问题讨论】:

    标签: shiny rstudio ggvis


    【解决方案1】:

    ui.R 中的 tabItems 中的第二个 tabItem 中有一个额外的 div() 元素。提供您暗示的参数或删除该 div() 元素。另外,我在server.RshinyServer() 函数中添加了session 参数。在这些更改后,应用程序运行没有任何错误。

    编辑:

    您忘记在tabItems() 函数中为Contingency 添加一个tabItem。此外,最好区分 tabName 和该选项卡的标题。根据我的经验,选项卡名称中不应有空格,这就是 Bivariate Regression 选项卡以前无法正常工作的原因。它现在应该可以正常工作了。

    更新代码:

    ui.R

    library(shiny)
    library(shinydashboard)
    library(ggvis)
    sidebar <- dashboardSidebar(
      br(),
      sidebarMenu(id="tabs",
                  menuItem("Import Data", tabName = "import", icon=icon("list-alt")),
                  menuItem("Bivariate Regression", tabName="bivariate_regression", icon=icon("line-chart")),
                  menuItem("Contingency", tabName = "contingency", icon = icon("table"))
      ))
    
    body <- dashboardBody(
      tabItems(      
        tabItem(tabName= "import",
                sidebarLayout(
                  sidebarPanel(
                    fileInput("file","Upload the file"), 
                    tags$hr(),
                    h5(helpText("Select the table parameters below")),
                    checkboxInput(inputId = 'header', label= 'Header', value= TRUE),
                    checkboxInput(inputId = "stringsAsFactors", "stringsAsFactors", FALSE),
                    br(),
                    radioButtons(inputId = 'sep', label = 'Seperator', choices = c(Comma=',', Semicolon=';', Tab='\t', Space= ' '), selected= ',')
                  ),
                  mainPanel(
                    uiOutput("tb")
                  )
                )),
    
        tabItem(tabName= "bivariate_regression",
                sidebarLayout(
                  #div(),
                  sidebarPanel(
                    fileInput('datfile', ''),
                    selectInput('x', 'x:' ,'x'),
                    selectInput('y', 'y:', 'y'),
                    uiOutput("plot_ui")
                  ),
                  mainPanel(
                    titlePanel("Plot Output"),
                    ggvisOutput("plot")
                  )
                )),
        tabItem(tabName="contingency", h2("Contigency Tab content"))
      ))
    
    
    dashboardPage(
      dashboardHeader(title = "COBE Dashboard"),
      sidebar,
      body)
    

    服务器.R

    library(shiny)
    library(dplyr)
    library(ggvis)
    shinyServer(function(input, output,session){   
      #read the data and give import prefrences  
      data <- reactive({
        file1 <- input$file
        if(is.null(file1)){return()}
        read.table(file=file1$datapath, sep= input$sep, header= input$header, stringsAsFactors= input$stringsAsFactors)
      })
    
      # display summary of table output
      output$filledf <-renderTable({
        if(is.null(data())){return ()}
        input$file
      })
    
      output$sum <- renderTable({
        if(is.null(data())){return ()}
        summary(data())
    
      })
      output$table <- renderTable({
        if(is.null(data())){return ()}
        data()
    
      })
    
      #generate tabsets when the file is loaded. 
      output$tb <- renderUI({
        if(is.null(data()))
          h2("App powered by", tags$img(src='Blue.png', height= 100, width=250))
        else
          tabsetPanel(tabPanel("About file", tableOutput("filledf")), tabPanel("Data", tableOutput("table")), tabPanel("Summary", tableOutput("sum")))
      })
      ########## Data import end #########
    
      ########## Bivariate regression begin ###########
      #load the data when the user inputs a file
      theData <- reactive({
        infile <- input$datfile        
        if(is.null(infile))
          return(NULL)        
        d <- read.csv(infile$datapath, header = T)
        d        
      })
    
      # dynamic variable names
      observe({
        data<-theData()
        updateSelectInput(session, 'x', choices = names(data))
        updateSelectInput(session, 'y', choices = names(data))
    
      }) # end observe
    
      #gets the y variable name, will be used to change the plot legends
      yVarName<-reactive({
        input$y
      })
    
      #gets the x variable name, will be used to change the plot legends
      xVarName<-reactive({
        input$x
      })
    
      #make the filteredData frame
    
      filteredData<-reactive({
        data<-isolate(theData())
        #if there is no input, make a dummy dataframe
        if(input$x=="x" && input$y=="y"){
          if(is.null(data)){
            data<-data.frame(x=0,y=0)
          }
        }else{
          data<-data[,c(input$x,input$y)]
          names(data)<-c("x","y")
        }
        data
      })
    
      #plot the ggvis plot in a reactive block so that it changes with filteredData
      vis<-reactive({
        plotData<-filteredData()
        plotData %>%
          ggvis(~x, ~y) %>%
          layer_points() %>%
          add_axis("y", title = yVarName()) %>%
          add_axis("x", title = xVarName()) %>%
          add_tooltip(function(df) format(sqrt(df$x),digits=2))
      })
      vis%>%bind_shiny("plot", "plot_ui")
    
      ##### add contingency table ########
      # display contingcy table output
      output$foo <- renderTable({
        if(is.null(data())){return ()}
        as.data.frame.matrix(table((data())))
      })
    
    })
    

    【讨论】:

    • 感谢@Shiva 的建议。更新后的代码使应用程序运行,但是当我更改为不同的选项卡时,双变量回归和列联表不会显示。我想让每个标签也能正常工作。
    • 非常感谢!它完美地工作我不能更高兴!谢谢湿婆!
    • 很高兴知道它对您有所帮助!请点赞并将答案标记为已接受,以便将来帮助他人!
    • 只要我获得足够高的声望就会这样做!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多