从 selectInput() 选择轴变量未从 plotly 中读取为数字

Selecting axis variable from selectInput() is not being read as numeric from plotly

在下面闪亮的应用程序中,当我尝试选择我的 y-axis 变量作为 selectInput() 的输入时,它好像无法被读取为数字并且显示不正确。

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)

您可以这样使用 get()

    fig <- plot_ly(su, type = 'scatter', mode = 'lines')%>%
      add_trace(x = ~WaterYear, y = ~get(input$SEL), name = as.character(input$SEL))%>%
      layout(showlegend = F)