簡體   English   中英

在沒有ObserveEvent()閃亮的情況下更改輸入時,如何使更改反映在許多地方

[英]How to make changes reflect in many places when input is changed without ObserveEvent() in shiny

我有一個輸入變量input$shop_id 使用以下命令在服務器功能中獲取數據:

 observeEvent(input$shop_id,{id<<-input$shop_id})
`Data=dbGetQuery(connection_name,paste0("SELECT * FROM tab_name WHERE id_shop=",id"))`

該數據還用於通過selectInput()創建動態UI

output$dependant=renderUI({
      selectInput("choice","Choose the Data you want to view",names(Data))
    })

我無法提出這些功能安排的邏輯。 我無法正常工作。 我創建了一個示例數據和類似的示例代碼供某人嘗試:

library(shiny)
library(ggplot2)
ui=fluidPage(
  column(6,uiOutput("shop_select")),
  column(6,uiOutput("cust_select")),
  column(6,uiOutput("select3")),
  column(12,offset=6,uiOutput("plot"))
)
server = function(input, output) {
  #sample data
  shopdata=data.frame(id=c(1,2,3,4,5,6,7,8,9,10),name=c("a","b","c","d","e","f","g","h","i","j")) 
  cdata=data.frame(id=c(123,465,6798,346,12341,45764,2358,67,457,5687,4562,23,12124,3453,12112),
                   name=c("sadf","porhg","wetgfjg","hwfhjh","yuigkug","syuif","rtyg","dygfjg","rturjh","kuser","zzsdfadf","jgjwer","jywe","jwehfhjh","kuwerg"),
                   shop=c(1,2,1,2,4,6,2,8,9,10,3,1,2,5,7),
                   bill_total=c(12341,123443,456433,234522,45645,23445,3456246,23522,22345,23345,23454,345734,23242,232456,345456),
                   crating=c(4,4.3,5,1.2,3.2,4,3.3,2.4,3.8,3,3.2,3.3,1.4,2.8,4.1))
    output$shop_select=renderUI({
    selectInput("shop_id","Shop ID",shopdata$id)
  })
    output$cust_select=renderUI({
      selectInput("cust_id","Customer ID",cdata$id,multiple = T)
    })
    output$select3=renderUI({
      a=input$shop_id
      selectInput("choice","Choose the Data you want to view",names(cdata))
    })
    output$plot=renderUI({
      renderPlot({
        require(input$choice)
        plotOutput(
      ggplot(cdata,aes(x=cust_id,y=input$choice))
    )})})
}
shinyApp(ui=ui,server=server)

我知道我不清楚這個問題。 修正我發布的代碼足以消除我的疑問。 基本上,我只需要知道這是什么邏輯,當我們在使用使用renderUI()這是依賴於另一個renderUI()

  1. renderUI生成UI元素。 因此,它只能包含ui函數。 您需要使用它來生成plotOutput ,然后分別使用renderPlot添加內容。
  2. 您在aes調用中分配的名稱是您提供的數據框中的變量名稱。 因此x應該是id而不是input$cust_id (必須將其稱為input$cust_id ,因為它引用了一個輸入對象。
  3. input$choice返回一個字符串,而不是一個對象,因此您不能在aes正常使用它(回想一下,如果這是一個正常的數據幀,您的aes將是aes(x=id, y=choice)而不是aes(x='id', y='choice') ,因此,您需要將aes_as.name函數一起使用,以將這些字符串轉換為正確的變量名稱。
  4. 我認為您要對input$cust_id是過濾器cdata以僅包括具有選定id值的行。 dplyr::filter是執行此操作的最佳方法。
  5. 最后,您在ggplot調用中缺少了geom_* ,而實際渲染數據是必需的。

如果將您的output$plot <- ...調用替換為以下代碼,它將按照我認為的方式工作:

output$plot=renderUI({
        plotOutput('plotout')
        })

output$plotout <- renderPlot({
    ggplot(dplyr::filter(cdata, id %in% input$cust_id),
           aes_(x=as.name('id'),y=as.name(input$choice))) +
        geom_point()
})

至於標題中的問題,如果您想將表達式中的代碼限制為僅在發生特定觸發器時才運行,則僅需要使用observeEvent 如果任何反應性值( reactiveValues對象或input$... )發生變化,則任何反應性表達式( reactiveobserverender_等)都將變得無效並進行更新。 如果render_塊中有input$或反應性值,則它們將在它們更改時更新-無需observeEvent

如果要設置一系列子設置操作,然后在每個子集上調用renderUI() ,則需要利用Shiny的renderUI() reactive({})表達式。

反應性表達式是產生變量的代碼塊,其神奇之處在於它們可以“監視”輸入數據的任何更改。 因此,在您的情況下,您shop_id在第一個UI元素中選擇一個shop_id ,反應式表達式會檢測到該值並自動更新!

這是顯示更新的示例,只需選擇不同的shop_id並即時查看可用的cust_id的更改。

library(shiny)
library(ggplot2)
library(tidyverse)
ui=fluidPage(
    column(6,uiOutput("shop_select")),
    column(6,uiOutput("cust_select")),
    column(6,uiOutput("select3")),
    column(12,offset=6,tableOutput("plot"))
)
server = function(input, output) {
    #sample data
    shopdata=data.frame(id=c(1,2,3,4,5,6,7,8,9,10),name=c("a","b","c","d","e","f","g","h","i","j")) 
    cdata=data.frame(id=c(123,465,6798,346,12341,45764,2358,67,457,5687,4562,23,12124,3453,12112),
                     name=c("sadf","porhg","wetgfjg","hwfhjh","yuigkug","syuif","rtyg","dygfjg","rturjh","kuser","zzsdfadf","jgjwer","jywe","jwehfhjh","kuwerg"),
                     shop=c(1,2,1,2,4,6,2,8,9,10,3,1,2,5,7),
                     bill_total=c(12341,123443,456433,234522,45645,23445,3456246,23522,22345,23345,23454,345734,23242,232456,345456),
                     crating=c(4,4.3,5,1.2,3.2,4,3.3,2.4,3.8,3,3.2,3.3,1.4,2.8,4.1))


    output$shop_select=renderUI({
        selectInput("shop_id","Shop ID",shopdata$id)
    })

    cdata_reactive <- reactive({
        req(input$shop_id)
        filter(cdata, shop == input$shop_id)
    })

    output$cust_select=renderUI({
        selectInput("cust_id","Customer ID",cdata_reactive()$id, multiple = T)
    })
    output$select3=renderUI({
        selectInput("choice","Choose the Data you want to view",names(cdata_reactive()))
    })
    output$plot <- renderTable({
        filter(cdata_reactive(), id %in% input$cust_id) %>%
                                   .[input$choice]
                                     })
}
shinyApp(ui=ui,server=server)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM