Shiny:从反应变量中删除列



我想使用shine读取CSV文件,然后允许用户删除所选列。

下面是我的例子。但我一直收到no applicable method for 'select' applied to an object of class "c('reactiveExpr', 'reactive', 'function')"错误。

删除功能已经处于被动环境中,我不知道如何解决这个问题。

非常感谢你的帮助。

library(shiny)
ui = fluidPage(
fileInput(inputId = "rawFile", label="File"), 
uiOutput("selectColumn"),
actionButton(inputId = "Remove", label = "Remove"),
tableOutput("DFTable")
)
server = function(input, output, session){
# initiate a reactive value
df <- reactiveValues(data = NULL)
removecolumn <- function(df, nameofthecolumn){dplyr::select(df, -nameofthecolumn)}

inputData <- reactive({
inFile <- input$rawFile
if (is.null(inFile)){return(NULL)}
read.csv(inFile$datapath, header = T)
})

# update column names
output$selectColumn <- renderUI({
shiny::req(inputData())
selectInput(inputId = "selectColumn",
label = "Remove selected sample(s)",
multiple = TRUE,
choices = names(inputData())
)
})

df$data <- reactive({inputData()})

# delete column
observeEvent(input$Remove, {
df$data <- removecolumn(df$data, input$selectColumn)
})

# show result
output$DFTable <- renderTable(df$data())

}
shinyApp(ui, server)

您可以在reactive中分配以读取原始数据,而不是分配给df$data(即df$data<-reactive(inputData())(,如下所示:

inputData <- reactive({
inFile <- input$rawFile
if (is.null(inFile)){return(NULL)}
data = read.csv(inFile$datapath, header = T)
df$data = data
return(data)
})

然后,我们观察input$Remove,并像这样更新df$data

observeEvent(input$Remove, {
df$data <- removecolumn(df$data, input$selectColumn)
})

完整代码:

library(shiny)
ui = fluidPage(
fileInput(inputId = "rawFile", label="File"), 
uiOutput("selectColumn"),
actionButton(inputId = "Remove", label = "Remove"),
tableOutput("DFTable")
)
server = function(input, output, session){
# initiate a reactive value
df <- reactiveValues(data = NULL)
removecolumn <- function(df, nameofthecolumn){dplyr::select(df, -(all_of(nameofthecolumn)))}

inputData <- reactive({
inFile <- input$rawFile
if (is.null(inFile)){return(NULL)}
data = read.csv(inFile$datapath, header = T)
df$data = data
return(data)
})

# update column names
output$selectColumn <- renderUI({
shiny::req(inputData())
selectInput(inputId = "selectColumn",
label = "Remove selected sample(s)",
multiple = TRUE,
choices = names(inputData())
)
})

##df$data = reactive(inputData())

observeEvent(input$Remove, {
df$data <- removecolumn(df$data, input$selectColumn)
})

# show result
output$DFTable <- renderTable({
df$data
})

}
shinyApp(ui, server)

相关内容

  • 没有找到相关文章

最新更新