闪亮:从反应变量中删除列
Shiny: delete columns from a reactive variable
我想使用 shiny 读取 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)
而不是分配给 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)
我想使用 shiny 读取 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)
而不是分配给 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)