3
반짝이는 웹 응용 프로그램을 사용하여 데이터 프레임에 대한 새 열을 만들려고합니다.
이 앱의 수식 옵션에 수식을 입력하면 해당 수식을 받아야하며 데이터 프레임에 새로운 열을 만들어야합니다. 내가 attach(), subset() 함수를 사용해 보았는데 ...
도와 주시겠습니까?
데이터 프레임에 새 열을 추가하는 수식 전달
UI :
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Data Frame", "Data Frame"),
sidebarPanel(
wellPanel(
fileInput('file', 'Select csv file', accept=c('text/csv')),
checkboxInput('header', 'Header', TRUE),
gsub("label class=\"radio\"", "label class=\"radio inline\"",
radioButtons('sep', 'Separator', c(Comma=',', Semicolon=';', Tab='\t')))
),
wellPanel(
checkboxInput('addcol', 'Create New Variable', FALSE),
conditionalPanel(condition="input.addcol!=0",
textInput('newvar', "Variable name",""),
textInput('newformula', "Formula",""),
actionButton("apply","Apply Changes")
)
)
),
mainPanel(
tableOutput('contents')
)
))
서버 :
library(shiny)
library(stats)
library(reshape)
require(utils)
shinyServer(function(input,output,session){
dataset = reactive({
inFile<-input$file
if(is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep)
})
alterdata = reactive({
if(input$apply==0){
dataset<-transform(dataset(), new='')
dataset <- rename(dataset, c(new=input$newvar))
dataset
}
else
{
attach(dataset())
dataset<-dataset()
dataset$new<-cat(input$newformula, "\n")
detach(dataset())
dataset <- rename(dataset, c(new=input$newvar))
dataset
}
#dataset
})
data = reactive({
if(input$addcol!=0)
{
alterdata()
}
else
{
dataset()
}
})
output$contents<-renderTable({
if (is.null(input$file)) { return() }
data()
})
})
})