在shiny中从上传的文件创建动态ggvis图表

4
我正在尝试使用Shiny和ggvis来:
1)上传数据集
2)让用户选择两列(x,y)
3)返回一个显示从上传的数据集中提取的(x,y)的ggvis图表
我已经尝试编辑Shiny Interactivity页面的示例以及电影浏览器示例。但是,没有显示任何图表。
我认为我的问题在于上传数据集,但我不知道从哪里开始...有什么建议吗?
注意-我也尝试过使用rCharts,但我遇到了类似的问题,即没有显示任何图表。

server.R

library(shiny)
library(dplyr)
library(ggvis)

shinyServer(function(input, output, session) {

fileHeaderNames <- reactive({

  infile <- input$datfile

  if(is.null(infile))
    return(NULL)

  d <- read.csv(infile$datapath, header = T)
  return(names(d))

})

# dynamic variable names
observe({

  updateSelectInput(session, 'x', choices = fileHeaderNames())
  updateSelectInput(session, 'y', choices = fileHeaderNames())

}) # end observe

  # uploading data set
  theData <- reactive({ 

    validate(
       need(input$datfile != "", "Please upload a file")
    )

    infile <- input$datfile
    dat <- read.csv(infile$datapath, 
                    header = T,
                    stringsAsFactors = F)

    if(is.null(infile)) return(NULL)

    data.frame(x = dat[, input$x],
               y = dat[, input$y])

    })

  # A simple visualisation. In shiny apps, need to register observers
  # and tell shiny where to put the controls
  theData %>%
    ggvis(~x, ~y) %>%
    layer_points() %>%
    bind_shiny("plot", "plot_ui")

})

ui.R

library(ggvis)
library(shiny)
shinyUI(pageWithSidebar(
  div(),
  sidebarPanel(
    fileInput('datfile', ''),
    selectInput('x', 'x:' ,'x'),
    selectInput('y', 'y:', 'y'),
    uiOutput("plot_ui")
  ),
  mainPanel(
    ggvisOutput("plot")
  )
))

我可以使用googleVis让它工作,但我想使用一些在该包中不可用的功能。 - maloneypatr
1个回答

5
这里有一种尝试,我添加了几个响应块以获取应该添加在图表轴上的名称。
一个技巧是创建一个经过筛选的数据框,其中包含两列"x"和"y",当用户更改"selectInput"中的值时,该数据框会发生变化。然后您可以告诉"ggvis"从该经过筛选的数据框绘制"x"和"y",这样就可以进行交互式绘图。
library(shiny)
library(dplyr)
library(ggvis)

shinyServer(function(input, output, session) {
  #load the data when the user inputs a file
  theData <- reactive({
    infile <- input$datfile        
    if(is.null(infile))
      return(NULL)        
    d <- read.csv(infile$datapath, header = T)
    d        
  })



  # dynamic variable names
  observe({
    data<-theData()
    updateSelectInput(session, 'x', choices = names(data))
    updateSelectInput(session, 'y', choices = names(data))

  }) # end observe

  #gets the y variable name, will be used to change the plot legends
  yVarName<-reactive({
    input$y
  })

 #gets the x variable name, will be used to change the plot legends
  xVarName<-reactive({
    input$x
  })

  #make the filteredData frame

  filteredData<-reactive({
    data<-isolate(theData())
    #if there is no input, make a dummy dataframe
    if(input$x=="x" && input$y=="y"){
      if(is.null(data)){
        data<-data.frame(x=0,y=0)
      }
    }else{
      data<-data[,c(input$x,input$y)]
      names(data)<-c("x","y")
    }
    data
  })

  #plot the ggvis plot in a reactive block so that it changes with filteredData
  vis<-reactive({
    plotData<-filteredData()
    plotData %>%
    ggvis(~x, ~y) %>%
    layer_points() %>%
    add_axis("y", title = yVarName()) %>%
    add_axis("x", title = xVarName()) %>%
    add_tooltip(function(df) format(sqrt(df$x),digits=2))
  })
    vis%>%bind_shiny("plot", "plot_ui")

})

编辑:添加了工具提示。


嘿@NicE,非常感谢你在这件事上的帮助!我从未想过将data <- theData()放在observe语句中。最后一件我想做的事情是添加工具提示,我认为它会起作用,但如果我无法做到,我可能需要寻求帮助。:p 再次感谢! - maloneypatr
当然,很高兴能帮忙! - NicE
你能否更新一下代码,演示一下如何使用工具提示?我在添加工具提示和编辑数据时遇到了错误。例如,如果我想计算sqrt(x)而不是x,它就无法正常工作。再次感谢您的帮助! - maloneypatr
1
我在ggvis图中添加了一个提示行,当你悬停在一个点上时,你会得到一个带有x值的平方根的提示。格式只是保持为两位小数。 - NicE
我想值得强调的是,空的虚拟数据框架至关重要,否则在上传任何内容之前应用程序将崩溃,因为Vega(ggvis的底层)无法处理NULL。 - Matt Bannert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接