In the shiny app below when I try to choose my y-axis
variable as an input from selectInput()
it is like it cannot be read as numeric and is not displayed correctly.
library(shiny)
library(shinydashboard)
library(plotly)
su<-structure(list(WaterYear = c(2014, 2015, 2016, 2017, 2018, 2019
), Discharge = c(1783.939638, 1970.891674, 1937.04757, 5421.213746,
1778.965559, 1172.697293), EnvWater = c(6, 1, 1, 0, 242, 5)), row.names = c(NA,
-6L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"))
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput("SEL","select y",choices = colnames(su)[c(2,3)])
),
dashboardBody(
plotlyOutput("pro")
)
)
server <- function(input, output) {
output$pro<-renderPlotly({
fig <- plot_ly(su, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~WaterYear, y = ~as.numeric(input$SEL), name = as.character(input$SEL))%>%
layout(showlegend = F)
options(warn = -1)
fig <- fig %>%
layout(
xaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
plot_bgcolor='#e5ecf6', width = 900)
fig
})
}
shinyApp(ui, server)
CodePudding user response:
You can use get()
like this:
fig <- plot_ly(su, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~WaterYear, y = ~get(input$SEL), name = as.character(input$SEL))%>%
layout(showlegend = F)