Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear text input after submit

Here I am adding new column to my data frame.
The below code is working good. After added a new column, the texts inside the "Variable name" and "Formula" should be empty.

Could you please help me.

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

})
like image 313
Punith Avatar asked Feb 18 '26 12:02

Punith


1 Answers

You can do that by using updateTextInput(). Here's the help on that function.

Here's what the updated server.R will look like:

Modified Server.R

Note that two lines have been added to the alterdata() reactive function.

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

})

Note that I had to include plyr so that rename could be invoked.

like image 121
Ram Narasimhan Avatar answered Feb 21 '26 00:02

Ram Narasimhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!