使用reactiveValues和ggplot处理闪亮

时间:2015-08-10 10:11:23

标签: r ggplot2 shiny

我使用R和shiny来查询SQL数据库。用户可以搜索并添加到反应数据帧,其输出在ggplot中绘制。但是,我需要将反应数据帧的列更改为绘图的因子。我可以使用ggplot(aes(factor(...),)直接执行此操作。但是,如果我添加使用反应输入更改绘制变量的选项,我必须使用aes_string。如果我使用aes_string,它不喜欢aes(factor(...),。这是一个有效的例子:

服务器:

# 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")
      )
    )
  )
)

我添加了工作和非工作的输出图代码(非工作已注释掉)。

现在,我读过这个(Formatting reactive data.frames in Shiny)和这个(R shiny modify reactive data frame),但我很困惑。因为我使用reactiveValues来存储数据,所以我使用代码值$ df来访问数据...但是如果我想将列转换为上述目的的因素呢?这似乎不起作用:

new <- reactive(as.factor(values$df$Wafer))

也许我正在咆哮错误的树?

1 个答案:

答案 0 :(得分:0)

好的,我通过更改查询本身的数据类型来解决问题:

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="") })

那样我以后就不用乱了。它适用于我,但如果有人读这篇文章想告诉我这是一个坏主意,请做。我是SQL和R的新手所以请纠正我,这样我就可以学习。感谢