在 R Shiny 中重写反应数据集
Rewrite reactive dataset in R Shiny
我对反应数据集有疑问。我使用 eventReactive() 更改反应数据集 df1 中的列数据类型。它工作正常。列数据类型已更改的新数据集为 modified_df1。但是现在,当我想更改另一列中的数据类型时,之前的更改将被重置。这是因为在更改之前我仍然使用 df1 作为主数据框。有什么方法可以在第二次数据类型更改之前重写 df1?
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 = "")
})
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") {
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(.)))
}else if (input$choose_class == "Integer") {
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.integer(.)))
}
})
不确定我的回答是否合适,因为你没有提供很多信息。
我只会使用一个反应性数据帧 df1
,但不会使用反应性导体,而是使用反应性值:
df1 <- reactiveVal(original_dataframe)
然后我会和观察者一起修改它:
observeEvent(input$chg_class, {
if (input$choose_class == "Numeric") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.numeric(.))))
} else if (input$choose_class == "Factor") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.factor(.))))
} else if (input$choose_class == "Character") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.character(.))))
} else if (input$choose_class == "Date") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.Date(.))))
}else if (input$choose_class == "Integer") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.integer(.))))
}
})
编辑
df1 <- reactiveVal()
observeEvent(input$file1, {
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
df1(df)
})
observeEvent(df1(), {
updateCheckboxGroupInput(session, "class_var",
label = "Select Columns",
choices = names(df1()),
selected = "")
})
observeEvent(input$chg_class, {
if (input$choose_class == "Numeric") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.numeric(.))))
} else if (input$choose_class == "Factor") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.factor(.))))
} else if (input$choose_class == "Character") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.character(.))))
} else if (input$choose_class == "Date") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.Date(.))))
}else if (input$choose_class == "Integer") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.integer(.))))
}
})
我对反应数据集有疑问。我使用 eventReactive() 更改反应数据集 df1 中的列数据类型。它工作正常。列数据类型已更改的新数据集为 modified_df1。但是现在,当我想更改另一列中的数据类型时,之前的更改将被重置。这是因为在更改之前我仍然使用 df1 作为主数据框。有什么方法可以在第二次数据类型更改之前重写 df1?
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 = "")
})
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") {
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(.)))
}else if (input$choose_class == "Integer") {
var <- df1() %>% mutate(across(all_of(input$class_var), ~ as.integer(.)))
}
})
不确定我的回答是否合适,因为你没有提供很多信息。
我只会使用一个反应性数据帧 df1
,但不会使用反应性导体,而是使用反应性值:
df1 <- reactiveVal(original_dataframe)
然后我会和观察者一起修改它:
observeEvent(input$chg_class, {
if (input$choose_class == "Numeric") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.numeric(.))))
} else if (input$choose_class == "Factor") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.factor(.))))
} else if (input$choose_class == "Character") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.character(.))))
} else if (input$choose_class == "Date") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.Date(.))))
}else if (input$choose_class == "Integer") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.integer(.))))
}
})
编辑
df1 <- reactiveVal()
observeEvent(input$file1, {
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
df1(df)
})
observeEvent(df1(), {
updateCheckboxGroupInput(session, "class_var",
label = "Select Columns",
choices = names(df1()),
selected = "")
})
observeEvent(input$chg_class, {
if (input$choose_class == "Numeric") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.numeric(.))))
} else if (input$choose_class == "Factor") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.factor(.))))
} else if (input$choose_class == "Character") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.character(.))))
} else if (input$choose_class == "Date") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.Date(.))))
}else if (input$choose_class == "Integer") {
df1(df1() %>% mutate(across(all_of(input$class_var), ~ as.integer(.))))
}
})