【问题标题】:Update plot based on user input in shiny根据闪亮的用户输入更新绘图
【发布时间】: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() 函数中。完全糊涂了!

标签: r shiny


【解决方案1】:

正如我首先提到的,您不想将 updateInput 保留在 Reactive 函数中。这是因为反应式是惰性评估。最好将它们放置在渴望评估的观察者(observeobserveEvent)中。

然后可以通过input$'inputId'获取User输入的值

我也会将绘图计算放在反应函数中,但这不是必需的。

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)

  })

})

【讨论】:

  • 我已经按照您的建议调整了我的 shinySever 调用:senderPosts(file=input$file1$datapath, user=input$sender),我的绘图函数现在采用 input$sendersenderPosts &lt;- function(file_in='data/testChat.txt', user=NA){ 并将其传递给我的 parseR():data &lt;- parseR(file=file_in,user=user) ... ,然后根据此值过滤数据框,但仍然无法正常工作。这是你的意思吗?
  • 不,它不起作用。当我隔离这两个函数时,它们按预期运行并生成在user='user' 上过滤的图,因此它一定是在shinySever 调用中发生的。有什么想法吗?
  • 你的 ui.r 是什么样子的?
  • 啊——我想通了。感谢您的帮助!
猜你喜欢
  • 2020-10-06
  • 1970-01-01
  • 2021-11-03
  • 2021-08-01
  • 2016-12-09
  • 2020-06-23
  • 2020-12-29
  • 1970-01-01
  • 2021-04-18
相关资源
最近更新 更多