使用reactiveValues和ggplot有光泽 [英] Working with reactiveValues and ggplot in shiny
问题描述
aes(factor(...),
)来做到这一点。但是,如果我添加了使用无功输入更改绘图变量的选项,必须使用aes_string。如果我使用aes_string,它不会像 aes(factor(...),
。这里是一个工作示例: 服务器:
#创建示例数据
set.seed(10)
MeasurementA -norm(1000,5,2)
MeasurementB -norm(1000,5,2)
Wafer < - rep(c(1: 100),每个= 10)
ID <-rep(c(101:200),each = 10)
Batch <-rep(c(1:10),each = 100)
dd < - data.frame(Batch,Wafer,ID,MeasurementA,MeasurementB)
#创建本地连接(实际上这将连接到主机站点)
con < - dbConnect(RSQLite :: SQLite(),:memory:)
dbWriteTable(con,dd,dd)
query< - function (...)dbGetQuery(con,...)
#创建空数据框来填充
wq = data.frame()
sq = data .frame()
shinyServer(函数(输入,输出){
#创建数据框以存储查询中的反应数据集
values < - reactiveValues()
值$ df< - data.frame()
#第一个查询的动作按钮
d< - eventReactive(输入$ do,{输入$ wafer})
#反应查询的第一阶段
a < - reactive({paste(从dd中选择ID where Wafer =,d(),sep =)})
wq < - reactive({query(a())})
#确认查询的输出是正确的
输出$ que< - renderPrint({a()})
输出$ pos< - renderPrint(wq()[1,1])$ b
$ b#动作按钮将查询结果添加到数据框
e < - eventReactive(input $ do2 ,{(qq),{{}}}
$ bb bb < - 反应({paste(select Wafer,Batch,MeasurementA,通过ID asc;,sep =)})
#观察e(),以便在用户按动作按钮之前不会添加数据
observe({
i f(!is.null(e())){
sq < - reactive({query(b())})
#将查询添加到反应数据框
值$ df <-rbind(isolate(values $ df),sq())
}
})
#结果输出
#无选择(工作)
输出$ boxV< - renderPlot({
ggplot(values $ df,aes(factor(Wafer),MeasurementA,fill = factor(Batch) ))+ geom_boxplot()
})
#使用度量选项(不工作)
#输出$ boxV< - renderPlot({
#ggplot(values $ df,aes_string ('Wafer'),输入$ char,fill = factor('Batch')))+ geom_boxplot()
#})
})
用户界面:
library(降价)
$ b $ shinyUI(fluidPage(
titlePanel(Generic grapher),
sidebarLayout(
sidebarPanel(
numericInput(wafer ,label = h3(Input wafer ID:),value = NULL),
actionButton(do,Search wafer),
actionButton(do2,Add to data frame),
selectInput(char,label =Boxplot choice:,
choices = list(A=MeasurementA B=MeasurementB),
selected =Von.fwd)
),
mainPanel(
verbatimTextOutput(que) ,
verbatimTextOutput(pos),
plotOutput(boxV)
)
)
)
)
我为工作和非工作都添加了输出绘图代码(非工作被注释掉)。
现在,我读了这个(在Shiny中格式化被动数据框架)和这个( R闪亮修改被动数据框)但我很困惑。因为即时通讯使用reactiveValues来存储数据,我使用代码值$ df来访问数据......但是如果我想将列转为上述目的的因子呢?这似乎不起作用:
new < - reactive(as.factor(values $ df $ Wafer))
也许我正在用这个吠叫错误的树?
好吧,我通过在查询本身内改变数据类型来解决问题:
b < - 反应({paste(从Wafer选择cast(Wafer as varchar),cast从批处理转换(批量为varchar) ID =,e(),Order by ID asc;,sep =)})
<这样,我不必在事后惹事。它适用于我,但如果有人阅读本文,想告诉我它是一个坏主意,请做。我新来SQL和R所以请纠正我,所以我可以学习。谢谢
Im using R and shiny to query an SQL database. The user can search and add to a reactive data frame, the output of which is plotted in ggplot. However, I need to change the columns of the reactive data frames to factors for plotting. I can do this directly with ggplot (aes(factor(...),
). However, if I add the option of changing the plotted variable using a reactive input, I must use aes_string. If I use aes_string it does not like aes(factor(...),
. Here is a working example:
Server:
# Create example data
set.seed(10)
MeasurementA <- rnorm(1000, 5, 2)
MeasurementB <- rnorm(1000, 5, 2)
Wafer <- rep(c(1:100), each=10)
ID <- rep(c(101:200), each=10)
Batch <- rep(c(1:10), each=100)
dd <- data.frame(Batch, Wafer, ID, MeasurementA, MeasurementB)
# Create local connection (in reality this will be a connection to a host site)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "dd", dd)
query <- function(...) dbGetQuery(con, ...)
# Create empty data frames to populate
wq = data.frame()
sq = data.frame()
shinyServer(function(input, output){
# create data frame to store reactive data set from query
values <- reactiveValues()
values$df <- data.frame()
# Action button for first query
d <- eventReactive(input$do, { input$wafer })
# First stage of reactive query
a <- reactive({ paste("Select ID from dd where Wafer=",d(), sep="") })
wq <- reactive({ query( a() ) })
# Output to confirm query is correct
output$que <- renderPrint({ a() })
output$pos <- renderPrint( wq()[1,1] )
# Action button to add results from query to a data frame
e <- eventReactive(input$do2, { wq()[1,1] })
b <- reactive({ paste("select Wafer, Batch, MeasurementA, MeasurementB from dd where ID=",e()," Order by ID asc ;", sep="") })
# observe e() so that data is not added until user presses action button
observe({
if (!is.null(e())) {
sq <- reactive({ query( b() ) })
# add query to reactive data frame
values$df <- rbind(isolate(values$df), sq())
}
})
# output of results
# Without mesurement choice (works)
output$boxV <- renderPlot({
ggplot(values$df, aes(factor(Wafer), MeasurementA, fill=factor(Batch))) + geom_boxplot()
})
# With measurement choice (doesnt work)
#output$boxV <- renderPlot({
#ggplot(values$df, aes_string(factor('Wafer'), input$char, fill=factor('Batch'))) + geom_boxplot()
#})
})
UI:
library(markdown)
shinyUI(fluidPage(
titlePanel("Generic grapher"),
sidebarLayout(
sidebarPanel(
numericInput("wafer", label = h3("Input wafer ID:"), value = NULL),
actionButton("do", "Search wafer"),
actionButton("do2", "Add to data frame"),
selectInput("char", label="Boxplot choice:",
choices = list("A"="MeasurementA", "B"="MeasurementB"),
selected="Von.fwd")
),
mainPanel(
verbatimTextOutput("que"),
verbatimTextOutput("pos"),
plotOutput("boxV")
)
)
)
)
Ive added output plot code for both working and non-working (non-working is commented out).
Now, ive read this (Formatting reactive data.frames in Shiny) and this (R shiny modify reactive data frame) but im confused. Because im using reactiveValues to store data, I use the code values$df to access the data...but what if i I want to turn a column to a factor for purpose of above? this doesnt seem to work:
new <- reactive(as.factor(values$df$Wafer))
Perhaps I am barking up the wrong tree with this?
Ok, I solved the problem by changing the data type within the query itself:
b <- reactive({ paste("select cast(Wafer as varchar) as Wafer, cast(Batch as varchar) as Batch, MeasurementA, MeasurementB from dd where ID=",e()," Order by ID asc ;", sep="") })
That way I didnt have to mess about afterwards. It works for me but if anyone reading this wants to tell me that its a bad idea, please do. Im new to SQL and R so please correct me so I can learn. Thanks
这篇关于使用reactiveValues和ggplot有光泽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!