如果在没有ObserveEvent()的情况下更改输入,如何使更改反映在许多地方
我有一个输入变量如果在没有ObserveEvent()的情况下更改输入,如何使更改反映在许多地方,r,ggplot2,shiny,R,Ggplot2,Shiny,我有一个输入变量input$shop\u id。用于通过以下方式获取服务器函数中的数据: observeEvent(input$shop_id,{id<<-input$shop_id}) `Data=dbGetQuery(connection_name,paste0("SELECT * FROM tab_name WHERE id_shop=",id"))` 我想不出这些功能安排的逻辑。我不能让它工作。我已经创建了一个示例数据和类似的示例代码供某人试用: library(shin
input$shop\u id
。用于通过以下方式获取服务器函数中的数据:
observeEvent(input$shop_id,{id<<-input$shop_id})
`Data=dbGetQuery(connection_name,paste0("SELECT * FROM tab_name WHERE id_shop=",id"))`
我想不出这些功能安排的逻辑。我不能让它工作。我已经创建了一个示例数据和类似的示例代码供某人试用:
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()时必须使用的逻辑是什么
renderUI
生成UI
元素。因此,它只能包含ui
函数。您需要使用它来生成plotOutput
,然后分别使用renderPlot
来添加内容
在aes
调用中指定的名称是您提供的数据框中的变量名称。因此,x
应该是id
而不是input$cust\u id
的值(必须称为input$cust\u id
,因为它指的是输入对象)
input$choice
返回一个字符串,而不是一个对象,因此您不能在aes
中正常使用它(回想一下,如果这是一个正常的数据帧,您的aes将是aes(x=id,y=choice)
而不是aes(x='id',y='choice')
。因此,您需要将aes\uu
与as.name
函数一起使用,以将这些字符串转换为正确的变量名
我认为您想要使用input$cust\u id
执行的是筛选cdata
以仅包括具有所选id
值的行。dplyr::filter
是最好的方法
最后,在ggplot调用中缺少一个geom.*
,这是实际渲染数据所必需的
如果要设置一系列子集操作,然后在每个子集上调用renderUI()
s,则需要利用Shiny的reactive({})
表达式来替换output$plot
反应式表达式是生成变量的代码块,其神奇之处在于它们“监视”输入数据的任何更改。因此,在您的示例中,您在第一个UI元素中选择一个shop\u id
,反应式表达式会自动检测并更新自身
下面是一个显示更新的示例,只需选择不同的店铺id,并动态观察可用的cust\u id
s更改
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)
库(闪亮)
图书馆(GG2)
图书馆(tidyverse)
ui=fluidPage(
列(6,uiOutput(“shop_select”),
列(6,uiOutput(“客户选择”),
列(6,uiOutput(“select3”),
列(12,偏移量=6,表格输出(“绘图”))
)
服务器=功能(输入、输出){
#样本数据
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(123465679834612341457642358,6745756874562,2312124345312112),
名称=c(“sadf”、“porhg”、“wetgfjg”、“hwfhjh”、“YUIKUG”、“syuif”、“rtyg”、“dygfjg”、“rturjh”、“kuser”、“ZZSDFAF”、“jgjwer”、“jywe”、“jwehfhjh”、“kuwerg”),
车间=c(1,2,1,2,4,6,2,8,9,10,3,1,2,5,7),
工程量清单总计=c(12341123443456433234524564523445345624623522223455233452345234523452345234523452345234523452345234523452345234523452422324563456),
板条箱=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))
输出$shop\u select=renderUI({
选择输入(“店铺id”、“店铺id”、店铺数据$id)
})
cdata\u这正是我所需要的。非常感谢!
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()
})
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)