R 允许用户在应用程序中向数据集添加新变量
大家好,闪亮的用户们 我希望我的用户能够将新变量添加到主数据框架中。用户将使用textInput键入定义。然后,我们将使用server.R将其添加到数据帧中。这是我的密码。我不能让它工作。任何帮助都将不胜感激。谢谢大家! 原始数据:R 允许用户在应用程序中向数据集添加新变量,r,shiny,R,Shiny,大家好,闪亮的用户们 我希望我的用户能够将新变量添加到主数据框架中。用户将使用textInput键入定义。然后,我们将使用server.R将其添加到数据帧中。这是我的密码。我不能让它工作。任何帮助都将不胜感激。谢谢大家! 原始数据: colA <- c('1','2','3','3','2') colB <- c('1','1','3','3','2') colC <- c('14','12','33','33','26') colD <- c('Value','Main
colA <- c('1','2','3','3','2')
colB <- c('1','1','3','3','2')
colC <- c('14','12','33','33','26')
colD <- c('Value','Mainstream','Value','Premium','Premium')
colE <- c(1,2,3,4,5)
rawdata <- as.data.frame(cbind(colA,colB, colC, colD, colE))
View(rawdata)
colA使用eval(解析(text=input$addVar))
应该可以工作
您还可以为textInput()
添加默认文本,以使textInput()
的使用(非常规但有趣)更加清晰
textInput("addVar", "New attribute definition",
"curr$df$Value <- ifelse(curr$df$colD == 'Value', 1, 0)")
textInput(“addVar”,“新属性定义”,
“curr$df$Value虽然对这个问题有一个公认的答案,但值得注意的是,eval(parse())
如果不小心使用,会有很大的风险(例如:按原样评估传入的文本。请参阅上的SO讨论)
规避这些风险的一种方法是对传入的文本进行变异(双关语,意料之外),然后对变异后的文本进行eval(parse())
评估。这种方法可以得到预期的结果或错误,但几乎不会像运行长代码那样发生灾难,比如说,T不是eval(parse(text=)
由于其范围是全局性的而存在风险?一个恶作剧或错误可能会影响当前文件夹中的所有文件。例如,您可以添加文本mt3 sry,我无法理解,您指的是哪些文件?我还建议使用1。('which files'))在执行闪亮应用程序的命令行中可用的文件;例如(感谢@flodel),如果用户在文本输入中输入rm(list=ls())
以创建新变量,它将删除所有文件。2.我没有劝阻使用(续)…是主观的。我想您指的是没有规定风险的解决方案;请参见下面的备选答案。
function(input, output, session) {
curr <- reactiveValues()
curr$df <- rawdata
observeEvent(input$addButton, {
eval(parse(text=input$filter))
})
}
curr$df$Value <- ifelse(curr$df$colD == 'Value', 1, 0)
curr$df$Premium <- ifelse(curr$df$colD == 'Premium', 1, 0)
textInput("addVar", "New attribute definition",
"curr$df$Value <- ifelse(curr$df$colD == 'Value', 1, 0)")
colA <- c('1','2','3','3','2')
colB <- c('1','1','3','3','2')
colC <- c('14','12','33','33','26')
colD <- c('Value','Mainstream','Value','Premium','Premium')
colE <- c(1,2,3,4,5)
rawdata <- as.data.frame(cbind(colA, colB, colC, colD, colE))
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("addVar", "New attribute definition", "curr$df$Value <- ifelse(curr$df$colD == 'Value', 1, 0)"),
helpText("Note: Type the attribute definition using R code."),
helpText("For example:"),
helpText("data$Value <- ifelse (data$price_tiers_new == 'Value', 1, 0)"),
br(),
actionButton("addButton", strong("Add!")),
width = 3
),
mainPanel(
verticalLayout(
br(),
verbatimTextOutput("txt")
#Will display histogram of the newly added variables
)
)
)
)
server <- function(input, output, session) {
output$txt <- renderPrint(curr$df)
curr <- reactiveValues()
curr$df <- rawdata
observeEvent(input$addButton, {
eval(parse(text=input$addVar))
})
}
shinyApp(ui, server)
input_vector <- list()
input_vector[[1]] <- "ifelse(am == 1, 'wohoo', 'io')"
input_vector[[2]] <- "vs == 1"
input_vector[[3]] <- "cyl >= 6"
input_vector[[4]] <- "0"
name_vector <- list()
name_vector[[1]] <- "automatic_"
name_vector[[2]] <- "VS_Equals_1_"
name_vector[[3]] <- "HighCylinder_"
name_vector[[4]] <- "Blank_"
new_var_count <- 1:4
mutated_data <- reactive({
mt %>%
{eval(parse(text=
paste0("mutate_(.,",
paste0(name_vector, as.character(new_var_count),"= input_vector[[",
as.character(new_var_count), "]]", collapse= " , "),
")"
)
))}
})