I have a chart where axis (x & y) are selected by a userinput. Firstly the user chooses the criteria type with a radio button, then a select input appears for him to set the actual axis parameter he wants. The dropdown menu in select input is a list of column names.
I'm able to make this work just fine with ggplot using the code below :
ui<- fluidPage(
radioButtons("xbkct", h1(class="cch", "Select X-axis criteria"),
choices =c("Bookies"="bks", "Team"="tmc"),
inline = TRUE
),
conditionalPanel("input.xbkct==='bks'",
selectInput("xbookct1", "Bookies criteria",
choices = NULL)
),
conditionalPanel("input.xbkct==='tmc'",
selectInput("xteamct1", "Team criteria",
choices = NULL)
),
radioButtons("ybkct", h1(class="cch","Select Y-axis criteria"),
choices = c("Bookies"="bks", "Team"="tmc")
,inline = TRUE
),
conditionalPanel("input.ybkct==='bks'",
selectInput("ybookct1", "Bookies criteria",
choices = NULL)
),
conditionalPanel("input.ybkct==='tmc'",
selectInput("yteamct1", "Team criteria",
choices = NULL)
)
plotOutput("plotui1")
)
server<- function(input, output, session) {
observeEvent(input$dataset,{
updateSelectInput(session, input = "xbookct1",
choices = sort(colnames(dataset()[c(18:19,21:26)])))
})
observeEvent(input$dataset,{
updateSelectInput(session, input = "xteamct1",
choices = sort(colnames(dataset()[c(27:38)])))
})
xvar <- reactive({
if (input$xbkct=='bks')
{
input$xbookct1
} else if (input$xbkct=='tmc')
{ input$xteamct1
}
})
yvar <- reactive({
if (input$ybkct=='bks')
{
input$ybookct1
} else if (input$ybkct=='tmc')
{ input$yteamct1
}
})
output$plotui1 <- renderPlot({
g1<-ggplot(dft(), aes_string(x=xvar(), y=yvar(),size=sz(),fill=as.factor(dft()$OUTCOME))) +
geom_point(alpha=0.8, shape=21)
g1
}]
However now I'm switching to the echarts4R package and I can't make it work.
Obviously I've change plotOutput() on the UI Side with echarts4rOutput().
The error is coming from the server side when I'm trying to select my e_chart() series with xvar() and yvar(). I've tried the following but couldn't make it work:
output$plotui1<- renderEcharts4r({
g1<- dft() %>%
e_chart(xvar()) %>%
e_scatter(yvar(),sz())
g1
})
I've not included the sz() criteria (size) but it follows the same logic as xvar() and yvar()
Similiar to using
ggplot2in a shiny appecharts4rrequires slightly more effort to use the values selected by the user. (Sidenote:aes_stringwas deprecated inggplot2 3.4.0. Instead it is recommended to switch to the.datapronoun. See?aes_string).One option would be to use the
xxx_family of functions, e.g.e_charts_, which serve a similar purpose as the now-deprecatedaes_stringand are actually called under the hood by the "non"-_version of the function. A second option would be to do some renaming, e.g. in the code below I rename the column to be mapped onxasxand so on.Using a minimal reprex based on
mtcars: