【发布时间】:2018-02-11 03:07:22
【问题描述】:
我正在尝试一个小型闪亮应用程序,其中我从本地目录加载一个 CSV 文件,然后从数据框中选择特定列并使用此子集数据框进行进一步的数据分析。
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
fileInput("dataset", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Include clarifying text ----
#helpText(em("Note: This app requires file in csv format only!!")),
helpText(em("Note:Select all the inputs and click on button as given below to exectute the app")),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
selectInput("select", "Select columns to display", names(datasetInput), multiple = TRUE),
actionButton("update", "Update Data set", class = "btn-primary",style='padding:4px; font-size:120%')
),
# Show a plot of the generated distribution
mainPanel(
h2('The Mydata'),
dataTableOutput('mytable')
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
datasetInput <- eventReactive(input$update, {
validate(need(input$dataset != "", "Please select a data set in csv format only!!!"))# custom error message on opening the app
read.csv(input$dataset$datapath,
header = input$header,
sep = input$sep)
}, ignoreNULL = FALSE)
dataset <- reactive({
df_input<-datasetInput()
df_input$x<-NULL
df_input
})
output$mytable = renderDataTable({
columns = names(dataset)
if (!is.null(input$select)) {
columns = input$select
}
dataset[,columns,drop=FALSE]
})
}
# Run the application
shinyApp(ui = ui, server = server)
查看一些 SO 答案;得到了一些;如下所示:
shiny allowling users to choose which columns to display
但是这个答案,数据集是预定义的;我想让用户下载他自己的数据集。
我收到以下错误:
lapply(obj, function(val) { : object 'datasetInput' not found
中的错误
我认为我必须在某个地方使用 observeEvent 函数?
【问题讨论】:
-
由于
selectInput的choices参数而发生错误。datasetInput在渲染应用的 UI 时不存在。 -
@Imran Ali 我知道....需要知道如何连接上传的 csv 文件并显示上传文件的列.....@987654328 中的数据集 id 应该是什么@参数
-
您需要动态
renderUI。 Dynamic selectInput in R shiny 可能会帮助您入门