我正在尝试编写一个脚本来上传.csv文件,并在UI中返回一个子集选项(以复选框的形式)。上传后,将显示以前不可见的复选框,显示实际数据值。同时,还会出现一个操作按钮,在(可能)更改所选数据值(通过复选框)后单击该按钮。
那我该怎么办?
1)上传 - 确定。
2)选择和显示复选框 - 确定。
3)显示操作按钮 - 确定。
4)正确执行所需的操作(=对数据进行子集化) - 不行;显然,subsetting返回一个空的或NULL数据帧。
非常感谢任何帮助。
这是代码。
我将补充说,我基于子集的Dte字段是一个日期字段,最初在csv中为dd / mm / yyyy格式且没有引号。
library(shiny)
library(lubridate)
ui = fluidPage(sidebarPanel(width = 2,
fileInput('files', '', accept = c('text/csv','text/comma-separated-values','text/tab-separated-values','text/plain','.csv','.tsv')),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator', c(Comma=',',Semicolon=';',Tab='\t'), selected=';'),
radioButtons('quote', 'Quote', c(None='','Double Quote'='"','Single Quote'="'"), selected='"'),
uiOutput('apply')),
mainPanel(width = 10, uiOutput('years'), dataTableOutput("tabel4")
)
)
server = function(input, output, session) {
### if a csv is selected (through fileInput()), it is read
vis0 <- eventReactive(input$files, {
vis <- read.csv(input$files$datapath, header = input$header, sep = input$sep, quote = input$quote,
stringsAsFactors =FALSE)
vis$Dte <- as.Date(as.character(vis$Dte), "%d/%m/%Y")
vis$year <- factor(year(vis$Dte))
vis
})
### button appearing only after
output$apply <- renderUI({
if (is.null(input$files)) return()
actionButton("Load", "APPLY DATA")
})
### get checkbox values from data and throw them back to UI
output$years <- renderUI({
df <- vis0()
df$year <- as.character(df$year)
if (is.null(df)) return(NULL)
items= sort(unique(df$year))
names(items)=items
checkboxGroupInput("dependent","Check or uncheck years to be included or excluded",
choices=items, selected = items, inline = TRUE)
})
### use selected checkbox values to reduce initial dataset
vis1 <- reactive({
if (is.null(input$Load)) return()
if (input$Load==0) return()
data <- vis0()
data <- subset(data, as.character(year) %in% input$years)
data
})
### using the checkbox-reduced data to create a table
output$tabel4 <- renderDataTable({
vis1()
})
}
shinyApp(ui = ui, server = server, options = list(launch.browser=TRUE))
答案 0 :(得分:0)
我找到了解决方案!
一个愚蠢的错误(至少)是A
必须是output = A(ismember(A(:,j), b), :);
。也许有更多(特别是在dependent
检查中)我修复并且没有正确跟踪,但下面的代码可以工作。
years