首页 > 解决方案 > 通过上传 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', label = 'Selección de variables', choices = 'Aún no hay variables a escoger'),
  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", label = "Select", choices = colnames(mytable))
    
  })
  
  mytable <- renderEcharts4r({
    myfileinput |> 
      e_charts(input$mydropdown)  |> 
      e_line(input$mydropdown1)
  })
}
shinyApp(ui, server)

标签: rshinyecharts4r

解决方案


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

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

  2. 绘图应保存在output$plot对应的echarts4rOutput("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', label = 'Selección de variables', choices = 'Aún no hay variables a escoger'),
  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", label = "Select", choices = colnames(rv$mytable))
    
  })
  
  output$plot <- renderEcharts4r({
    req(rv$mytable)
    rv$mytable |> 
      e_charts_(input$mydropdown)  |> 
      e_line_(input$mydropdown1)
  })
}

shinyApp(ui, server)

推荐阅读