【发布时间】:2018-04-30 05:11:04
【问题描述】:
我有一个闪亮的应用程序,它基本上解析用户上传的.txt 文件(请参阅this question 我询问了有关数据输入和解析功能的问题),然后通过调用位于我上面的一些绘图函数来生成多个绘图shinyServer 致电server.R。
我的 server.R 脚本中调用的函数是:
parseR() # Returns a dataframe (which is the result of parsing
the user input entered in `fileInput) ([see here][1] for details)
senderPosts() # Returns a plot that is shown in tabPanel 1
wordFreq() # Returns a plot that is shown in tabPanel 2
chatCloud() # Returns a plot that is shown in tabPanel 3
我可以成功获取 tabPanel 1 中的绘图,以显示 parseR() 返回的数据框中某个因子的级别,但我不确定如何使用它来实际更新绘图.
我的问题是,如何根据用户输入更新绘图?
这里是server.R:
shinyServer(function(input, output, session) {
data <- reactive({
req(input$file1)
inFile <- input$file1
df <- parseR(file=inFile$datapath) # Call my parser function
updateSelectInput(session, inputId = 'sender', label = 'Sender',
choices = levels(df$sender), selected = levels(df$sender))
return(df)
})
# Main page
output$contents <- renderTable({
head(data(), 25)
})
# tabPanel 1
output$postCount <-renderPlot({
senderPosts(file=input$file1$datapath)
})
# tabPanel 2
output$wordCount <-renderPlot({
wordFreq(file=input$file1$datapath)
})
# tabPanel 3
output$chatCloud <-renderPlot({
chatCloud(file=input$file1$datapath)
})
})
ui.R
library(shiny)
suppressMessages(library("wordcloud"))
shinyUI(fluidPage(
titlePanel("File plotter"),
tabsetPanel(
tabPanel("Upload File",
titlePanel("Upload your file"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Select your file',
accept='.txt'
),
tags$br()
),
mainPanel(
tableOutput('contents'),
plotOutput('messageCount')
)
)
),
# tabPanel 1
tabPanel("Post Count",
pageWithSidebar(
headerPanel('Number of posts per user'),
sidebarPanel(
# "Empty inputs" - they will be updated after the data is uploaded
selectInput('sender', 'Sender', "")
),
mainPanel(
plotOutput('postCount')
)
)
),
# tabPanel 2
tabPanel("Word Frequency",
pageWithSidebar(
headerPanel('Most commonly used words'),
sidebarPanel(
# "Empty inputs" - they will be updated after the data is uploaded
selectInput('word', 'Sender', "")
),
mainPanel(
plotOutput('wordCount')
)
)
),
# tabPanel 3
tabPanel("Chat Cloud",
pageWithSidebar(
headerPanel('Most used words'),
sidebarPanel(
# "Empty inputs" - they will be updated after the data is uploaded
selectInput('cloud', 'Sender', "")
),
mainPanel(
plotOutput('chatCloud')
)
)
)
)
)
)
【问题讨论】:
-
使用反应值
data()作为数据参数传递给renderPlot中的绘图函数。每当data()更新时,它将重新绘制。 -
渲染函数通常是反应式的。这意味着每当输入变量更改其中的值时。它们应该会自动重新计算
-
另外,您不希望在反应函数中包含更新输入函数,最好将其放在单独的观察(事件)函数中。
-
@BertilNestorius - 我不确定我是否理解。您能否发布说明作为答案(或说明的链接)?
-
@Nate - 我传递给绘图函数的数据框由 parseR() 生成。我不确定如何将我的选择传播到 parseR() 函数中。完全糊涂了!