R 将数据帧读入单独的函数中
我有一个闪亮的应用程序,允许用户上传CSV进行情绪分析 目标是: 我想使用Shiny上传CSV,然后使用单独的函数(CapSent)进行分析并输出结果 基本上,我试图将用户上传的“df”从Shiny传递到函数“CapSent”(驻留在global.R中)中。CapSent使用定制的词汇词典进行情感分析 到目前为止,我的代码是: 到目前为止,我已经: 用户界面:R 将数据帧读入单独的函数中,r,shiny,R,Shiny,我有一个闪亮的应用程序,允许用户上传CSV进行情绪分析 目标是: 我想使用Shiny上传CSV,然后使用单独的函数(CapSent)进行分析并输出结果 基本上,我试图将用户上传的“df”从Shiny传递到函数“CapSent”(驻留在global.R中)中。CapSent使用定制的词汇词典进行情感分析 到目前为止,我的代码是: 到目前为止,我已经: 用户界面: 库(闪亮) 来源('global.R') ui如果您想创建一个全局函数/变量,只需创建一个global.R,它将允许您在ui.R或ser
库(闪亮)
来源('global.R')
ui如果您想创建一个全局函数/变量,只需创建一个global.R,它将允许您在ui.R或server.R上的任何地方使用该函数/变量
这是了解更多信息的链接:
编辑:如果要显示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)
})`
`output$yourtabpanelid=DT::renderDataTable({
请求(输入$file1)
df试试这个:
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")
)
)
)
由于我不知道是哪个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")
}
CapSent感谢您的回复,但是该链接并没有真正解释代码不会触发的原因。我已将脚本更改为global.R,但仍然没有返回任何内容作为输出?您试图显示什么,以及如何处理myFunction()告诉我,如果你对这个答案有什么问题,或者你需要另一个结果,我会再次尝试解决它。汉克斯,我已经尝试澄清我上面的原始问题。你会看到myFunction()现在是CapSent()在上传的csv上进行情绪分析。正如你可能看到的,我只是从Shiny开始,所以可能没有正确设置。感谢你在这方面花时间。
`output$yourtabpanelid = DT::renderDataTable({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
return(df)
})`
`df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote
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 <- 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 <- 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")
}