Вопрос

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

})
Это было полезно?

Решение

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.

Другие советы

You can easily achieve this by Deans shinyjs package here.https://rdrr.io/cran/shinyjs/man/reset.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top