[英]Error: invalid first argument with R Shiny plot
I have written an R script to train myself and others on Shiny, with R. 我写了一个R脚本,用R对自己和其他人进行Shiny培训。
One can select a dataset and plot an 'x' and 'y' variable on a base plot. 可以选择一个数据集,并在基本图上绘制“ x”和“ y”变量。 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. 我提供了一个“提交”按钮来暂停该过程,您可以清楚地看到错误标志,而没有“提交”按钮的情况下,错误会短暂闪烁,然后消失,然后一切正常。
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. 控制台中的其他信息表明它可能与“ get”命令有关,但我看不到它可能进一步涉及什么以及如何处理。
Any ideas welcome, thanks. 任何想法都欢迎,谢谢。
The 2 shiny files = 2个闪亮的文件=
ui.R 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 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())
})
})
Since you're creating selectInput
dynamically you need to check for NULL
in your renderPlot
. 由于您是动态创建
selectInput
,因此需要在renderPlot
检查NULL
。 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())
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.