2017-10-29 15 views
0

R에 익숙해 져서 죄송합니다. 지금 당분간 문제를 직접 처리해 보았지만 알아낼 수는 없지만 해결하기는 쉽습니다. .ShinyIncubator : matrixinput을 사용한 선형 회귀

일부 통계 분석 (예 : 선형 회귀 분석)을 수행하고 matrixInput을 통해 사용자가 직접 데이터를 입력하게하고 싶습니다.

library(shiny) 
library(shinyIncubat) 

df <- data.frame(matrix(c("0","0"), 1, 2)) 
colnames(df) <- c("x", "y") 

ui <- pageWithSidebar(
    headerPanel('Enter data (x,y) here'), 
    sidebarPanel(
    matrixInput('foo', 'Foo', data=df) 
), 
    mainPanel(
    verbatimTextOutput(outputId = "linreg") 
) 
)) 

    server <- function(input,output) { 

    lm1 <- reactive({lm(y~x,data=input$foo)}) 
    output$linreg <- renderPrint({summary(lm1())}) 

} 

shinyApp(ui = ui, server = server) 

나는 오류가 발생 : '데이터'는 data.frame 아닌 매트릭스 또는 스테판 로랑 말했듯이, 당신은 데이터 프레임에 UserInput 사용자를 변환 할 필요가

+0

- 반응은 ({LM (Y ~ X, 데이터 = as.data.frame (입력 $ foo는))})' –

답변

0

배열해야합니다. 분명히 matrixInput은 colnames의 이름을 변경하기 때문에 이후에 새 데이터 프레임의 열 이름을 바꿔야합니다. 그렇지 않으면 회귀가 실패합니다.

library(shiny) 
library(shinyIncubator) 

df <- data.frame(matrix(c("0","0"), 1, 2)) 
colnames(df) <- c("x", "y") # you don't need this 

ui <- pageWithSidebar(
    headerPanel('Enter data (x,y) here'), 
    sidebarPanel(
    matrixInput('foo', 'Foo', data=df) 
), 
    mainPanel(
    verbatimTextOutput(outputId = "linreg") 
) 
) 

    server <- function(input,output) { 

    lm1 <- reactive({ 

    lmdata<-as.data.frame(input$foo) 
    names(lmdata)[1]<-'y' 
    names(lmdata)[2]<-'x' 

    reg<-summary(lm(y~x,data=lmdata)) 
    reg 
    }) 

    output$linreg <- renderPrint({lm1()}) 
    # output$linreg <- renderPrint({print(input$foo)}) this way you can check how the userInput looks like 

} 

shinyApp(ui = ui, server = server) 
당신은 LM1 <`시도 할 수
+0

이 시간을내어 주셔서 너무 감사가 도움을 초보자! P. 당신의 코드에 ')'가 없습니다 : '# output $ linreg <- renderPrint ({print (input $ foo)})' 아무도 그것을 복사하려고 시도한 경우에 대비해. –

+0

맞아요, 위 코드를 편집했습니다. – laurenzo

+0

'names (lmdata)'와'input $ foo'를 결합하는 방법이 있습니까? (input $ foo (names (input $ foo) [1] <- 'y', 이름 (input $ foo) [2] <- '같은 것을 만들기를 원한다. x '))})'당신이 보았던 것처럼 나는 올바른 구문을 모르고 어떤 문서도 찾지 못했고, 어떻게 완성되었는지를 알 수있었습니다. –