r - 将数据框读入与 Shiny 不同的函数中

  • 本文关键字:Shiny 函数 数据 r shiny
  • 更新时间 :
  • 英文 :


我有一个闪亮的应用程序,允许用户上传CSV进行情绪分析。

目标:

我想使用 Shiny 上传 CSV,然后使用单独的函数 (CapSent) 进行分析并输出结果。

基本上,我正在尝试将用户上传的"df"传递到函数"CapSent"(驻留在全局中。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)

Functions.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"添加到全局环境(使用读取器)时,"CapSent"有效,但我希望用户上传自己的数据进行分析。因此问题来了:

有没有办法将 df 从 Shiny 传递给全球环境?

任何建议将不胜感激。

试试这个:

ui.R

library(shiny)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input:  ----
fileInput("file1", "Choose CSV File",
multiple = TRUE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
actionButton("button", "Apply function/download df"),
hr(),
uiOutput("downloadButton")
),
# Main panel for displaying outputs ----
mainPanel(
h2("ORIGINAL DATA FRAME"),
DT::dataTableOutput("contents"),
br(),
uiOutput("modify")

)
)
)

server.R

server <- function(input, output) {
temp_df <- reactiveValues(df_data = NULL)
temp_df2 <- reactiveValues(df_data = NULL)

output$contents <- DT::renderDataTable({
req(input$file1)
temp_df$df_data <- read.csv(input$file1$datapath, sep = ";")
temp_df$df_data
}, options = (list(pageLength = 5, scrollX = TRUE)))
output$contents2 <- DT::renderDataTable({
temp_df2$df_data

}, options = (list(pageLength = 5, scrollX = TRUE)))
observeEvent(input$button,{
if(!is.null(temp_df$df_data)){
temp_df2$df_data <- CapSent(temp = 0.7, temp_df$df_data)
output$modify <- renderUI({
tagList(
h2("MODIFY DATA FRAME"),
DT::dataTableOutput("contents2")
)
})
output$downloadButton <- renderUI({
downloadButton("downloadData", "Download")
})
}else{
showNotification("No data was upload")
}
})

output$downloadData <- downloadHandler(
filename = function() { 
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(temp_df2$df_data, file)
})
}

因为我不知道我CapSent在原始数据框中添加新列的函数CapSent最终用途;

global.R

CapSent <- function(temp = 0.1, df){
newdf <- df
newdf$New_Col <- temp
return(newdf)
#....Do some sentiment analysis here on newdf
#....Then export the sentiment analysis results
#write.csv(newdf,"myResults.csv")
}

如果你想创建一个全局函数/变量,只需创建一个全局函数/变量。R 这将允许您在 ui 上的任何地方使用该函数/变量。R 或 server.R.

这是了解更多信息的链接:https://shiny.rstudio.com/articles/scoping.html

编辑:如果要显示CSV,首先需要制作一个选项卡面板,然后使用csv数据制作表格,例如:

使用DT包install.packages("DT")是一个制作动态表的包。

`output$yourtabpanelid = DT::renderDataTable({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
return(df)
})`

然后不要做函数。R,只需将函数放在函数下方,并将read.csv(......)放在函数之前,即可在服务器的所有函数中使用它。R喜欢:

`df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote

) 服务器 <- 函数(输入、输出、会话) {'

你可以退出df<-阅读.csv....函数 DT 但保留返回 (df)

最新更新