下载在 Shiny 中创建的 table
Download a table created in Shiny
我需要给用户一组 60 个观察结果。我有一个 master table,我想从中提取这 60 个观察结果的子集。因此,(1) 我将主机 table 作为已发布的 csv 文件托管在 google 驱动器上。 (2) 在 R studio 中为 60 个值的子集编写一个闪亮的代码。用户将必须输入我用作 set.seed 的组 ID,并确保用户每次尝试获取 60 个观察值时看到相同的子集。而且,它还可以帮助我跟踪用户的观察结果。
代码运行良好,我能够显示子集 table。但是,我无法下载工作。我看到一个 post 说 renderTable 创建一个无法下载的 HTML table,我应该在它外面创建 table。我尝试使用 reactive 来执行此操作,但它不起作用并且不断出现各种错误。例如:
"cannot coerce class ‘c("reactiveExpr", "reactive", "function")’ to a data.frame"
将不胜感激 - 即使有人可以指出我应该阅读的内容并尝试完成这项工作。
library(shiny)
db1 <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vS94xYLix6bDUNNXAgHejdQ-CcWi-G4t25nfxuhRZF57TloC8NwVgnperBB9-U-IuDvMcOnvdc9iavU/pub?gid=0&single=true&output=csv")
# Define UI
ui <- fluidPage(
# Application title
titlePanel("MnM"),
# Sidebar to take input of group ID
sidebarLayout(
sidebarPanel(
numericInput("seed","Group ID:", value = 100, min = 100, max = 999),
downloadButton("downloadData", "Download")
),
# Show the table
mainPanel(
tableOutput("table")
)
)
)
# Define server logic for the table
server <- function(input, output) {
output$table <- renderTable({
set.seed(input$seed)
zz <- sample(1:nrow(db1), size = 60, replace = TRUE)
data.frame(db1[zz,])})
output$downloadData <- downloadHandler(
filename = "test.csv",
content = function(file) {
write.csv(output$table, file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
创建您的 table 一次,然后在您的 renderTable
和 downloadHandler
中使用它。将其创建为反应式,因此它随处可用。
请注意,downloadHandler
在 RStudio 的预览中不起作用,请改为在浏览器中查看。有一个标有 'Open in Browser' 的按钮可以执行此操作。
这是您应用的代码:
library(shiny)
db1 <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vS94xYLix6bDUNNXAgHejdQ-CcWi-G4t25nfxuhRZF57TloC8NwVgnperBB9-U-IuDvMcOnvdc9iavU/pub?gid=0&single=true&output=csv")
# Define UI
ui <- fluidPage(
# Application title
titlePanel("MnM"),
# Sidebar to take input of group ID
sidebarLayout(
sidebarPanel(
numericInput("seed","Group ID:", value = 100, min = 100, max = 999),
downloadButton("downloadData", "Download")
),
# Show the table
mainPanel(
tableOutput("table")
)
)
)
# Define server logic for the table
server <- function(input, output) {
#Create dataframe
mytable <- reactive({
set.seed(input$seed)
zz <- sample(1:nrow(db1), size = 60, replace = TRUE)
data.frame(db1[zz,])
})
#Display dataframe in table
output$table <- renderTable({
mytable()
})
#Download dataframe
output$downloadData <- downloadHandler(
filename = "test.csv",
content = function(file) {
write.csv(mytable(), file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
我需要给用户一组 60 个观察结果。我有一个 master table,我想从中提取这 60 个观察结果的子集。因此,(1) 我将主机 table 作为已发布的 csv 文件托管在 google 驱动器上。 (2) 在 R studio 中为 60 个值的子集编写一个闪亮的代码。用户将必须输入我用作 set.seed 的组 ID,并确保用户每次尝试获取 60 个观察值时看到相同的子集。而且,它还可以帮助我跟踪用户的观察结果。
代码运行良好,我能够显示子集 table。但是,我无法下载工作。我看到一个 post 说 renderTable 创建一个无法下载的 HTML table,我应该在它外面创建 table。我尝试使用 reactive 来执行此操作,但它不起作用并且不断出现各种错误。例如:
"cannot coerce class ‘c("reactiveExpr", "reactive", "function")’ to a data.frame"
将不胜感激 - 即使有人可以指出我应该阅读的内容并尝试完成这项工作。
library(shiny)
db1 <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vS94xYLix6bDUNNXAgHejdQ-CcWi-G4t25nfxuhRZF57TloC8NwVgnperBB9-U-IuDvMcOnvdc9iavU/pub?gid=0&single=true&output=csv")
# Define UI
ui <- fluidPage(
# Application title
titlePanel("MnM"),
# Sidebar to take input of group ID
sidebarLayout(
sidebarPanel(
numericInput("seed","Group ID:", value = 100, min = 100, max = 999),
downloadButton("downloadData", "Download")
),
# Show the table
mainPanel(
tableOutput("table")
)
)
)
# Define server logic for the table
server <- function(input, output) {
output$table <- renderTable({
set.seed(input$seed)
zz <- sample(1:nrow(db1), size = 60, replace = TRUE)
data.frame(db1[zz,])})
output$downloadData <- downloadHandler(
filename = "test.csv",
content = function(file) {
write.csv(output$table, file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
创建您的 table 一次,然后在您的 renderTable
和 downloadHandler
中使用它。将其创建为反应式,因此它随处可用。
请注意,downloadHandler
在 RStudio 的预览中不起作用,请改为在浏览器中查看。有一个标有 'Open in Browser' 的按钮可以执行此操作。
这是您应用的代码:
library(shiny)
db1 <- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vS94xYLix6bDUNNXAgHejdQ-CcWi-G4t25nfxuhRZF57TloC8NwVgnperBB9-U-IuDvMcOnvdc9iavU/pub?gid=0&single=true&output=csv")
# Define UI
ui <- fluidPage(
# Application title
titlePanel("MnM"),
# Sidebar to take input of group ID
sidebarLayout(
sidebarPanel(
numericInput("seed","Group ID:", value = 100, min = 100, max = 999),
downloadButton("downloadData", "Download")
),
# Show the table
mainPanel(
tableOutput("table")
)
)
)
# Define server logic for the table
server <- function(input, output) {
#Create dataframe
mytable <- reactive({
set.seed(input$seed)
zz <- sample(1:nrow(db1), size = 60, replace = TRUE)
data.frame(db1[zz,])
})
#Display dataframe in table
output$table <- renderTable({
mytable()
})
#Download dataframe
output$downloadData <- downloadHandler(
filename = "test.csv",
content = function(file) {
write.csv(mytable(), file, row.names = FALSE)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)