我有一个闪亮的应用程序,基本上解析用户上传的 .txt
文件(请参阅this question我询问有关数据输入和解析功能),然后通过调用 server.R
中 shinyServer
调用上方的一些绘图函数生成几个图 .
The functions called in my server.R script are:
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()返回的数据帧中的一个因子的级别,但我不确定如何使用它实际上 update 该绘图 .
我的问题是, how can I update a plot based on user input?
这是 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')
)
)
)
)
)
)
1 回答
正如我先提到的那样,您不希望将updateInput保留在Reactive函数中 . 这是因为反应性是懒惰的评价 . 它们更适合作为热切评价的观察者(
observe
或observeEvent
) .然后,您可以通过
input$'inputId'
获取用户输入的值我还将绘图计算放在反应函数中,但这不是必需的 .