我想根据复选框输入动态选择的列对数据进行子集。
有什么方法可以使我的输入文件在我的代码中全局可用
以便可以方便地进行进一步的操作。
以下是我的代码:
Server.R
library(shiny)
shinyServer(function(input, output) {
dInput <- reactive({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
finput <- read.csv(inFile$datapath, header=TRUE, sep=',',quote="'")
fheaders <- names(finput)
return(fheaders)
})
output$choose_columns <- renderUI({
checkboxGroupInput("columns", "Choose columns",
choices = dInput(),
selected = NULL)
})
# to observe in environment which columns are selected
observe({ print(input$columns) })
output$data_table <- renderTable({
# If missing input, return to avoid error later in function
if(is.null(input$file1))
return()
# Get the data set
dat <- get(input$file1)
# Keep the selected columns
dat <- dat[, input$columns, drop = FALSE]
# Return first 20 rows
head(dat, 20)
})
})
ui.R
library(shiny)
# Define UI for application
shinyUI(fluidPage(
# Application title
titlePanel("Subset a table"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
uiOutput("choose_columns")
),
mainPanel(
tableOutput("data_table")
)
)
))
显示 tableOutput("data_table") 时出现以下错误
Error : invalid first argument