【问题标题】:Error: invalid first argument with R Shiny plot错误:R Shiny 图的第一个参数无效
【发布时间】:2017-06-03 21:26:34
【问题描述】:

我编写了一个 R 脚本,用 R 训练自己和其他人使用 Shiny。

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

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

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

欢迎任何想法,谢谢。

两个闪亮的文件 =

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

服务器.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())
  })
})

【问题讨论】:

    标签: r plot get shiny


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 2018-08-18
      • 1970-01-01
      • 2013-07-22
      • 1970-01-01
      • 2017-09-12
      • 2015-03-28
      相关资源
      最近更新 更多