r - 通过上传 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)
解决方案
代码中的语法错误很少,逻辑错误也很少。
您应该将数据存储在可在应用程序中的任何位置使用的反应性对象中。
绘图应保存在
output$plot
对应的echarts4rOutput("plot")
.由于您将列名的字符值传递给 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)
推荐阅读
- mongodb - 如果您打算在“数据库”对象上调用“findOne”方法,它会失败,因为不存在这样的方法
- linux - android终端rm只读文件系统
- reactjs - 如何在 Azure 上设置从中获取 manifest.json 的地址
- java - 关于如何修复和获取 WeightPlot 和 WeightEditor 选项卡的任何建议?
- google-chrome - 当我链接到另一个页面时,Liferay 6.1 PortletSession ID 在 Google Chrome 的 iframe 中发生了更改
- sql - 是否可以从 sql 的表中对列中的所有重复项求和?
- xpath - XPATH - 在某个 html 元素之后停止抓取
- nltk - 获取与其词性标签相关的同义词集
- android - 如何将值从适配器传递到活动(在 oncreate 方法中)我正在获取值 bt 我希望在 create 方法中声明该值
- github - 机器人 fork 我在 GitHub 上的存储库。他们将要做什么?