使用用户界面,nearpoint/renderprint/verbatimTextOutput('print')在用户单击时显示部分变量



下面是我用于Shiny应用程序的代码。我想知道如何具体使用click="plot_click"/近点/渲染打印,以显示部分数据()。如果你看一下附带的图片,你会看到一堆信息已经出现了,但我需要采取"名字"和获胜的时间。

#code------------------------------------  
server<- function(input, output, session) {
#wrangling data:
gslam<- as.data.frame(gslam1)
gslam$tournament <- sapply(gslam$tournament, function(val) 
{agrep(val, c('Australian Open', 'U.S. 
Open','Wimbledon','FrenchOpen'), 

value = TRUE)})
#pivot plot based on Tournament
reactive_data<-reactive({
req(input$sel_tournament)
df<- gslam %>% filter(tournament %in% 
input$sel_tournament)%>%group_by(winner,tournament)%>% 
mutate(winningNumber=n())
df<-df %>% arrange(winner)
})
#dynamic list
observe({
updateSelectInput(session,"sel_tournament", choices = 
gslam$tournament, select="U.S. Open")
})

# create the plot  
output$WinnerPlot <- renderPlot({
g<- ggplot(reactive_data(),aes( y =winner,  x 
=winningNumber),decreasing=FALSE) + 
theme(legend.position=none")

g + geom_bar (stat ="identity&quot宽度= 0.5,填补="black")})

输出$print = renderPrint({nearPoints (Reactive_data(), #绘图数据输入$plot_click, # Input变量获取x/yMaxpoints = 1, #只显示最近的一个点阈值= 1000 #基本上是一个搜索半径。设置这个大

# to show at least one point per click
)})
output$Top5Plot <-renderPlot({
g<- ggplot(reactive_data(),aes( y =winner,  x 
=winningNumber),decreasing=FALSE) + theme(legend.position = 
"none")
g+geom_bar(stat="identity",width=0.5, fill="black")
})
}
ui <- navbarPage("Grand Slam Shiny Application", id="cc",
tabPanel("Winners' Rank",
fluidRow(titlePanel("Grand Slam 
ShinyApp"),
sidebarPanel(
selectInput(inputId = 
"sel_tournament",label="Choose Tournament:","Names"))), 
plotOutput(("WinnerPlot"), click="plot_click"),
verbatimTextOutput('print')
),

tabPanel("Top 5 Winners' Performance", 
plotOutput("Top5Plot"))
)
# Run the App
shinyApp(ui, server)

您可以在nearPoints中寻求dplyr::select的帮助。

output$print = renderPrint({
nearPoints(
dplyr::select(reactive_data(), winner, winningNumber),      
input$plot_click,   
maxpoints = 1,      
threshold = 1000  
)})

最新更新