2017-02-16 29 views
0

我將錯誤顯示爲「警告:grep中的錯誤:無效」模式「參數」和「錯誤[:不正確的維數」 ),同時執行閃亮的代碼。請幫忙。下面是代碼片段。當我不評論最後一行時,出現錯誤[:不正確的維數(在執行Shiny R代碼的時候)中的錯誤

library(MASS) 
    library(shinythemes) 
    library(shiny) 
    library(ggplot2) 

    mass.tmp <- data(package = "MASS")[3] 
    mass.datasets <- as.vector(mass.tmp$results[,3]) 

    ui <- fluidPage(

    theme = shinytheme("superhero"), 
    titlePanel("Linear Regression Modelling"), 
    sidebarLayout(
     sidebarPanel(
     selectInput("dsname", "Dataset:",choices = c(mass.datasets)), 
     uiOutput("y_axis"), 
     uiOutput("x_axis") 
    )  , 
     mainPanel(
     tags$br(), 
     tags$br(), 
     "R-squared:", 
     tags$span(tags$b(textOutput("rsquared")),style="color:blue") 
    ) 

    ) 
) 
    server <- function(input, output) { 

    output$x_axis <- renderUI({ 
     col_opts <- get(input$dsname) 
     selectInput("x_axis2", "Independent Variable:", choices = c(names(col_opts))) 
    }) 

    cols2 <- reactive({ 
     col_opts2 <- get(input$dsname) 
     #names(col_opts2)[!grepl(input$x_axis2, names(col_opts2))] 
    }) 

    output$y_axis <- renderUI({ 
     selectInput("y_axis2", "Dependent Variable:", choices = c(names(cols2()))) 
    }) 

    model <- reactive({ 
     #lm(input$dsname[,names(input$dsname) %in% input$y_axis2] ~ input$dsname[,names(input$dsname) %in% input$x_axis2]) 
     #tmp <- paste(input$y_axis2,"~",input$x_axis2,sep = " ") 

     lm(input$y_axis2 ~ input$x_axis2 , data = input$dsname) 
    }) 

    model_summary <- reactive({summary(model())}) 
    output$rsquared <- renderText({ model_summary()$r.squared }) 

    } 

    shinyApp(ui = ui, server = server) 
+1

請提供完全可重複的例子。 – BigDataScientist

+0

我已編輯我的帖子。請指導。 –

回答

1

是的,這樣做更好。 有多個錯誤: 我們不應該爲你調試它,但這裏有一些指針。 這應該會幫助你找到他們。

1) 您正在使用:input$x_axisinput$y_axis,但在最後用「2」來定義它。所以適應這一點。

2) 您應該定義:

cols2 <- reactive({ col_opts2 <- get(input$dsname) names(col_opts2)[!grepl(input$x_axis2, names(col_opts2))] })

renderUI功能之外。

3)此外,似乎有什麼錯這個片斷: names(col_opts2)[!grepl(input$x_axis2, names(col_opts2))]

最後,如果你生產NULLS我會檢查,並禁止通過!is.null()

編輯:問題更新:

你試過用字符串,您可以閃亮的測試外建lm()公式:將不起作用。 您應該使用formula()功能,並拿出事端,如:

lm(formula(paste(input$y_axis2, input$x_axis2, sep =" ~ ")), data = get(input$dsname)) 
+0

執行lm函數時,我現在收到錯誤「invalid'envir'type'character'」參數。 ----模型< - 無效(輸入$ y_axis2〜輸入$ x_axis2,數據=輸入$ dsname) }) –

+0

可以更新代碼然後 – BigDataScientist

+0

代碼更新。調試完成後,我認爲在lm函數中有一個錯誤。我嘗試了不同的事情,但沒有成功。 –

相關問題