【发布时间】:2018-03-23 07:48:32
【问题描述】:
我有一个 Shiny 应用程序,它允许用户上传 CSV 来进行情绪分析。
目标:
我想使用 Shiny 上传 CSV,然后使用单独的函数 (CapSent) 进行分析并输出结果。
基本上,我试图将用户上传的“df”传递给 Shiny 的函数“CapSent”(位于 global.R 中)。 CapSent 使用自定义词典进行情绪分析。
到目前为止我的代码:
到目前为止我有:
用户界面:
library(shiny)
source('global.R')
ui <- fluidPage(
sidebarPanel(
# Input: Select a file ----
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv"))
))
服务器:
server <- function(input, output) {
output$contents <- renderTable({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
CapSent(0.1, df) # 0.1 represents a threashold, df is the data
})
}
shinyApp(ui, server)
函数.R:
CapSent <- function(0.1, df){
newdf<-data.frame(df,stringsAsFactors = FALSE)
#....Do some sentiment analysis here on newdf
#....Then export the sentiment analysis results
write.csv(newdf,"myResults.csv")
}
问题
使用上面的代码,我收到错误“编码错误
当我手动将“df”添加到全局环境(使用 readr)时,“CapSent”有效,但我希望用户上传自己的数据进行分析。因此问题是:
有没有办法将 df 从 Shiny 传递给 Global Environment?
任何建议将不胜感激。
【问题讨论】: