如何解决Shiny无法从上传的csv中基于selectInput读取列 问题:准备工作:库和示例数据 UI 服务器保险丝警告:
问题:
我正在尝试构建一个闪亮的应用程序,该应用程序根据用户上传的csv中的用户指定列来绘制n-gram的频率。此外,还添加了一个功能,可以根据用户指定的日期列绘制随时间变化的兴趣点。
该应用在本地正常运行,显示警告,但发布后无法正常工作。请参见以下示例,以获取可重复的示例。
准备工作:库和示例数据
p = [[0,1,1],[0,2,3,3],4]]
array = [0.6,0.5,0.9]
for i in range(len(p)):
if p[i][0] == 0:
p[i][0] = array[i] # use random() if you want to replace with a random number
print(p)
UI
# Load R packages
library(shiny)
library(tidyverse)
library(shinythemes)
library(lubridate)
library(tidytext)
library(textdata)
# Creating a example csv file for upload
Sample_csv <-
data.frame(text = janeaustenr::emma,id = 1:length(janeaustenr::emma),date = sample(seq(as.Date('1900/01/01'),as.Date('1920/01/01'),by="day"),replace = T,length(janeaustenr::emma)))
write.csv(Sample_csv,"Sample_csv.csv",row.names = F)
服务器
ui <- fluidPage(theme = shinytheme("united"),titlePanel("Text glancer"),sidebarLayout(
sidebarPanel(
# Input: Select a file ----
fileInput("csv_file","Feed csv here",multiple = FALSE,accept = c(".csv")),#Conditional panel
conditionalPanel(
# use a server side condition
condition = "output$fileUploaded",# Input: Select ----
uIoUtput("text_select"),# Input: Select ----
uIoUtput("date_select"),# Input: Simple integer interval ----
sliderInput("top_frequency","Top n ngrams to be plotted:",min = 5,max = 20,value = 10),# Input: Select ----
selectInput("ngrams","Ngrams of your choice:",c("Single word" = 1,"Bigram" = 2,"Trigram" = 3)
)
),# Submit bottom
submitButton("Update View",icon("refresh"))
),# sidebarPanel
mainPanel(
tabsetPanel(
tabPanel(h2("Most frequenlty used n-grams"),plotOutput("frequency_plot",height = 900,width = 1200)),tabPanel(h2("Sentiment of the months"),plotOutput("sentiment_plot",width = 1200))
)
)
)
)
保险丝
server <- function(input,output,session) {
# create reactive version of the dataset (a data.frame object)
LOAD_DATA <- reactive({
infile <- input$csv_file
if (is.null(infile))
{return(NULL)}
{read_csv(infile$datapath)}
})
# inform conditionalPanel wheter dropdowns sohould be hidden
output$fileUploaded <- reactive({
return(!is.null(LOAD_DATA()))
})
outputoptions(output,'fileUploaded',suspendWhenHidden=FALSE)
## update 'column' selectors
output$text_select <- renderUI({
if(is.null(LOAD_DATA()))
{return(NULL)}
else
selectInput("text_col","Select the text column:",colnames(LOAD_DATA()))
})
output$date_select <- renderUI({
if(is.null(LOAD_DATA()))
{return(NULL)}
else
selectInput("date_col","Select the date column (ymd):",colnames(LOAD_DATA()))
})
# Create reactive parameters
TOP_FREQUENCY <- reactive({
input$top_frequency
})
N_GRAMS <- reactive({
as.numeric(as.character(input$ngrams))
})
# Output frequency of ngrams
output$frequency_plot <- renderPlot( {
if(is.null(LOAD_DATA()))
{return(NULL)}
else{
WORK_DATA <- LOAD_DATA()[,c(input$text_col,input$date_col)]
names(WORK_DATA) <- c("TEXTS","DATES")
CSV_DOC_N_Grams <-
WORK_DATA %>%
# LOAD_DATA() %>%
# select(TEXTS = TEXT_COL(),DATES = DATE_COL()) %>%
mutate(TEXTS = gsub("http.*"," ",TEXTS)) %>%
# mutate(text = gsub("\\@.* |\\@.* .|\\@.*,",text)) %>%
unnest_tokens(words,TEXTS,token = "ngrams",n = N_GRAMS()) %>%
select(words) %>%
filter(str_detect(words,"[a-zA-Z]")) %>%
separate(words,c("word1","word2","word3"),sep = " ",remove = F) %>%
filter(! word1 %in% stop_words$word &
! word2 %in% stop_words$word&
! word3 %in% stop_words$word)
#Counting ngrams
CSV_DOC_N_Gramss_Count <-
CSV_DOC_N_Grams %>%
count(words,sort=T) %>%
select(N_Gram_Text = words,N_Gram_Count = n)
#Plotting ngram frequency
CSV_DOC_N_Gramss_Count_freq <-
CSV_DOC_N_Gramss_Count %>%
mutate(N_Gram_Text = fct_reorder(N_Gram_Text,N_Gram_Count)) %>%
top_n(TOP_FREQUENCY(),N_Gram_Count) %>%
ggplot(aes(x = N_Gram_Text,y = N_Gram_Count,fill = N_Gram_Count)) +
geom_col()+
coord_flip() +
scale_fill_gradient2()+
labs(title = paste0("Top ",TOP_FREQUENCY()," ngrams used in csv doc"),x = "ngrams",y = "frequency") +
theme_bw()+
theme(legend.position = "none",axis.text.x = element_text(face='bold',size=12),axis.text.y = element_text(face='bold',axis.title.x = element_text(face='bold',size=18),axis.title.y = element_blank())
print(CSV_DOC_N_Gramss_Count_freq)
}
})
output$sentiment_plot <- renderPlot( {
if(is.null(LOAD_DATA())){return(NULL)}
else{
WORK_DATA <- LOAD_DATA()[,"DATES")
tk_afinn <-
WORK_DATA %>%
mutate(TEXTS = gsub("http.*",TEXTS)) %>%
unnest_tokens(word,TEXTS) %>%
filter(! word %in% stop_words$word) %>%
filter(str_detect(word,"[a-zA-Z]")) %>%
filter(! DATES %in% NA) %>%
inner_join(get_sentiments("afinn")) %>%
mutate(YEAR_Month = ymd(paste(year(DATES),month(DATES),"1",sep="-"))) %>%
group_by(index = YEAR_Month) %>%
summarise(sentiment = sum(value))
tk_afinn_plot <-
tk_afinn %>%
ggplot(aes(x = index,y = sentiment)) +
geom_line()+
labs(x = "date (year-month)",y = "sentiment of the month") +
theme_bw()+
theme(legend.position = "none",axis.title.y = element_blank())
print(tk_afinn_plot)
}
})
}
警告:
加载csv文件后,本地应用会报告:
“ shinyApp(ui = ui,server = server)
输入mutate()
有问题。
找不到对象“ TEXTS”
输入TEXTS
是TEXTS
。”
指定文本列和日期列后,两个选项卡均显示图。但是,将其发布到Shinyapp.io之后,它会报告错误并且无法运行。
有人可以帮助解决这个问题吗?我已经咨询了其他线程,包括this> https://stackoverflow.com/questions/47248534/dynamically-list-choices-for-selectinput-from-a-user-selected-column,但仍然没有运气。
任何见识将不胜感激!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。