我很抱歉提前成为R的新手。我现在尝试自己处理这个问题一段时间了,无法解决,但我相信它很容易解决。
我想做一些统计分析(例如线性回归),让用户通过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'必须是data.frame,而不是矩阵或数组
答案 0 :(得分:0)
正如StéphaneLaurent所说,您需要将userInput转换为数据框。显然,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)