简体   繁体   English

根据闪亮的用户输入更新图

[英]Update plot based on user input in shiny

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 . 我有一个闪亮的应用程序,基本上解析用户上传.txt文件(见这个问题 ,我问有关数据输入和分析功能),然后通过调用位于上述我的一些绘图函数产生几个地块shinyServer呼叫server.R

The functions called in my server.R script are: 我的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

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. 我可以在tabPanel 1中成功获取该图,以显示parseR()返回的数据帧中某个因子的水平,但是我不确定如何使用它来实际更新该图。

My question is, how can I update a plot based on user input? 我的问题是, 如何根据用户输入更新图?

Here is server.R : 这是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 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')
               )
             )
    )



  )
)
)

as I mentioned first you don't want to keep the updateInput in the Reactive function. 正如我首先提到的,您不想将updateInput保留在Reactive函数中。 This is because reactives are lazy evaluation. 这是因为反应物是懒惰的评估。 They are better placed within a observer ( observe or observeEvent ) which are eager evaluation. 最好将它们放在渴望评估的观察者( observeobserveEvent )中。

You can then get the value of the User input through input$'inputId' 然后,您可以通过input$'inputId'获取用户输入的值

I would also place the plot calculations in a reactive function but that is not necessary. 我还将绘图计算放在反应函数中,但这不是必需的。

shinyServer(function(input, output, session) {

  data <- reactive({ 
    req(input$file1)

    inFile <- input$file1 

    df <- parseR(file=inFile$datapath) # Call my parser function 

    return(df)
  })
  observe({
     df = data()
     updateSelectInput(session, inputId = 'sender', label = 'Sender',
                      choices = levels(df$sender), selected = levels(df$sender))
  })
  # Main page
  output$contents <- renderTable({
    head(data(), 25)
  })

  # tabPanel 1
  output$postCount <-renderPlot({
    senderPosts(file=input$file1$datapath,newParamter = input$sender)

  })

  # tabPanel 2
  output$wordCount <-renderPlot({
    wordFreq(file=input$file1$datapath)

    })

  # tabPanel 3
  output$chatCloud <-renderPlot({
    chatCloud(file=input$file1$datapath)

  })

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM