从上传 csv 文件渲染 echart

问题描述

我正在尝试制作一个应用程序,该应用程序可以通过将 csv 文件上传到它来为您呈现图表,您可以在其中选择要绘制的变量。事实是我不知道我做错了什么,因为该应用程序不呈现图形。有什么想法或建议吗?

代码是:

library(shiny)
library(echarts4r)

ui <- fluidPage(
  
  selectInput('mydropdown',label = 'Selección de variables',choices = 'Aún no hay variables a escoger'),selectInput('mydropdown1',fileInput('myfileinput',label = 'Archivo a usar',accept = c(".csv")),echarts4rOutput("plot")

)

   #Server
   server <- function(input,output,session) {
  
   observeEvent(input$myfileinput,{
    
    mytable <- read.csv(input$myfileinput$datapath)
    
    updateSelectInput(session,"mydropdown",label = "Select",choices = colnames(mytable))
    updateSelectInput(session,"mydropdown1",choices = colnames(mytable))
    
  })
  
  mytable <- renderEcharts4r({
    myfileinput |> 
      e_charts(input$mydropdown)  |> 
      e_line(input$mydropdown1)
  })
}
shinyApp(ui,server)

解决方法

代码中的语法错误很少,逻辑错误也很少。

  1. 您应该将数据存储在可在应用程序中的任何位置使用的反应式对象中。

  2. 绘图应保存在对应于 output$plotecharts4rOutput("plot") 中。

  3. 由于您将列名的字符值传递给 echarts,因此请使用函数 e_charts_e_line_

尝试以下操作 -

library(shiny)
library(echarts4r)

ui <- fluidPage(
  
  selectInput('mydropdown',label = 'Selección de variables',choices = 'Aún no hay variables a escoger'),selectInput('mydropdown1',fileInput('myfileinput',label = 'Archivo a usar',accept = c(".csv")),echarts4rOutput("plot")
  
)

#Server
server <- function(input,output,session) {
  
  rv <- reactiveValues()
  
  observeEvent(input$myfileinput,{
    
    rv$mytable <- read.csv(input$myfileinput$datapath)
    
    updateSelectInput(session,"mydropdown",label = "Select",choices = colnames(rv$mytable))
    updateSelectInput(session,"mydropdown1",choices = colnames(rv$mytable))
    
  })
  
  output$plot <- renderEcharts4r({
    req(rv$mytable)
    rv$mytable |> 
      e_charts_(input$mydropdown)  |> 
      e_line_(input$mydropdown1)
  })
}

shinyApp(ui,server)