【问题标题】:Create dynamic ggvis chart from uploaded file in shiny从闪亮的上传文件创建动态ggvis图表
【发布时间】:2015-03-26 10:47:02
【问题描述】:

我正在尝试使用 Shiny 和 ggvis 来:

1) 上传数据集

2) 让用户选择 2 列 (x, y)

3) 从上传的数据集中返回一个显示 (x, y) 的 ggvis 图

我尝试编辑 Shiny Interactivity 页面和 movie explorer 示例中的示例。但是,没有显示图表。

我认为我的问题是关于上传数据集,但我不知道从哪里开始...有什么建议吗?

注意 - 我也尝试过使用 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 一起使用,但我想使用该软件包中没有的一些功能。

标签: r shiny rcharts ggvis


【解决方案1】:

这是一个尝试,我添加了几个反应块来获取应该在绘图轴上添加的名称。

您可以使用的一个技巧是创建一个过滤后的数据框,该数据框有两列 xy,并且当用户更改 selectInput 中的值时会发生变化。然后,您可以告诉 ggvis 从过滤后的数据框中绘制 xy,并且该绘图将是交互式的。

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 &lt;- theData() 放在observe 语句中。我最不想听到的是添加工具提示,我认为它会起作用,但如果我不能,我可能不得不伸出援手。 :p 再次感谢!
  • 当然,很高兴我能帮上忙!
  • 你能更新这个来演示工具提示是如何工作的吗?我在添加工具提示和编辑数据时遇到错误。例如,如果我想计算 sqrt(x) 而不是 x,它不起作用。再次感谢您的帮助!
  • 我在 ggvis 图中添加了一条工具提示行,当您将鼠标悬停在一个点上时,您将获得一个带有 x 值 sqrt 的工具提示。格式只是为了保持两位数。
  • 我想值得强调的是,空的虚拟 data.frame 至关重要,否则应用程序将在您有机会上传任何内容之前崩溃,因为 Vega 无法处理 NULL(在ggvis 的引擎盖)。
猜你喜欢
  • 1970-01-01
  • 2016-10-11
  • 2016-02-22
  • 2017-04-24
  • 2014-06-19
  • 1970-01-01
  • 2018-03-12
  • 2021-01-23
  • 1970-01-01
相关资源
最近更新 更多