dheeraj kambhampati
dheeraj kambhampati

Reputation: 13

pass text box value to R File

I am trying to pass the entered string value in text box to another R File named test.R on click of button

The passed value ntext is input to another variable in my test.R

Server.R

shinyServer(function(input, output) {



  ntext <- eventReactive(input$goButton, {input$text})

  output$ntext <- renderText({ntext()

    assign('ntext',envir=.GlobalEnv)

    })
})

invisible(readline(prompt="Press [enter] to continue")) source("test.R")

Ui.R

shinyUI(fluidPage(
  titlePanel("Sentiment Analysis"),

  sidebarLayout(
    sidebarPanel(
      helpText("See current opinion around the globe " ),
      textInput("text", "Keyword"),
      actionButton("goButton", "Go!"),



      mainPanel(
           mainPanel(uiOutput('tweets_txt'))



    )
  )
))

Test.R

s<-searchTwitter(ntext(),100,lang="en")

df <- do.call("rbind", lapply(s, as.data.frame))
df$text <- sapply(df$text,function(row) iconv(row, "latin1", "ASCII", sub=""))

#tweets_txt<-sapply(df$text,function(x) x$getText())

write.table(df$text,file="C:/Users/mtech/Desktop/Twitter/EXTRACT/SentiT/output.txt")

tweets_txt<-df$text

I edited the code as per your suggestions but value is not being passed to Test.R also value must be passed only after user entering the textbox thats the reason i included a invisible line to temporarily halt the execution

Please suggest

Upvotes: 1

Views: 1419

Answers (1)

SBista
SBista

Reputation: 7694

As said earlier in the comments, this seems to work

library(shiny)
source("test.R")

ui <- fluidPage(
  titlePanel("Sentiment Analysis"),

  sidebarLayout(
    sidebarPanel(
      helpText("See current opinion around the globe " ),
      textInput("text", "Keyword"),
      actionButton("goButton", "Go!")),

      mainPanel(
        verbatimTextOutput("ntext")
    )
  ))


  server <- shinyServer(function(input, output) {

    ntext <- eventReactive(input$goButton, {input$text})

    output$ntext <- renderText({ntext()
      testfunc(ntext())

      #assign('ntext',envir=.GlobalEnv)

    })
  })


  shinyApp(ui = ui, server = server)

My test.R file is as shown below and it prints the text on the console:

testfunc <- function(texts){

  print(texts)
}

[EDIT]:

As per your edited code I have changed your server and Test.R file in a way that I think it should work.

The server is as follows:

source("Test.R")
server <- shinyServer(function(input, output) {

    ntext <- eventReactive(input$goButton, {input$text})

    output$tweets_txt <- renderText({

      searchInTwitter(ntext())
      ntext()
    })
  })

The Test.R is as follows:

searchInTwitter <- function(ntext){
  s<-searchTwitter(ntext,100,lang="en")

  df <- do.call("rbind", lapply(s, as.data.frame))
  df$text <- sapply(df$text,function(row) iconv(row, "latin1", "ASCII", sub=""))

  #tweets_txt<-sapply(df$text,function(x) x$getText())

  write.table(df$text,file="C:/Users/mtech/Desktop/Twitter/EXTRACT/SentiT/output.txt")

  # tweets_txt<-df$text
}

Upvotes: 1

Related Questions