I have a shiny app that basically parses a user-uploaded .txt file (see this question I asked regarding the data input and parsing function), and then produces several plots by calling some plotting functions that are located above my shinyServer call in server.R.
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
I can successfully get the plot in tabPanel 1 to show the levels of a factor in the dataframe returned by parseR(), but I'm not sure how to use this to actually update the plot.
My question is, how can I update a plot based on user input?
Here is 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()as the data argument passed to your plotting functions insiderenderPlot. That will re-plot wheneverdata()is updated.