2013-10-09 6 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.

Вот что обновленный server.R будет выглядеть следующим образом:

Modified Server.R

Обратите внимание, что две линии, которые были добавлены к alterdata() реактивной функции.

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()  
    }) 

}) 

Обратите внимание, что я должен был включать в себя plyr так, что rename можно ссылаться.

+0

Спасибо ... Это сработало. Вы добавили реактивную функцию clearText, я думаю, что это не требуется. А также я добавил reshape вместо plyr, он работает .. – Punith

Смежные вопросы