错误:使用R Shiny plot无效的第一个参数

时间:2017-01-19 08:48:42

标签: r plot get shiny

我写了一个R脚本来训练自己和其他人在Shiny上,用R.

可以选择数据集并在基础图上绘制'x'和'y'变量。还有一些其他用户定义的参数。这一切都有效,但它也会踢“错误:无效的第一个参数”,可以在“Plot”选项卡上看到(在闪亮的仪表板上)。

我已经添加了一个“提交”按钮来暂停该过程,您可以清楚地看到错误标志,如果没有提交按钮,则错误会短暂闪烁,消失然后一切正常。

控制台中的其他信息表明它可能与“get”命令有关,但我看不出它可能会进一步引用什么,以及如何处理它。

欢迎任何想法,谢谢。

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())
  })
})

1 个答案:

答案 0 :(得分:3)

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

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())
  })