我有一个selectInput小工具要求用户选择 y 轴。选项包括流派、体验和评级,我想输出我创建的三个条形图之一(ratebars3、expbars3 和 genbars4)。每个图都有相同的 x 轴(公司),但图表会根据 y 的不同而有所不同。以下是我的 UI 中选择输入的代码:
selectInput("selecty", label = h3("Select Y axis"),
choices = list("Rating", "Experience", "Genre"))
Run Code Online (Sandbox Code Playgroud)
我一直if.. return在服务器中尝试一些类型语句,但没有成功。如果有人知道一些服务器代码可以根据用户输入的内容显示我的绘图,那将是一个巨大的帮助。
我正在使用该plotly软件包,但似乎我已经回答了我自己的问题。我必须创建一个反应式表达式:
服务器:
barplottest <- reactive({
if ( "Rating" %in% input$selecty) return(bars3)
if ( "Experience" %in% input$selecty) return(expbars3)
if( "Genre" %in% input$selecty) return(genbars4)
})
output$barplot <- renderPlotly({
dataplots = barplottest()
print(dataplots)
})
Run Code Online (Sandbox Code Playgroud)
用户界面
selectInput("selecty", label = h3("Select Y axis"),
choices = list("Rating", "Experience", "Genre")),
plotlyOutput("barplot")
Run Code Online (Sandbox Code Playgroud)