错误:R Shiny图的第一个参数无效 [英] Error: invalid first argument with R Shiny plot

查看:55
本文介绍了错误:R Shiny图的第一个参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个R脚本,用R对自己和其他人进行Shiny培训.

I have written an R script to train myself and others on Shiny, with R.

可以选择一个数据集,并在基本图上绘制"x"和"y"变量.也有几个其他用户定义的参数.一切正常,但是还会出现错误:无效的第一个参数",可以在绘图"选项卡上(在闪亮的仪表板上)看到.

One can select a dataset and plot an 'x' and 'y' variable on a base plot. There are a couple of other user defined arguments also. It all works, however it also kicks 'Error: invalid first argument', which can be seen on the 'Plot' tab (on the shiny dashboard).

我提供了一个提交"按钮来暂停该过程,您可以清楚地看到错误标志,而没有提交"按钮的情况下,错误会短暂闪烁,然后消失,然后一切正常.

I have included a 'Submit' button to pause the process down and you can see the error sign clearly, with out the submit button the the error flashes briefly, disappears and then everything works.

控制台中的其他信息表明它可能与"get"命令有关,但是我看不到它可能进一步涉及什么以及如何处理.

Additional information in the console suggests it may have something to do with a 'get' command, but I can't see what it may refer to further, and what to do about it.

欢迎提出任何想法,谢谢.

Any ideas welcome, thanks.

2个闪亮的文件=

ui.R

library(shiny)
data_sets = c("iris", "mtcars", "trees")

shinyUI(fluidPage(

  titlePanel(h1("Plotting Playaround")),

    sidebarLayout(

    sidebarPanel(

      selectInput("var_data", "Select a dataset to plot up!", choices = data_sets),
      br(),
      uiOutput("x_var"),
      br(),
      uiOutput("y_var"),
      br(),
      br(),
      selectInput("plt_pts", "What sorta plot points do ya want?", 
              choices = c("points" = "p" ,
                          "lines" = "l" ,
                          "both" = "b" ,
                          "lines_only" = "c" ,
                          "overplotted" = 'o' ,
                          "hist_like" = 'h' ,
                          "stairs" = "s" ,
                          "alt_stairs"= "S",
                          "no_plot" = "n" )),
      radioButtons("plt_col", "Choose a colour!", 
               choices = c("Red",
                           "Green",
                           "Blue")),
      submitButton("Submit!")

    ),

    mainPanel(

      tabsetPanel(type = 'tab',
        tabPanel("Plot", plotOutput("p")),
        tabPanel("Summary", verbatimTextOutput("sum"))

      ) # tabsetPanel
      ) # mainPanel
)))

server.R

library(shiny)
shinyServer(function(input, output){

  # reactive variables
  data_var = reactive({
    switch (input$var_data,
      "iris" = names(iris),
      "mtcars" = names(mtcars),
      "trees" = names(trees)
    )
  })

 my_data = reactive({
    switch (input$var_data,
        "iris" = iris,
        "mtcars" = mtcars,
        "trees" = trees
   )
  })

    pltpts = reactive({
    as.character(input$plt_pts)
  })

  pltcol = reactive({
    input$plt_col
  })

  # outputs
  output$x_var = renderUI({
    selectInput("variablex", "Select the 'X' variable!", choices = data_var())
  })

  output$y_var = renderUI({
selectInput("variabley", "select the 'Y' variable", choices = data_var())

})

  output$p = renderPlot({
    attach(get(input$var_data))
    plot(x = get(input$variablex), 
         y = get(input$variabley), 
         xlab = input$variablex, 
         ylab = input$variabley, 
         type = pltpts(),
         col = pltcol())
  })

  output$sum = renderPrint({
    summary(my_data())
  })
})

推荐答案

由于要动态创建 selectInput ,因此需要在 renderPlot中检查 NULL .像这样:

Since you're creating selectInput dynamically you need to check for NULL in your renderPlot. Like so:

output$p = renderPlot({   
    if(is.null(input$variablex) || is.null(input$variabley)){return()}

    attach(get(input$var_data))
    plot(x = get(input$variablex), 
         y = get(input$variabley), 
         xlab = input$variablex, 
         ylab = input$variabley, 
         type = pltpts(),
         col = pltcol())
  })

这篇关于错误:R Shiny图的第一个参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆