嗨给定代码使用DT代表字母和百分比列表生成基本表。我希望在DT表中添加一个滑块,这样当我将滑块放在特定的百分比上时,我得到的行对应于该百分比及以下。谢谢,请帮助。
## app.R ##
library(shiny)
library(shinydashboard)
library(DT)
library(scales)
ui <- fluidPage(
titlePanel("Basic datatable"),
# Create a new row for the table.
fluidRow(
DT::dataTableOutput("table")
)
)
server <- function(input, output) {
# Filter data based on selections
output$table <- DT::renderDataTable(DT::datatable({
a = percent(1:10)
b = c("aa","bbb","cc","dd","ee","ff","gg","ff","gg","hh")
data1 = data.frame(a,b)
data1
}))
}
shinyApp(ui, server)
答案 0 :(得分:0)
以下代码改编自here。
library(DT)
DT::datatable(
data.frame(
a = 1:10/100,
b = letters[1:10]
),
filter = "top"
) %>% formatPercentage('a')
答案 1 :(得分:0)
以下是实施sliderInput
并动态更改datatable
中的server
的方法。还有其他方法可以动态控制datatable
,但我发布此代码以符合您的确切要求。
library(shiny)
library(shinydashboard)
library(DT)
a <- percent(1:10)
b <- c("aa","bbb","cc","dd","ee","ff","gg","ff","gg","hh")
data1 <- data.frame(a,b)
a_temp <- as.numeric(gsub("%|,", "", a)) #Removing % and , from the data frame
data1_temp <- data.frame(a_temp,b) #New data frame with integer values to reference row number required later
if(interactive()){
shinyApp(
ui <- fluidPage(
titlePanel("Basic datatable"),
sliderInput("slider1", "Choose Range of Percentage",
min = a_temp[1],
max = a_temp[10],
value = c(a_temp[1], a_temp[10]),
step = 100
),
fluidRow(
DT::dataTableOutput("table")
)
),
server = function(input, output){
minRowVal <- reactive({
which(grepl(input$slider1[[1]], data1_temp$a)) #Retrieve row number that matches selected range on sliderInput
})
maxRowVal <- reactive({
which(grepl(input$slider1[[2]], data1_temp$a)) #Retrieve row number that matches selected range on sliderInput
})
observeEvent(input$slider1, {
output$table <- DT::renderDataTable({
data1[minRowVal():maxRowVal(), ]
})
})
}
)
}