R 如何使用plotly创建条形图?

R 如何使用plotly创建条形图?,r,shiny,plotly,R,Shiny,Plotly,我正在尝试生成一个包含用户输入的条形图。我试着在输入之前使用~,但不起作用。我看到的是一个带有axis标签的空白画布 代码 库(闪亮) 图书馆(绘本) ui您可以使用get()将输入变量转换为plotly可以使用的变量。在您的情况下,请尝试以下方法: output$bar <- renderPlotly( mtcars %>% plot_ly( x = ~get(input$input1), y =

我正在尝试生成一个包含用户输入的条形图。我试着在输入之前使用
~
,但不起作用。我看到的是一个带有axis标签的空白画布

代码

库(闪亮)
图书馆(绘本)
ui您可以使用
get()
将输入变量转换为plotly可以使用的变量。在您的情况下,请尝试以下方法:

 output$bar <- renderPlotly(
     mtcars %>% 
         plot_ly(
             x = ~get(input$input1),
             y = ~get(input$input2),
             
             type = "bar"
         )
输出$bar%
阴谋(
x=~get(输入$input1),
y=~get(输入$input2),
type=“bar”
)
您可以使用
get()
将输入变量转换为绘图时可以使用的变量。在您的情况下,请尝试以下方法:

 output$bar <- renderPlotly(
     mtcars %>% 
         plot_ly(
             x = ~get(input$input1),
             y = ~get(input$input2),
             
             type = "bar"
         )
输出$bar%
阴谋(
x=~get(输入$input1),
y=~get(输入$input2),
type=“bar”
)
第二种选择是使用
as.公式类似于这样:

server <- function(input, output, session) {
  output$bar <- renderPlotly(
    mtcars %>% 
      plot_ly(
        x = as.formula(paste0('~', input$input1)),
        y = as.formula(paste0('~', input$input2)),
        
        type = "bar"
      )
  )
}
server第二个选项是使用
as.formula
如下:

server <- function(input, output, session) {
  output$bar <- renderPlotly(
    mtcars %>% 
      plot_ly(
        x = as.formula(paste0('~', input$input1)),
        y = as.formula(paste0('~', input$input2)),
        
        type = "bar"
      )
  )
}
服务器