我有一个 Shiny 应用程序,它可以显示绘图或打印数据框。虽然它两者都做,但它只打印数据框的第 10 行并添加“... 86 行”。我想显示至少 40 行数据框。我尝试了 a & head(a, n=50) 但它只显示总数的 10 行。我怎样才能让它显示更多的行。
这就是我所拥有的
output$IPLMatch2TeamsPlot <- renderPlot({
printOrPlotIPLMatch2Teams(input, output)
})
# Analyze and display IPL Match table
output$IPLMatch2TeamsPrint <- renderPrint({
a <- printOrPlotIPLMatch2Teams(input, output)
head(a,n=50)
#a
})
output$plotOrPrintIPLMatch2teams <- renderUI({
# Check if output is a dataframe. If so, print
if(is.data.frame(scorecard <- printOrPlotIPLMatch2Teams(input, output))){
verbatimTextOutput("IPLMatch2TeamsPrint")
}
else{ #Else plot
plotOutput("IPLMatch2TeamsPlot")
}
})
Run Code Online (Sandbox Code Playgroud)
用户界面
tabPanel("Head to head",
headerPanel('Head-to-head between 2 IPL teams'),
sidebarPanel(
selectInput('matches2TeamFunc', 'Select function', IPLMatches2TeamsFuncs),
selectInput('match2', 'Select matches', IPLMatches2Teams,selectize=FALSE, size=20),
uiOutput("selectTeam2"),
radioButtons("plotOrTable1", label = h4("Plot or table"),
choices = c("Plot" = 1, "Table" = 2),
selected = 1,inline=T)
),
mainPanel(
uiOutput("plotOrPrintIPLMatch2teams")
)
Run Code Online (Sandbox Code Playgroud)
当您知道您的输出将是一个 data.frame 而不仅仅是任何随机的文本位时,您可以选择一个为显示表格数据而优化的输出。你可以试试renderTableandtableOutput而不是你的renderPrintand verbatimTextOutput。另一种选择renderDataTable来自 DT 包。这将创建一个表,在不同的页面上放置额外的行,以便您可以访问所有行,并且您可以随时修改它将显示的行数。
例如,将当前的替换renderPrint为以下内容:
output$IPLMatch2TeamsPrint <- DT::renderDataTable({
a <- printOrPlotIPLMatch2Teams(input, output)
datatable(a,
options = list(
"pageLength" = 40)
)
})
Run Code Online (Sandbox Code Playgroud)
并替换你verbatimTextOutput("IPLMatch2TeamsPrint")的DT::dataTableOutput("IPLMatch2TeamsPrint")应该给你一个有 40 行的表格,并且可以选择将更多的行作为表格中的不同页面。
为了清晰起见,您可能还想将名称从打印更改为表格,因为您不再只是打印了。