【发布时间】:2015-06-22 08:35:35
【问题描述】:
我想创建一个闪亮的应用程序,用户将在其中上传 csv 文件,上传后下拉框将填充数据集中的所有变量。
当用户从下拉框中选择一个变量时,应在主面板中绘制相应的直方图。
这是我所做的
ui.r 文件
library(shiny)
shinyUI(pageWithSidebar(
headerPanel( "Demand Forecast", "Flowserve"),
sidebarPanel(
fileInput('file1', 'Select csv file',accept=c('text/csv')
),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator', c(Comma=',',Tab='\t', Semicolon=';' )
),
tags$hr(),
selectInput("product", "Select: ","")
),
mainPanel(tableOutput('contents'),
plotOutput("hist")
)
))
server.r 文件
library(shiny)
library(ggplot2)
shinyServer(function(input,output,session){
dataUpload<-reactive({
inFile<-input$file1
print(inFile)
if(is.null(inFile))
return(NULL)
dt_frame = read.csv(inFile$datapath, header=input$header, sep=input$sep)
updateSelectInput(session, "product", choices = names(dt_frame))
})
output$hist <- renderPlot({
dataset<- dataUpload()
hist(dataset[,dataset$product])
})
})
但是当我运行这个应用程序时,它给了我一个错误“闭包”类型的对象不是子集
请帮忙..
提前致谢
【问题讨论】:
-
这是一个很好的例子,说明为什么不 使用函数名作为对象名 - 一个原因是它会导致混淆错误消息。
dt是基础 R 中的一个函数(请参阅?dt),当您调用dt[, colm]而范围内不存在数据框dt时,R 将尝试对函数dt进行子集化。因此,看看您是否可以弄清楚为什么您的数据框dt不在您的hist调用范围内。 -
@jbaums 感谢您的 rpl.. 我已经更改了我的 server.r 文件现在当我运行这个应用程序时,它给了我一个错误,即 X 必须是数字
-
传递给
hist()函数的值是数字吗? -
@Keniajin No.. 其实我是直接从 drop box 中取出变量的。所以,它也包含其他类型的变量。