重写反应输出中的变量
Rewrite variable in reactive output
我在反应输出中重写变量时遇到问题。在我的代码中 df1() 是上传的主要数据集。在 modified_df1 中,我根据用户 select 更改了变量的数据类型。我的问题是,在最终数据集中 modified_df1 a 我只得到新列 var (因为我使用 cbind(df1(), var)).有什么方法可以重写以前的专栏,而不用 cbind() 添加新内容吗?我把服务器代码放在这里,谢谢。
df1 <- reactive({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
df
})
colnames <- reactive({ names(df1()) })
observeEvent(df1(), {
updateCheckboxGroupInput(session, "class_var",
label = "Select Columns",
choices = colnames(),
selected = "")
})
## update when selection changes
# storing the selected variable from the variables list table
table.sel <- reactive({
df1()[,which(colnames(df1()) == col.name()[input$class_var,1])]
})
modified_df1 = eventReactive(input$chg_class,{
if( input$choose_class == "Numeric"){
var <- as.numeric(df1()[, input$class_var])
} else if(input$choose_class == "Factor"){ message("get new vars");
var <- as.factor(df1()[, input$class_var])
} else if( input$choose_class == "Character"){
var <- as.character(df1()[, input$class_var])
} else if( input$choose_class == "Date"){
var <- as.Date(df1()[, input$class_var])
}
df2 = cbind(df1(), var)
})
由于我们选择了 多个 列,我们可以使用 dplyr::across
循环遍历它们。例如,如果您想将所选列更改为一个字符:
var <- df1() %>% mutate(across(all_of(input$class_var), ~as.character(.)))
我制作了一个应用程序来更好地说明正在发生的事情。
library(shiny)
library(tidyverse)
write_csv(iris, file = "iris.csv")
# ui ----------------------------------------------------------------------
ui <- fluidPage(
fluidRow(
column(
width = 6,
fileInput(
"file1",
"Upload File",
multiple = FALSE,
accept = NULL,
width = NULL,
buttonLabel = "Browse...",
placeholder = "No file selected"
),
checkboxGroupInput("class_var", "Select Columns", choices = "", selected = ""),
radioButtons("choose_class", "Select Columns", choices = c("Numeric", "Factor", "Character", "Date")),
actionButton("chg_class", "Change Class")
),
column(
width = 6,
tableOutput("table_df")
)
)
)
# server ------------------------------------------------------------------
server <- function(input, output, session) {
df1 <- reactive({
req(input$file1)
read.csv(input$file1$datapath)
})
colnames <- reactive({
names(df1())
})
observeEvent(df1(), {
updateCheckboxGroupInput(session,
"class_var",
label = "Select Columns",
choices = colnames(),
selected = ""
)
})
## update when selection changes
# storing the selected variable from the variables list table
table.sel <- reactive({
df1()[, which(colnames(df1()) == col.name()[input$class_var, 1])]
})
modified_df1 <- eventReactive(input$chg_class, {
if (input$choose_class == "Numeric") {
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.numeric(.)))
} else if (input$choose_class == "Factor") {
message("get new vars")
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.factor(.)))
} else if (input$choose_class == "Character") {
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.character(.)))
} else if (input$choose_class == "Date") {
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.Date(.)))
}
var
})
output$table_df <- renderTable({
modified_df1()
})
}
shinyApp(ui, server)
请注意,如果您尝试将字符向量转换为 Date,则会发生错误。
我在反应输出中重写变量时遇到问题。在我的代码中 df1() 是上传的主要数据集。在 modified_df1 中,我根据用户 select 更改了变量的数据类型。我的问题是,在最终数据集中 modified_df1 a 我只得到新列 var (因为我使用 cbind(df1(), var)).有什么方法可以重写以前的专栏,而不用 cbind() 添加新内容吗?我把服务器代码放在这里,谢谢。
df1 <- reactive({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
df
})
colnames <- reactive({ names(df1()) })
observeEvent(df1(), {
updateCheckboxGroupInput(session, "class_var",
label = "Select Columns",
choices = colnames(),
selected = "")
})
## update when selection changes
# storing the selected variable from the variables list table
table.sel <- reactive({
df1()[,which(colnames(df1()) == col.name()[input$class_var,1])]
})
modified_df1 = eventReactive(input$chg_class,{
if( input$choose_class == "Numeric"){
var <- as.numeric(df1()[, input$class_var])
} else if(input$choose_class == "Factor"){ message("get new vars");
var <- as.factor(df1()[, input$class_var])
} else if( input$choose_class == "Character"){
var <- as.character(df1()[, input$class_var])
} else if( input$choose_class == "Date"){
var <- as.Date(df1()[, input$class_var])
}
df2 = cbind(df1(), var)
})
由于我们选择了 多个 列,我们可以使用 dplyr::across
循环遍历它们。例如,如果您想将所选列更改为一个字符:
var <- df1() %>% mutate(across(all_of(input$class_var), ~as.character(.)))
我制作了一个应用程序来更好地说明正在发生的事情。
library(shiny)
library(tidyverse)
write_csv(iris, file = "iris.csv")
# ui ----------------------------------------------------------------------
ui <- fluidPage(
fluidRow(
column(
width = 6,
fileInput(
"file1",
"Upload File",
multiple = FALSE,
accept = NULL,
width = NULL,
buttonLabel = "Browse...",
placeholder = "No file selected"
),
checkboxGroupInput("class_var", "Select Columns", choices = "", selected = ""),
radioButtons("choose_class", "Select Columns", choices = c("Numeric", "Factor", "Character", "Date")),
actionButton("chg_class", "Change Class")
),
column(
width = 6,
tableOutput("table_df")
)
)
)
# server ------------------------------------------------------------------
server <- function(input, output, session) {
df1 <- reactive({
req(input$file1)
read.csv(input$file1$datapath)
})
colnames <- reactive({
names(df1())
})
observeEvent(df1(), {
updateCheckboxGroupInput(session,
"class_var",
label = "Select Columns",
choices = colnames(),
selected = ""
)
})
## update when selection changes
# storing the selected variable from the variables list table
table.sel <- reactive({
df1()[, which(colnames(df1()) == col.name()[input$class_var, 1])]
})
modified_df1 <- eventReactive(input$chg_class, {
if (input$choose_class == "Numeric") {
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.numeric(.)))
} else if (input$choose_class == "Factor") {
message("get new vars")
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.factor(.)))
} else if (input$choose_class == "Character") {
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.character(.)))
} else if (input$choose_class == "Date") {
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.Date(.)))
}
var
})
output$table_df <- renderTable({
modified_df1()
})
}
shinyApp(ui, server)
请注意,如果您尝试将字符向量转换为 Date,则会发生错误。