안녕하세요 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','Mainstream','Value','Premium','Premium')
colE <- c(1,2,3,4,5)
rawdata <- as.data.frame(cbind(colA,colB, colC, colD, colE))
View(rawdata)
ui.R :
fluidPage(
sidebarLayout(
sidebarPanel(
textInput("addVar", "New attribute definition"),
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()
#Will display histogram of the newly added variables
)
)
)
)
서버 .R :
function(input, output, session) {
curr <- reactiveValues()
curr$df <- rawdata
observeEvent(input$addButton, {
eval(parse(text=input$filter))
})
}
예를 들어, 다음 두 가지 새로운 변수 정의를 시도해보십시오. 첫 번째 정의를 추가하면 rawdata에 하나의 추가 열 (Value)이 있습니다. 두 번째 정의를 추가하면 rawdata에 두 개의 추가 열 (Value 및 Premium)이 있습니다.
curr$df$Value <- ifelse(curr$df$colD == 'Value', 1, 0)
curr$df$Premium <- ifelse(curr$df$colD == 'Premium', 1, 0)
사용 eval(parse(text=input$addVar))
이 작동합니다.
textInput()
(비 전통적이지만 흥미로운) 사용을 textInput()
더 명확 하게 만들기 위해에 대한 기본 텍스트를 추가 할 수도 있습니다 .
textInput("addVar", "New attribute definition",
"curr$df$Value <- ifelse(curr$df$colD == 'Value', 1, 0)")
전체 앱 (결과 확인을위한 textOutput 포함)은 다음과 같습니다.
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)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다