2013-10-09 5 views
2

여기에서 데이터 프레임에 새 열을 추가하고 있습니다.
아래 코드가 잘 작동합니다. 새 열을 추가 한 후 "변수 이름"및 "수식"안의 텍스트는 비어 있어야합니다.제출 후 일반 텍스트 입력

도와주세요.

ui.R

library(shiny) 
shinyUI(pageWithSidebar(
    headerPanel("", ""), 
    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("addvar","Apply"))  
    ) 
    ), 

    mainPanel(
    tabsetPanel(
     tabPanel(tableOutput('contents')) 
    ) 
) 

)) 

server.R

library(shiny) 
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$addcol!=0&&input$addvar!=0){ 
     isolate({ 
     df<-dataset() 
     df$Var1<-eval(parse(text=input$newformula), df) 
     df<-rename(df, c(Var1=input$newvar)) 
     df 
     }) 
    } 
    else 
    { 
     dataset() 
    } 
    }) 

    output$contents<-renderTable({ 
    if (is.null(input$file)) { return() }        
    alterdata() 
    }) 

}) 

답변

2

당신은 updateTextInput().를 사용하여 여기에 help on that function 있다고 할 수 있습니다. 두 개의 선이 alterdata() 반응 기능에 추가 된 것을

주 Server.R 수정

:

다음은 업데이트 server.R는 다음과 같이 표시됩니다. 내가 rename 호출 할 수 있도록 plyr을 포함했다

library(shiny) 
library(plyr) 
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$addcol!=0&&input$addvar!=0){ 
     isolate({ 
     df<-dataset() 
     df$Var1<-eval(parse(text=input$newformula), df) 
     df<-rename(df, c(Var1=input$newvar)) 

     #add these two lines 
     updateTextInput(session, "newvar", value = " ")  
     updateTextInput(session, "newformula", value = " ")  

     df   
     }) 
    } 
    else 
    { 
     dataset() 
    } 
    }) 


    output$contents<-renderTable({ 
    if (is.null(input$file)) { return() }        
    alterdata()  
    }) 

}) 

참고.

+0

감사 ... 작동합니다. clearText 리 액션 기능을 추가했는데 필요하지 않다고 생각합니다. 그리고 또한 plyr 대신 재구성을 추가했는데 작동 중입니다. – Punith

관련 문제