【问题标题】:Showing Model Predictions with Shiny in R在 R 中用 Shiny 显示模型预测
【发布时间】:2015-05-27 21:36:58
【问题描述】:

我很乐意在 R Shiny 方面获得一些帮助,我是新手。 我构建了一个逻辑回归模型,该模型在名为“Rdata1.csv”的训练集上运行。 然后在我训练模型之后,我想让用户上传一个测试集文件 并在测试集上运行预测。

这是我的 ui.R:

 library(shiny)
 shinyUI(fluidPage(
 titlePanel("Predictions"),

 sidebarLayout(
 sidebarPanel(
  fileInput("file", label = h3("Upload CSV")),

  hr(),
  fluidRow(column(4, verbatimTextOutput("value")))
  ),

mainPanel(
  textOutput("text1")
)
)
))

这是我的服务器。R:

library(shiny)
library(aod)
library(ROCR)

mydata <- read.csv("C:/Rdata1.csv")
mydata$Continent <- factor(mydata$Continent)
mydata$IP <- factor(mydata$IP)

mylogit <- glm(Good ~ Continent + IP, data = mydata, family = "binomial")

shinyServer(function(input, output) {

 output$text1 <- renderPrint({ 
 mydatanew <- read.csv(input$file[4])
 mydatanew$Continent <- factor(mydatanew$Continent)
 mydatanew$IP <- factor(mydatanew$IP)
 mydatanew$predicted<-predict(mylogit, newdata=mydatanew, type="response")
 paste("predictions", mydatanew)
  })

 }
 )

在应用程序中,我没有得到任何输出 - 相反,我得到了错误: “文件必须是字符串或连接”,我什么也没看到。 有人知道代码有什么问题吗? 谢谢!

【问题讨论】:

  • 我有一段时间没有使用 Windows R...但是文件名中的斜线是否正确,您可能必须转义它们...可能是“C:\ Rdata1.csv”或“C:\\Rdata1.csv”
  • 嗨,是的,他们是正确的,脚本单独运行良好,但闪亮却不行

标签: r shiny


【解决方案1】:

欢迎来到 SO。这个问题很难重现,但看起来fileInput 中的datapath 没有正确传递。见?fileInput

Whenever a file upload completes, the corresponding input variable is set to a dataframe. 
This dataframe contains one row for each selected file, and the following columns: ...

...第四个是datapath

换句话说

file <- data.frame(name="foo",size=999,type="text/plain",datapath = "~/temp/whatever.csv", stringsAsFactors=FALSE)

str(file[4]) # this gives you a little data frame, probably not what you want
str(file[1,4]) # this gives you the character value itself

这行得通吗?

【讨论】:

  • 感谢您的回答。我尝试用文件 [1,4] 替换文件 [4],但没有成功
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 2019-06-08
  • 2018-08-30
  • 2019-09-12
相关资源
最近更新 更多