【问题标题】:pass text box value to R File将文本框值传递给 R 文件
【发布时间】:2017-08-14 01:22:54
【问题描述】:

我试图通过单击按钮将文本框中输入的字符串值传递给另一个名为 test.R 的 R 文件

传递的值 ntext 输入到我的测试中的另一个变量。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")) 来源(“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

我根据您的建议编辑了代码,但没有将值传递给 Test.R 也必须仅在用户输入文本框后才传递值,这就是我包含一条不可见的线以暂时停止执行的原因

请推荐

【问题讨论】:

  • 您可以在您的应用程序中获取文件test.R 并在该文件中编写一个函数,该函数将ntext 作为输入。
  • 试过了,但ntext本身不能被识别为R中的变量

标签: r shiny


【解决方案1】:

正如前面在 cmets 中所说,这似乎有效

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)

我的 test.R 文件如下所示,它在控制台上打印文本:

testfunc <- function(texts){

  print(texts)
}

[编辑]:

根据您编辑的代码,我已经以我认为应该可以工作的方式更改了您的服务器和 Test.R 文件。

服务器如下:

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

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

    output$tweets_txt <- renderText({

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

Test.R如下:

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
}

【讨论】:

  • 它工作正常 tq 但问题是来自文本框的关键字应该在这里 searchTwitter(searchterm,100,lang="en") 代替 searchterm 但我不能跨度>
  • 我需要将文本框中的值转移到 'searchTwitter(searchterm,100,lang="en")' 中的 searchterm 是我想要实现的上面的代码仅用作函数,而不是存储文本框中的内容。
  • 对于要在搜索功能中传输的 searchterm,您可以将其传递为 'searchTwitter(ntext(),100,lang="en")'
  • 我已经编辑了使用上述代码测试的有问题的代码 nchar 中的错误:找不到函数“ntext”堆栈跟踪(最内层优先)
  • 一旦用户在文本框中输入并单击按钮,我的 Test.R 应该运行,因为 source(test.R) 写在服务器 test.R 之上,即在没有任何输入的情况下运行,即 nothing首次运行时出现在 tnext 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-29
  • 2016-02-07
  • 2016-04-20
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 2013-10-31
相关资源
最近更新 更多